Create daily backups on an encrypted disk image

May 17, '04 11:05:00AM

Contributed by: ppatoray

With all of the recent news about new trojan programs that can delete a user's home folder, I thought I might share my backup routine, especially since many of the pieces I have assembeled came from knowledge picked up here. I run the following script when I go to sleep. It backs up a number of pieces of system data, like my Apache2 document root and my MySQL databases, to my home folder, and then archives my home folder to an encrypted disk image and stores it on my external FireWire drive. It then will continue and archive other specified user accounts to their own encrypted disks.

I've tried to provide as much commenting through the script as I could; if you copy and paste, make sure that you get the line breaks correct (three lines have been split here for easier reading). It has to be run as root or with sudo privledges, or as I have done, attached to the periodic entry for daily, /etc -> periodic -> daily -> 500.daily with the line sh /Users/User1/bin/DailyMaintenance. It also contains passwords, so it should be chmodded to 700 and stored somewhere safe.

I'm curious what other vital pieces of data that others might suggest backing up (/etc folder, /var/logs, etc.), and if anyone has any ideas on how to make this do its thing faster. It takes about three to four hours to back up the two home directories on my PowerBook, which are only about 5 gigabytes total.

Read the rest of the hint for the script ... it's also available on my website [4KB download].

#!/bin/sh

# User1
# Make archive directory in /Users/User1 to hold system data
echo "Deleting and recreating Daily Archive folder"
rm -rf /Users/User1/Archives/
mkdir /Users/User1/Archives/

# Remove any .DS_Store files and backup Document Root
echo "Removing .DS_Store files"
find /Library/Apache2/htdocs/. -iname ".DS_Store" -delete
echo "Archiving /Library/Apache2/htdocs"
tar czf /Users/User1/Archives/ApacheDocRoot.tar.gz /Library/Apache2/htdocs
echo "Archiving /Library/Apache2/conf"
tar czf /Users/User1/Archives/ApacheConfig.tar.gz /Library/Apache2/conf
echo "Archiving /Library/Apache2/logs"
tar czf /Users/User1/Archives/ApacheLogs.tar.gz /Library/Apache2/logs

# Dump and Zip up SQL Data
echo "Dumping Database Data"
# NEXT TWO LINES ARE ONE LONG LINE (use a space between the two parts)!
/Library/MySQL/bin/mysqldump --user=XXXXXXXX --password=XXXXXXXX -A >
  /Users/User1/Archives/SQLDump.txt
echo "Zipping up Database Data"
# NEXT TWO LINES ARE ONE LONG LINE (use a space between the two parts)!
tar czf /Users/User1/Archives/SQLDump.txt.tar.gz
  /Users/User1/Archives/SQLDump.txt
echo "Removing Dump File"
rm /Users/User1/Archives/SQLDump.txt

# Creating the backup disk image file and volume name
echo "Creating archived .dmg from /Users/User1"
SOURCE='/Users/User1'
FILEDEST='/Volumes/Backups/Archives'
VOLUMENAME=`date +%Y-%m-%d`_User1
IMAGENAME=$FILEDEST/$VOLUMENAME.dmg


# NEXT TWO LINES ARE ONE LONG LINE (use a space between the two parts)!
hdiutil create -srcfolder $SOURCE -encryption -passphrase XXXXXXXX
  -fs HFS+ -volname $VOLUMENAME $IMAGENAME

########################################

# User2
echo "Creating archived .dmg from /Users/User2"
SOURCE='/Users/User2'
FILEDEST='/Volumes/Backups/Archives'
VOLUMENAME=`date +%Y-%m-%d`_User2
IMAGENAME=$FILEDEST/$VOLUMENAME.dmg

# NEXT TWO LINES ARE ONE LONG LINE (use a space between the two parts)!
hdiutil create -srcfolder $SOURCE -encryption -passphrase XXXXXXXX
  -fs HFS+ -volname $VOLUMENAME $IMAGENAME

echo "Daily Backups Completed"
[robg adds: I haven't tested this one...]

Comments (21)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20040514101743247