Below is a very simple script that I've used to back up data from a remote machine with rsync. I've also scheduled it to be run daily using crontab.
#!/bin/bash
# Simple script to back up the contents of my website
#
host=HOSTNAME
user=USERNAME
dest=BACKUP_DESTINATION
#SSH_AUTH_SOCK need to declare for rsync to work via a cronjob, you may need to use a different value
export SSH_AUTH_SOCK=/tmp/launch-LaeM5h/Listeners
echo "Starting Website Backup" `date`;
function sync() {
if [ ! -e $2 ]
then
mkdir $2;
fi
echo "Backing up $host:$1 to $2";
rsync -raz -e "ssh -i $HOME/.ssh/id_dsa" $user@$host:$1 $2;
}
sync "/home/$user" "$dest";
dir=ZIP_DESTINATION/`date "+Backup_%d_%m_%y"`;
echo "Copying files to " $dir;
tar -czf $dir.tar.gz $dest
echo "Finished Website Backup" `date`;
Crontab setup
The first thing you need to do is setup up Passwordless ssh with NO passphrase.
You then need to set up a cronjob to run the script daily.
For example create a file crontab.txt with the following content:
0 12 * * * /PATH_TO_SCRIPT/backup.sh > /PATH_TO_LOG/backup.log 2>&1
Next load this file into crontab (this will delete other cronjobs if they exist but aren't included).
crontab crontab.txt
To check that your cronjob has been loaded run:
crontab -l
No comments:
Post a Comment