There are very few off-the-shelf backup systems for FreeBSD or Linux variants, primarily because each system is unique in design. As a result, a system administrator eventually must write custom backup shells scripts to perform this important operation. This article describes a generalized template for building one such custom script.
First, I obtain the current system date, and use it as the basis for all of our generated backup files. In particular, I specify the home directory compressed file name and the MySQL database dump output file:
mydate=$(date "+%Y%m%d")
HOMEFILE=/tmp/ziggyhome-$mydate.tar.bz2
MYSQLFILE=/tmp/ziggysql-$mydate.sql
I then tar the home directories. The -C / command-line argument suppresses a somewhat annoying, but harmless, leading slash warning, and the j argument implies that bzip2 compression is utilized:
tar -C / -cjf $HOMEFILE home
Next, I backup the MySQL databases with mysqldump. The database output remains uncompressed, although it could easily be piped into gzip or bzip2:
mysqldump --opt --all-databases > $MYSQLFILE
Finally, I use ncftpput to copy these files to the backup FTP site:
ncftpput -V -DD -u $FTPUSER -p $FTPPASS $FTPHOST / $MYSQLFILE
ncftpput -V -DD -u $FTPUSER -p $FTPPASS $FTPHOST / $HOMEFILE
The -DD command-line argument deletes the local file after it has been successfully uploaded.
To execute this script at regular intervals, I load the backup script into crontab:
15 4 * * 1,4 /root/backup.sh
The instruction indicates that on 4:15 AM, Monday and Thursday, the script will perform a backup. This works well for my hosts as most of the content is static or changes infrequently.
Depending on the level of security desired, a variety of file encryption tools are also available for your use. These tools include enigma, gpg, and bdes. The usage of these tools, however, are beyond the scope of this post.
Excellent post, sir.
I’m still a huge fan of using rsync as a backup solution versus a tarball, but that all depends on how much data you’re backing up, really.