Title: Daily Backup Preparations
Course: CIS 208: Linux/UNIX III: Intermediate System Administration, Networking, and Security ~ For IGNITE Review > Section: Learning Modules

Daily Backup Preparations

Since bare metal backups are designed explicitly to restore a system after a catastrophe, they may not be the best solution for daily backups of user files.  The best software for backing up user files provides users with an interface which does not require administrative assistance.  The user will then be able to see multiple recent versions of any given file, see the date(s) of each version, and restore any of the versions to a chosen location.  Ideally the software will provide de-duplication and compression to maximize use of storage media, and it will not require client software to be installed.

It is essential to plan for the preparation of system files and settings which cannot be backed up properly as normal files.  The two examples of such data are:

- The MBRs of each hard drive
- The data within a database.

To prepare a backup of the MBR, simply use dd to create an image file.  A very short script in /usr/local/sbin would make this easy to run when needed.

For example, if there were two hard drives, sda and sdb, the following would create the image files:

dd if=/dev/sda bs=512 count=1 of=/boot/sda.mbr.img
dd if=/dev/sdb bs=512 count=1 of=/boot/sdb.mbr.img

All the major database programs have a way to extract data for each database into a flat file which can then be used to restore the databases.  The extraction of data allows the databases to run with no interruption of service.

For example, the following command would archive the mysql database into a file in /var/lib/mysql-bu.

mysqldump --opt -u root -pSecRet mysql > \
/var/lib/mysql-bu/prod-server.mysql.sql

A script in /usr/local/sbin could backup all the databases.

In this example, the password for the root account for mysql is stored in plain-text, so plan a method to hide the contents from normal users.

mkdir -P /var/lib/mysql-bu
cd /var/lib/mysql-bu
HostName=${HostName:=`hostname -f`}
User=${User:=root}
Passwd="${Passwd:=SecRet}"
mysql -u $User -p"$Passwd: -B -e "show databases;" | tail -n +2 |\
while read DbName; do
if [ "$DbName" != information_schema ]; then
mysqldump --opt -u $User -p"$Passwd" $DbName > $HostName.$DbName.sql
fi
done

After writing the scripts, test to make sure they run correctly without errors.  Most backup software allow one or more scripts to be run prior to copying the files.  If only one script is allowed, write a single script that will run all the other scripts.  An alternative is to set up a cronjob to run the script prior to the backup.