Mysql Sync Host

From HaFrWiki42
Revision as of 12:29, 31 May 2026 by Hjmf (talk | contribs) (Created page with "wk:{{TOCright}} == Introduction == Synchronize a remote database (Shared Hosting) with a local Db (on MacOS) is not always simple. <br>In my case I wanna synchronize using ssh access. <br>This article comes from searching Google Gemini and StackExchange. == SSH Synchronization == On macOS, you can perfectly automate this with a native Bash script combined with crontab (the built-in Mac task scheduler). <br>Because the database size can impact transfer speeds, we will u...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

wk:

Introduction

Synchronize a remote database (Shared Hosting) with a local Db (on MacOS) is not always simple.
In my case I wanna synchronize using ssh access.
This article comes from searching Google Gemini and StackExchange.

SSH Synchronization

On macOS, you can perfectly automate this with a native Bash script combined with crontab (the built-in Mac task scheduler).
Because the database size can impact transfer speeds, we will use a compressed SSH stream.
This compresses the data on the server before sending it over the network, dramatically speeding up the sync. Here is the step-by-step setup to automate your one-way sync.

  1. Step 1: Set Up Passwordless SSH Crucial for Automation
  2. Step 2: Create the Compressed Sync Script
  3. Step 3: Automate with macOS Crontab

Set Up Passwordless SSH

To make the script run automatically in the background, your Mac needs to log into your shared host via SSH without asking you to type a password every time.

  1. Open your Mac Terminal and check for an existing SSH key:
    $ cat ~/.ssh/id_rsa.pub

    If it says "No such file or directory", generate a new key by running
    $ ssh-keygen -t rsa -b 4096
    and pressing Enter through all prompts.
  2. Copy your Mac's key to your shared hosting server:
    $  ssh-copy-id user@remote_host
  3. Test it. Run
    $ ssh user@remote_host
    If it logs you straight in without asking for your account password, you are ready. [1]

Create the Compressed Sync Script

Now, create a script that compresses the data on the server, downloads it, extracts it, and loads it into your local MySQL database.

  1. Create a new file on your Mac:
    $ nano ~/db_sync.sh
    Paste the following script inside (replace the UPPERCASE placeholders with your actual details):
  2. !/bin/bash
  3. Configuration

REMOTE_SSH="user@remote_host" REMOTE_USER="remote_db_user" REMOTE_PASS="remote_db_password" REMOTE_DB="remote_db_name"

LOCAL_USER="root" LOCAL_PASS="local_db_password" LOCAL_DB="local_db_name"

  1. Execute compressed streaming sync

ssh $REMOTE_SSH "mysqldump -u $REMOTE_USER -p'$REMOTE_PASS' $REMOTE_DB | gzip -c" | gunzip -c | mysql -u $LOCAL_USER -p'$LOCAL_PASS' $LOCAL_DB

echo "Database sync completed at $(date)" >> ~/db_sync.log

  1. Save and exit (Press Ctrl + O, then Enter, then Ctrl + X).
  2. Make the script executable so macOS is allowed to run it:
    $ chmod +x ~/db_sync.sh


Automate with macOS Crontab

To make this script run entirely on its own at regular intervals, add it to your Mac's internal scheduler.

  1. Open the crontab editor in your terminal:
    env EDITOR=nano crontab -e
  2. Add a line at the very bottom defining how often you want it to run.

* To sync every day at midnight: 0 0 * * * /bin/bash /Users/YOUR_MAC_USERNAME/db_sync.sh * To sync every hour on the hour: 0 * * * * /bin/bash /Users/YOUR_MAC_USERNAME/db_sync.sh


(Note: Replace YOUR_MAC_USERNAME with your actual Mac user account name. You can find it by typing whoami in the terminal). # Save and exit (Ctrl + O, Enter, Ctrl + X).

To wrap up this automation smoothly: * Do you need the script to ignore specific tables (like massive, non-essential log tables) to make the sync even faster? * Do you know roughly how large your database is (e.g., under 100MB or multiple gigabytes)? [1] [1](https://docs.hex-rays.com/8.4/user-guide/debugger/debugger-tutorials/mac_debugger_primer2)

See also

top

Reference

top