This simple unix shell script automatically creates backups of a specific folder at regular hourly, nightly, weekly, and monthly intervals. DreamHost has this feature for their main accounts but for those of us running on the new Private Servers they haven't set it up yet. The old perl script they use on the main accounts does not work with the higher security of the PS kernels and virtual server setup.

I just learned that using your dreamhost account for backups like this shell-script is a violation of the Terms of Service, so please don't use this method on DreamHost.

Why Automated Backups

I make a lot of mistakes while developing and hacking code for my sites and I've come to rely on having access to these backup versions so I can quickly revert. Contacting support to get access to them is a waste of everyones time compared to finding a solution, so I did. The DreamHost wiki has a couple of complicated but workable solutions for backups but not for PS users, and nothing this simple. Just another great feature of DreamHosts debian linux based web hosting.

My Shell Script

Instead of the usual method for copying directory trees using tar with fifo, pipes, or DreamHost's rsync and NFS method this script uses cpio which is much much faster and has a lot of cool options like saving m/a/c times and symlinks, and also being able to handle weird characters and file names.

mysnapshot.sh shell script

#!/bin/bash
#
# GNU Free Documentation License 1.2
# 06-11-08 - AskApache (www.askapache.com)
#
#
#    .mysnapshot
#    |-- hourly.0    (one hour ago)
#    |-- nightly.0   (one night ago)
#    |-- weekly.0    (one week ago)
#    `-- monthly.0   (one month ago)
#

### Source and Destination
source=$HOME/sites
dest=$HOME/.mysnapshot/${1:-hourly}.0/`basename $source`

### Make Nice - lower load
renice 19 -p $$ &>/dev/null

### Non-Absolute links, check source exists
cd $source || exit 1

### Hide errs, copy dirtree
find . -depth -print0 2>/dev/null | cpio -0admp $dest &>/dev/null

cd $OLDPWD

exit 0

mysnapshot.sh crontab entries

MAILTO=user@domain.com
# MY SNAPSHOTS
@hourly /web/user/scripts/mysnapshot.sh hourly &>/dev/null
@midnight /web/user/scripts/mysnapshot.sh nightly &>/dev/null
@weekly /web/user/scripts/mysnapshot.sh weekly &>/dev/null
@monthly /web/user/scripts/mysnapshot.sh monthly &>/dev/null

Just save the script to your server, chmod u+x, add the crontab entries, and you will have automated backups of any folder you want other than your HOME folder.


Stay tuned, I'm learning some really incredible stuff right now *if you run with open-source* and will be posting more tutorials soon about some really cool stuff.

Shell Scripting

Comments