Automated Folder Backup Shell-Script

« AskApache.com May 2008 DreamHost Site of The MonthThe Right to Read »

Jun 12, 08

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 /home/user/scripts/mysnapshot.sh hourly &>/dev/null
@midnight /home/user/scripts/mysnapshot.sh nightly &>/dev/null
@weekly /home/user/scripts/mysnapshot.sh weekly &>/dev/null
@monthly /home/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.

« AskApache.com May 2008 DreamHost Site of The MonthThe Right to Read »


Reader Comments

Skip to form
  1. MySQL Shell Backup Script says:July 13, 3h

    [...] I’ve progressed far beyond this simple shell script, to even simpler versions that can do much [...]

Like honey.. We Keep em' Buzzin

Be polite, my .htaccess anti-spam is crazy-tight..
Please wrap any code blocks in <pre>...</pre> tags, code words in <code>...</code> tags.

WebDev Technology

Someone's Reading

Related Articles

Popular

Technology Articles

Online Tools

.htaccess Forum

Ask Apache News

Random Articles

Other Articles

This work by AskApache.com is licensed under the most accommodating license type available, just credit source according to license. .htaccess examples


Search