backup_to_usbv0.97 April 24, 2019

Automatic Backup to an External USB Drive

Introduction

The backup_to_usb script allows external backup drives to be unplugged and plugged in without having to log in first.

Little USB disk drives are a fast and convenient backup media for backing up servers. However, one has to log in to the server in order to mount or dismount the disk drives before they can be used or removed. Using the backup_to_usb script avoids all that. The backup_to_usb script was written to look at all the USB drives attached to the computer and to automatically mount a drive if it is a backup drive without requiring anyone to log in first. To rotate your backups, one only has to unplug the current drive and plug in the next.

A disk drive is a backup drive if it has a top-level directory named "backup" (or whatever name is configured in the script).

If a drive is automatically mounted, it is automatically unmounted when the backup is finished. This allows the drive to be disconnected from the computer at any time, and a new one swapped in without having to log in.

The rsync program is used to backup the information so the backups are very fast.

Messages and errors are posted to the system log file. A complete log of the files backed up is written to a log file on the backup drive. The log file is also emailed to root and/or whoever is configured in the script.

Example backup report

Download

A gzip compressed file containing the backup_to_usb script can be downloaded here: backup_to_usb.gz

Installation

Uncompress the download file and move it to /usr/local/sbin/. Set permissions so only root (or your backup user) can run the script.

# gunzip backup_to_usb.gz
# mv backup_to_usb /usr/local/sbin/backup_to_usb
# cd /usr/local/sbin
# chmod 750  backup_to_usb

The backup_to_usb script is intended to be run automatically by cron. An example entry in /etc/crontab file to backup at 5:40am is:

40 5 * * * root /usr/local/sbin/backup_to_usb

Backup Disk Preparation

USB drives can be formatted any way your computer recognizes. FAT file systems do not handle very large files and no ownership or file privileges are kept with the file. FAT32 filesystems do not record file ownership or privileges either. NTFS filesystems require the extra preparation step of altering the privileges of the backup directory after creating it on the drive to make sure it is writable. I also had problems backing up jpg files with file names using utf-8 foreign characters. The ext2 filesystem is a preferred filesystem for backing up a Linux ext3 or ext2 disks.

To find the drive associated to the external USB drive, do:

# fdisk -l

The following assumes the external disk drive found is named /dev/sdc1.

To format the drive:

# mkfs.ext2 /dev/sdc1

To make the drive a backup drive, create the “backup” directory on it:

# mkdir /mnt/usb		# not necessary if it already exists
# mount /dev/sdc1 /mnt/usb
# mkdir /mnt/usb/backup		# the name "backup" is configurable in the script
# umount /dev/sdc1		# finish

As a final note, consider making the USB disk a bootable drive with a live distribution instead of just formatting it. That would allow the computer to be booted off the USB drive and you would have ability to repair and restore your entire system.