Automated Rsync Backup

  • Post author:
  • Post category:Tools

Here’s how to setup an automated backup process using Rsync.

1. Test rysnc using a password. Here we will be copying files from /src-dir to /dest-dir on username@home.

 > rsync -rvz -e 'ssh -p 22' --progress /src-dir username@host:/dest-dir

2. Now let’s automate Rsync. Create the public and private keys on the source machine.

 > ssh-keygen

3. Copy the public key to username’s ~/.ssh/authorized_keys file in the destination machine.

 > ssh-copy-id "username@host -p 22"

4. Test SSH using key authentication.

> ssh "username@host -p 22"

5. Test Rsync using key authentication.

 > rsync -rvz -e 'ssh -p 22' --progress /src-dir username@host:/dest-dir

6. Add Rsync to daily cron.

 > cd /etc/cron.daily/
> echo 'rsync -rz -e "ssh -p 22" /src-dir username@host:/dest-dir' >> rsync.cron
> chmod u+x rsync.cron

7. In case you want to use a password instead (not recommended), you can use sshpass.

 > rsync -rvz -e 'sshpass -p password ssh -l username -p 22' --progress /src-dir username@host:/dest-dir