Wrote another script today, this time to help me in my weekly backups. I’ve been running rsync as a cronjob to mirror all of my files weekly so I that I have a relatively recent backup in case of system failure or infection. I don’t use RAID because I don’t have the hardware and software RAID would suck resources, but primarily because RAID is meant to protect against hardware failure and to keep the system running, where I am more concerned with viruses or – more likely – mucking up my own system. With RAID the errors would be on all the drives and then I’d be screwed.
Anyway, I used to be backing up an 80gb internal IDE drive to a second one, but now I have a 160gb drive in an external enclosure, so now I’m backing up both internals to the external via Firewire. The problem that can arise with an external hard drive (as I discovered the first time I tried this) is that if for some reason it is not mounted (power is off, cable is disconnected, firewire module is throwing a fit), just running rsync will copy the entire drive onto itself in a folder where the mount should be. At least until it’s completely full and the system dies. Which is what happened to me. So I wrote a Bash script to check if the drive is mounted, attempt to mount it if it’s not, and then either run rsync to backup or exit with an error.
#!/bin/bash
BACKUPHD='/external'
EXCLUDE='/home/michael/rsync.exclude'
backup() {
echo -e "Running rsync to backup to $BACKUPHD\n"
rsync -ac --delete --exclude-from=$EXCLUDE / $BACKUPHD
}
if grep $BACKUPHD /etc/mtab
then
backup
exit 0
else
echo "Backup drive not mounted! Attempting to mount... "
if mount $BACKUPHD
then
echo -e "Success!\n"
backup
exit 0
else
echo -e "Failed!\n"
exit 1
fi
fi
exit 0
And the crontab for every Sunday at 3am:
# Backup
0 3 * * 0 /home/michael/sysutils/backup.sh
Tags: linux
This was a great example … I didn’t realize checking for mount would be so easy…. seeing the script flow also was educational.
Thanks.
Looks good, will definitely use it :]
Hi
Great script. I use this to backup to an external USB drive.
Problem is that if I disconnet the usb drive, the script does “not work”.. but thats okey.. What I have tried figuring out is how can I check if the disk /dev/disk/by-id/somethinglonghere is connected or not connected – before I run this script ?
Hi Stig, glad to hear this was of use to you. If your system auto-mounts the drive — I think most modern Linux distributions do — this script already checks to see whether it is mounted before it runs the backup and, if not, it lets you know the drive isn’t there.
If for some reason your system doesn’t mount the drive as soon as you plug it in, perhaps this post might set you in the right direction:
http://themikecam.com/blog/2005/07/14/assign_permanent_device_name_to_removabl/
It’s quite old, however, so I’m not sure whether that’s still a good solution.
Forgot to mention that I myself have moved towards more configurable/featureful GUI-based solutions, such as Unison and now Time Drive and Back in Time, though none of them seems to do as good a job of supporting removable media as I would like. Really, all I want is a scheduled, automatic backup which is fast, accurate, will just prompt me when it wants me to plug in my media instead of failing, and generally is as painless as possible.
Thanx for prompt respons.
I use Centos 5.0 with no gui. I have several USB disks connected and use these to
-store images on one HDD, which is shared and readable though PCs Picasa. Works great!
-another HDD with NFS with shares movies to my popcorn hour players (yes I have 2 ;)
-connect a 250Gb HDD now and then which a cron-job, does backup images/pictures and valuable data. This is disk I physically connected now and then (mostly weekends..) and a script run with cron.daily helps me “mount, backup stuff to the disk and umount it again”. Then I can easily disconnet it (just physicaly unplugg it) and put the disk in a fireproof safe we have.
I.e. if you unplugg the USB disk (know as /media/mountfolder) – you will see that the /media/mountfolder will be empty if you try to “ls -la” it.. of “df” etc…
So what I was looking for was a “if statement?” that would check if the disk is physically connected to the PC or not.