Part 2 – Web pages synchronization using rsync over ssh
public key authentication for webhost account
We need to ssh to webhost without password. Refer to usage series 5 article to setup private/public key for webhost account.
Make sure no passpharse assigned to the private key.
Setup rsync script on NAS
- SSH to the NAS
- install bash/nail if not yet installed
# ipkg install bash
# ipkg install nail
- Create the rsync script
# vi /opt/usr/local/bin/rsync.sh
- Paste the content to the script
/usr/syno/bin/rsync -avz --log-file=/opt/var/log/rsync.log --copy-dirlinks --exclude-from=/opt/etc/exclude-remote-file -e "ssh -i /volume1/share/hostgator/id_rsa -p 22" firstname.lastname@example.org:/home/id/public_html /volume1/web
/opt/var/log/rsync.log - The log files for debug
/opt/etc/exclude-remote-file - This is the file contains a list of files and directories to be excluded from the synchronization, if you wanna sync everything, keep the file empty.
/volume1/share/hostgator/id_rsa - The private key used to login to the web host account.
-p 22 - defines the SSH port, some webhost using custom port like 2222 (hostgator)
email@example.com - the login ID of SSH
/home/id/public_html - The SOURCE folder. Which is the directory of the webhost account to be mirrored. NOTED (IMPORTANT!) This directory MUST matched the sub folder of the virtual host we setup eariler. If the directory we gonna mirror is /home/id/public_html/subdomain, then the sub folder of the virtual host should be subdomain instead of public_html.
/volume1/web - The Destination folder. Define where to host the mirror site. The virtual host sub folder will be created here.
- Create the exclude file list
# vi /opt/etc/exclude-remote-file
- Edit the exclude file list, for example
- Add a cron job to run the rsync script daily
# vi /etc/crontab
Example below execute the script every 12pm.
0 12 * * * root /opt/bin/bash /opt/usr/local/bin/rsync.sh | /opt/bin/nail -s "Sync job done" firstname.lastname@example.org
!!! Make sure the db backup plugin already generated the backup script before executing the cron job!