Linux.com

ravindra mudumby--Rsync

Posted by: Administrator on August 17, 2005 11:23 AM
Want to automate your backups? Want to do it securely? Want it to be efficient and fast? Then Rsync backups over SSH using public/private keys are for you.

The following script will setup a nightly rsync backup of a directory you specify on your host to another Unix server (whatever you use as remote_host). The remote host must be running SSHd.

remote_host=ahostname.com
directory_to_backup=/a/directory/to/backup

{
cd<nobr> <wbr></nobr>/root
# create a private key with no pass phrase.
# You want to make sure no one ever gets access to this file
# (else they'll be able to log into your remote host)
ssh-keygen -f backup.private.key -t rsa -C "backupkey" -N ""
# pop the public key into a variable
public_key=`cat backup.private.key.pub`
# add a backup user on the remote machine, install the public key
ssh $remote_host "adduser backupuser;mkdir ~backupuser/backups; mkdir ~backupuser/.ssh;echo \"$public_key\" > ~backupuser/.ssh/authorized_keys"
# back on the localhost setup a job that will do the backup

echo "
#!/bin/bash
# invoked by<nobr> <wbr></nobr>/etc/cron.daily/rsyncbackupcronjob.sh
ssh-add<nobr> <wbr></nobr>/root/backup.private.key

# copy a directory to the backup server
rsync --delete --compress --archive --rsh=ssh $directory_to_backup backupuser@$remote_host:backups/
" ><nobr> <wbr></nobr>/root/rsyncbackup.sh
chmod +x<nobr> <wbr></nobr>/root/rsyncbackup.sh

# and then you need a job to lauch the previous one. This job will execute nightly via cron
echo '#!/bin/bash' ><nobr> <wbr></nobr>/etc/cron.daily/rsyncbackupcronjob.sh
echo "ssh-agent<nobr> <wbr></nobr>/root/rsyncbackup.sh" >><nobr> <wbr></nobr>/etc/cron.daily/rsyncbackupcronjob.sh
chmod +x<nobr> <wbr></nobr>/etc/cron.daily/rsyncbackupcronjob.sh
}

Edit<nobr> <wbr></nobr>/root/rsyncbackup.sh if you want to change the files/directories you need backed up

#

Return to Making secure remote backups with Rsync