Backup your data with cron, rsync and lftp
Thursday, February 12th, 2009This post will explain how I do my backups.
In case my crontab should be deleted or lost, I have the script in an own file. Like in my code-nippets.
My computer is only used by me so I only need to backup my home-dir, where I save everything. /dev/sda5 is mounted to /home.
This script backups my home directory to one of my external harddrives, as long as the backup dir on the disk hasn’t been edited the last day and rsync is not running. Off course, rsync could run in other situations, but I don’t want to run two rsync’s at once and is therefore added as a case. It would probably consume all the bandwidth on my laptop when running two rsyncs on my slow 5400rpm harddrive.
if [ -d "/media/WD Elements/backup/" ] && [ -z "`ps --no-heading -C rsync`" ] && [ "`ls -ld /media/WD\ Elements/backup | awk '{print $6}'`" != "`date '+%Y-%m-%d'`" ];
then
rsync -au --force --delete-during /home/vegard/. /media/WD\ Elements/backup;
fi
Code above is from “/home/vegard/Linux/backup.sh” which runs every 5 min, using crontab. Use `crontab -e` to insert your cron-rule.
*/5 * * * * /bin/bash /home/vegard/Linux/backup.sh
Keeping backup of your webhotel is also important, and since I’m allowed ssh access to mine I can use sftp to download my files.
In “/home/vegard/Linux/sftp.sh” i have:
lftp -e "mirror . /home/vegard/Backup/hammerseth.com/" sftp://<user>@<host>/path
For the password, you can use ssh-keyrigs or if you dare sftp://<user>:<passwd>@<host>/path
The file above is run each sunday at 18:00 with this cron-rule
0 18 * * 0 /bin/bash /home/vegard/Linux/sftp.sh
I should also mention that I’m using a wordpress-plugin called WordPress Database Backup which mails all my database tables to my email, every week. Quite handy.
Be safe.


