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. It backs up /home, /etc, /usr and /var.
Download and use version 0.3.1 for free: backup-to-hotswap-usb-disk.sh.
Download and use version 0.2.1 for free: backup-to-hotswap-usb-disk-0.2.1.sh (old version, not recommended in most cases).
You need to do the following manual steps to set it up:
Configuration 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! - Connect the disk to another computer that isn't the server, for safety's sake. Become superuser. Change the partition's filesystem type to ext3:
# cfdisk /dev/<disk device reference i.e. sdb>
Select the partition → Type → 83 → Write → yes [Enter] → Quit - If the disk has now been automatically mounted by something like GNOME or KDE, you won't be able to complete the next step without unmounting it by either, 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 (and an inode size of 128 bytes rather than the default of 256 bytes so the Ext2IFS driver on Windows can read it):
# mkfs.ext3 -c -c -I 128 /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 - Put a file on the disk to signify which disk in particular it is, for example 'backup-disk-1', so you can differentiate between disks when logged in remotely:
- If you have disk auto mounting software running, unplug the disk and re-attach it; if you don't then:
# mkdir /media/backup# mount -t ext3 -L backup /media/backuptouch /media/backup/backup-disk-<number>- Unmount the disk
Configuration On The Server
- Download the latest version of 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 02:00:
# echo "00 2 * * * root /root/backup-to-hotswap-usb-disk.sh" >> /etc/crontab - Create a /etc/fstab entry, for the disk labeled 'backup' to mount to /media/backup:
# echo "LABEL=backup /media/backup ext3 defaults,noauto 0 0" >> /etc/fstab
(If by chance you're using a desktop environment with an auto disk mounting feature, 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)
Notes
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.
To start the backup manually, from a remote terminal, and be able to logout, leaving the backup running, use this command when logged in as root: ~/backup-to-hotswap-usb-disk.sh &. Note however that when you logout it will fail to logout (this may be fixable).
When the backup is running you should see processes such as these when using top:
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
3237 root 18 0 1864 592 1704 R 1.0 0.1 0:00.18 cp
702 root 10 -5 0 0 0 D 0.3 0.0 4:30.13 usb-storage
If you've amended your backup script to instead use scp rather than cp then when the backup is running you should see processes such as these when using top:
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
12265 root 15 0 5204 3004 1928 S 10.7 0.3 1:37.82 ssh
12264 root 18 0 4228 1236 956 S 2.0 0.1 0:14.08 scp
702 root 10 -5 0 0 0 D 0.3 0.0 4:30.13 usb-storage
If a file is busy during the backup then it won't be backed up. A record of such files will be kept in the log, with the comment 'Text file busy'.
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 www.fs-driver.org/relnotes.html.
Occasionally you should perform a filesystem check on the backup disk by doing the following:
- Unmount the backup disk, in case it is already mounted:
umount /media/backup - Do filesystem check:
e2fsck -p /dev/<disk partition reference i.e. sdb1>
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.
If instead of backing up the same server as the backup script is running on, you want to backup a remote server, replace in the script 'cp' with 'scp -p -r root@server:'; and remove the '--archive --update' (which has the downside that all files will be copied regardless, so the backup will take considerably longer to run). So this command isn't prompted for a password, create a certificate on the remote server and put it on the local server (details to be added later on how to do this).
Changes To This Page
1.5.0 - 10 June 2009
- The current version 1.11a of the Ext2IFS driver (recommended here for reading the backup disk under Windows) only mounts volumes with an inode size of 128 bytes. Recent versions of Linux's mkfs.ext3 will format the disk with an inode size of 256 bytes and when attached to Windows it will say it can't read the disk and ask if it is to be formatted.
This guide now specifies an inode size of 128 bytes when formatting the disk.
The fix for existing disks is to format them again using
mkfs.ext3 -I 128 /dev/<disk partition reference i.e. sdb1>(which will delete everything on the disk) and name them again usinge2label /dev/<disk partition reference i.e. sdb1> backup.You can find the inode size of an existing partition using
tune2fs -l /dev/<disk partition reference i.e. sdb1>as root, amongst the information will be something like Inode size: 256 or Inode size: 128.
1.4.1 - 27 April 2009
- Changed time of backup again to run at 02:00 so doesn't clash with mail server setting to delete mail marked for deletion at 01:00.
1.4.0 - 15 April 2009
- Instead of /etc/fstab using, for example, '/dev/sdb1', instead use 'LABEL=backup'. This fixes the issue on some systems where, if the USB-attached disk is attached on startup, instead of being, say, /dev/sdb1, it becomes /dev/sda1. This needs to be done in conjunction with labeling other fixed disks and referencing them in both fstab and GRUB's menu.lst kernel root setting.
- Changed the backup script to run at 01:00 rather than 04:00 to reduce the chance it'll still be running at 09:00 (likely if USB is version 1.1 rather than 2.0) when people may be wanting to use the system again.
Changes To The Script
0.3.1 - 2 February 2010
No changes are required in order to deploy this new version over version 0.3.0 other than to replace the file backup-to-hotswap-usb-disk.sh.
- Fix to only create each top-level directory if it doesn't already exist, otherwise when run under root's crontab root is emailed errors such as 'error: mkdir /media/backup/home'. Additionally if these directories already exist but as a symlink directory or file then they're first deleted.
0.3.0 - 12 November 2008
No changes are required in order to deploy this new version over version 0.2.1 other than to replace the file backup-to-hotswap-usb-disk.sh.
- If the backup disk was already mounted, it would fail to mount again and so the backup wouldn't run. An already mounted backup disk can now be coped with.
- Now saves log in /var/log/hotswap-backup/ rather than /var/log/, otherwise there are far too many log files messing up /var/log/. The new directory /var/log/hotswap-backup/ is automatically created if it doesn't already exist.
- The backup log filenames now include a description of which part of the backup they relate to 'backup-<location>-<time and date>' rather than each being called 'backup-<time and date>'. These are created when files are copied or attempted to be copied. An empty file should mean that the backup was successful. Example log filenames:
backup-general-2008-11-12-04-00-02
backup-home-2008-11-12-04-00-07
backup-etc-2008-11-12-07-30-01
backup-usr-2008-11-12-07-30-15
backup-var-2008-11-12-08-01-49 - An additional backup log file called backup-general-<time and date> has been added for issues with mounting media. You now get the specific reason for the mount failure, rather than just 'FATAL: Backup did not run because error mounting media' for all different types of mount failures. Only if the disk isn't already mounted do you get the log file, empty or not; it should be empty if there were no issues.