Mysql Sync Host: Difference between revisions
mNo edit summary |
|||
| (8 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
{{TOCright}} | |||
== Introduction == | == Introduction == | ||
| Line 17: | Line 17: | ||
== Set Up Passwordless SSH == | == 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. | 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. | ||
# Open your Mac Terminal and check for an existing SSH key: <pre>$ cat ~/.ssh/id_rsa.pub | # Open your Mac Terminal and check for an existing SSH key: | ||
# Copy your Mac's key to your shared hosting server:<pre>$ ssh-copy-id user@remote_host</pre> | <pre style="margin-left:55px"> | ||
# Test it. Run <pre>$ ssh user@remote_host</pre> If it logs you straight in without asking for your account password, you are ready. [1] | # Checks by showing the file. | ||
$ cat ~/.ssh/id_rsa.pub | |||
# Lists all rsa's in the required ssh directory. | |||
$ ls -aL ~/.ssh | |||
# If it says "No such file or directory", generate a new key by running and pressing Enter through all prompts. | |||
$ ssh-keygen -t rsa -b 4096 | |||
</pre> | |||
#<li value="2"> Copy your Mac's key to your shared hosting server:<pre>$ ssh-copy-id user@remote_host</pre> | |||
# Test it. Run <pre>$ ssh user@remote_host</pre> If it logs you straight in without asking for your account password, you are ready. [1] | |||
== Create the Compressed Sync Script == | == 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. | Now, create a script that compresses the data on the server, downloads it, extracts it, and loads it into your local MySQL database. | ||
# Create a new file on your Mac: <pre>$ nano ~/db_sync.sh</pre>Paste the following script inside (replace the UPPERCASE placeholders with your actual details): | # Create a new file on your Mac: <pre>$ nano ~/db_sync.sh</pre>Paste the following script inside (replace the UPPERCASE placeholders with your actual details): | ||
<pre style="margin-left:55px"> | |||
#!/bin/bash | #!/bin/bash | ||
# Configuration | # Configuration | ||
| Line 40: | Line 51: | ||
</pre> | </pre> | ||
# Save and exit (Press Ctrl + O, then Enter, then Ctrl + X). | #<li value="2"> Save and exit (Press Ctrl + O, then Enter, then Ctrl + X). | ||
# Make the script executable so macOS is allowed to run it: <pre>$ chmod +x ~/db_sync.sh</pre> | # Make the script executable so macOS is allowed to run it: <pre>$ chmod +x ~/db_sync.sh</pre> | ||
| Line 46: | Line 57: | ||
To make this script run entirely on its own at regular intervals, add it to your Mac's internal scheduler. | To make this script run entirely on its own at regular intervals, add it to your Mac's internal scheduler. | ||
# Open the crontab editor in your terminal:<pre>env EDITOR=nano crontab -e</pre> | # Open the crontab editor in your terminal:<pre>env EDITOR=nano crontab -e</pre> | ||
# Add a line at the very bottom defining how often you want it to run.<pre> | # Add a line at the very bottom defining how often you want it to run. | ||
<pre> | |||
* To sync every day at midnight: | |||
0 0 * * * /bin/bash /Users/YOUR_MAC_USERNAME/db_sync.sh | |||
* To sync every hour on the hour: | |||
<pre> | 0 * * * * /bin/bash /Users/YOUR_MAC_USERNAME/db_sync.sh | ||
# Save and exit (Ctrl + O, Enter, Ctrl + X). | </pre> | ||
Note: Replace YOUR_MAC_USERNAME with your actual Mac user account name. You can find it by typing whoami in the terminal. | |||
#<li value="3"> Save and exit (Ctrl + O, Enter, Ctrl + X). | |||
------------------------------ | ------------------------------ | ||
Latest revision as of 16:14, 31 May 2026
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.
- Step 1: Set Up Passwordless SSH Crucial for Automation
- Step 2: Create the Compressed Sync Script
- 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.
- Open your Mac Terminal and check for an existing SSH key:
# Checks by showing the file. $ cat ~/.ssh/id_rsa.pub # Lists all rsa's in the required ssh directory. $ ls -aL ~/.ssh # If it says "No such file or directory", generate a new key by running and pressing Enter through all prompts. $ ssh-keygen -t rsa -b 4096
- Copy your Mac's key to your shared hosting server:
$ ssh-copy-id user@remote_host
- 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.
- Create a new file on your Mac:
$ nano ~/db_sync.sh
Paste the following script inside (replace the UPPERCASE placeholders with your actual details):
#!/bin/bash # 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" # 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
- Save and exit (Press Ctrl + O, then Enter, then Ctrl + X).
- 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.
- Open the crontab editor in your terminal:
env EDITOR=nano crontab -e
- 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)