Auto backup a server to a hotswap USB disk
This (BASH, command-line) script will allow you to connect a USB-attached hard disk, leave it overnight to get backed-up to, unplug it the next day and take it away. It is intended to be run on a server.
Download and use version 0.2.1 for free: backup-to-hotswap-usb-disk.sh.
You need to do these manual steps to set it up:
On Each Backup Disk
- Connect the disk to the computer you'll be using it on. Get the disk's device reference:
# fdisk -l
That produces something like the following, amongst other disks:
Device Boot Start End Blocks Id System /dev/sdb1 1 19456 156280288+ c W95 FAT32 (LBA)
There will likely be multiple blocks of disks, your internal hard disk(s) which will either be /dev/hdaX /dev/sdaX and then your removable which will be /dev/sdaX or /dev/sdbX. The disk will likely already be partitioned with a (probably FAT16 or FAT32 filesystem) single partition, as above.
YOU HAVE TO BE COMPLETELY SURE YOU HAVE THE CORRECT DISK DEVICE AND DISK PARTITION REFERENCE ELSE YOU COULD DESTROY YOUR SERVER! - Redefine the partition's filesystem type that will be used on it interactively using:
# cfdisk /dev/<disk device reference i.e. sdb>
Select the partition → Type → 83 → Write → yes [Enter] -> Quit - If the disk has been automatically mounted, such as if you have GNOME auto mounting such disks, you won't be able to complete the next step. In this case you should unmount it by either, from within GNOME, right-clicking on it and choosing to unmount; or, from the command-line umount /media/<name of disk>
- Format (and verify) the disk with the Linux ext3 filesystem:
# mkfs.ext3 -c -c /dev/<disk partition reference i.e. sdb1>
You should see the progress of the formatting. This will take a long time; the two -c's cause it to do a read-write bad block test, you can speed it up by using just one -c so it does just a read test, or remove both -c's to not test for bad blocks at all. - Name the disk 'backup':
# e2label /dev/<disk partition reference i.e. sdb1> backup
On The Server
- Download the backup script to root's home directory:
# wget http://thegoldenear.org/toolbox/unices/backup-to-hotswap-usb-disk.sh -O /root/backup-to-hotswap-usb-disk.sh - Make it executable:
# chmod +x /root/backup-to-hotswap-usb-disk.sh - Create a /media/backup directory:
# mkdir /media/backup - Schedule this backup script to run daily at 04:00:
# echo "00 4 * * * root /root/backup-to-hotswap-usb-disk.sh" >> /etc/crontab - Create a /etc/fstab item in advance for the <disk partition reference i.e. sdb1> to mount to /media/backup using the following line in /etc/fstab, changing it to suit your disk partition reference:
# echo "/dev/<disk partition reference i.e. sdb1> /media/backup ext3 defaults,noauto 0 0" >> /etc/fstab
(If this operating system is Debian and you're using something like GNOME with its gnome-volume-manager to auto mount disks, with the disk named 'backup' it should auto mount to /media/backup when connected so you shouldn't need this but you'd also possibly need to take the mount command out of the backup script)
Note
If you want to test the backup is going to work you can run it manually by logging in as root and running ~/backup-to-hotswap-usb-disk.sh. You should see files and directories being added in /media/backup.
Beware of USB extension cables, they can have sloppy connectors that cause the disk drive to fall off. Those that come with the Freecom Toughdrive for example are unusable. If this is happening you'll see messages like this in dmesg:
sdb:<6>usb 6-7: reset high speed USB device using ehci_hcd and address 2 usb 6-7: reset high speed USB device using ehci_hcd and address 2 usb 6-7: device descriptor read/64, error -71 sd 2:0:0:0: scsi: Device offlined - not ready after error recovery sd 2:0:0:0: SCSI error: return code = 0x00050000 end_request: I/O error, dev sdb, sector 24 Buffer I/O error on device sdb, logical block 3 usb 6-7: USB disconnect, address 2 sd 2:0:0:0: rejecting I/O to offline device Buffer I/O error on device sdb, logical block 3 unable to read partition table
To read the backup disk from a machine running Microsoft Windows you can install 'Ext2 Installable File System For Windows' from http://www.fs-driver.org/relnotes.html.
If you add or remove a fixed SCSI hard disk in the machine (or an ATA disk for some Linux distributions) then the 'disk device reference i.e. sdb' and 'disk partition reference i.e. sdb1' will change accordingly. You should not have the backup disk drive attached to the machine whilst you attach such a new fixed disk (in the case of hotswap fixed disks) and whilst you power up the machine afterward (in the case of non-hotswap fixed disks) so that the fixed disk can take the backup disk's more stable device reference and the backup disk then take another. Afterward, use fdisk -l to re-determine the backup disk's device reference and correspondingly change the backup disk's disk partition reference in /etc/fstab, for example replacing sdb1 with sdc1.