Synology usage series 28 – Mirroring wordpress from webhost to synology box – the automatic way


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

  1. SSH to the NAS
  2. install bash/nail if not yet installed


    # ipkg install bash
    # ipkg install nail

  3. Create the rsync script

    # vi /opt/usr/local/bin/rsync.sh

  4. Paste the content to the script

    #!/opt/bin/bash
    /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" id@webhost.net:/home/id/public_html /volume1/web

    Parameters:


    /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)

    id@webhost.net - 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.

  5. Create the exclude file list

    # vi /opt/etc/exclude-remote-file

  6. Edit the exclude file list, for example

    public_html/.htaccess
    public_html/other_sub_domain/*
    public_html/test/*

  7. 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" email@address.com

    !!! Make sure the db backup plugin already generated the backup script before executing the cron job!





Leave a Reply

Your email address will not be published. Required fields are marked *