How To Rsync

From wiki

Jump to navigation Jump to search

How to setup cygin and Rsync to do automatic, fast backups in Windows XP. The backup will run every night at 1:20AM (see the crontab step below). An email is sent indicating what was backed up.

Rsync compares two directory trees and sends only the differences. The example script below will backup a website directory (RSremote) to a local drive (RSlocal). This is useful if you have data in a wiki that you want to backup for example.

Note that this works correctly only if sshd is running a linux server. It does not work with sshd running under cygin. See HowToRsyncd and HowToRsyncToRsyncd to get around this problem.

  * Assumes:
     * cygwin is installed
     * there is a directory called e:\bin that contains scripts, etc.
     * there is a drive f: that will contain all backups
  * setup cygwin packages. These are needed below
     * install openssh
     * install rsync
     * install ssmtp
     * install cygrunsrv
     * install cron
  * set up an excludes file called backupwebsites_excludes.txt. Here is the content:

[ .svn index/*** ]

     * the first line excludes subversion subdirectories
     * the second line excludes a subdirectory called 'index' and everything underneath it
  * create a backup script called


  1. ! /usr/bin/bash

RSoptions='-zvrth -e ssh --stats --delete --temp-dir=/tmp --exclude-from=backupwebsites_excludes.txt' RSremote='' RScmd="/usr/bin/rsync $RSoptions" RSroot='/cygdrive/f/backups' RSlocal=$RSroot/websites RSlog=$RSroot/backupwebsites.log.txt RScurrlog=$RSroot/backupwebsites.current.txt

echo Subject: rsync Backup Results > $RScurrlog echo To: >> $RScurrlog echo From: rsyncbackup >> $RScurrlog echo '=============================' >> $RScurrlog /usr/bin/date >> $RScurrlog

  1. note no trailing slash!

RScurrdir=somedir/cgi-bin/somesubdir echo ===== backing up $RScurrdir ... >> $RScurrlog mkdir -p $RSlocal/$RScurrdir $RScmd $RSremote/$RScurrdir/ $RSlocal/$RScurrdir --log-file=$RScurrlog

/usr/sbin/ssmtp -t < $RScurrlog

cat $RScurrlog >> $RSlog ]

  * setup password certificates so ssh does not ask for them:
     * see
     * On the client run the following commands:


        $ mkdir -p $HOME/.ssh
        $ chmod 0700 $HOME/.ssh
        $ ssh-keygen -t dsa -f $HOME/.ssh/id_dsa -P 


     * This should result in two files, $HOME/.ssh/id_dsa (private key) and $HOME/.ssh/ (public key). 
     * Copy $HOME/.ssh/ to the server.
     * On the server run the following commands:


        $ cat >> $HOME/.ssh/authorized_keys
        $ chmod 0600 $HOME/.ssh/authorized_keys


     * On the client test the results by ssh'ing to the server:


        $ ssh -i $HOME/.ssh/id_dsa yeruserid@server


  * setup ssmtp to send the email at the end:
     * mkdir /etc/ssmtp
     * run ssmtp-config
        * mailhub=your.isp.mail.server.url
        * hostname=YOUR_COMPUTER_NAME
        * when asked if you want to link sendmail, say yes.
  * setup cron as an NT service
     * cygrunsrv -I cron -p /usr/sbin/cron -a -D
     * cygrunsrv -S cron
     * cygrunsrv -L
  * setup the backup script in crontab
     * crontab -e 
     * (wait for VIM)
     * add line: 20 1 * * * /cygdrive/e/bin/
Personal tools