Skip to content

Instantly share code, notes, and snippets.

@mahemoff
Created December 2, 2009 14:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mahemoff/247241 to your computer and use it in GitHub Desktop.
Save mahemoff/247241 to your computer and use it in GitHub Desktop.
#!/bin/bash
# The makings of a simple backup utility. In cronjob, add:
# 45 * * * * /path/to/backup.sh
# CHANGE THIS
DAILY_BACKUP_HOUR="00" # the hour when content is dumped to permanent daily backup
function dumpValuableContent() {
curl -O http://example.com
}
# DON'T CHANGE THIS
TMPDIR="/tmp/backup$$"
CURRENT_HOUR=`date +%H`
CURRENT_DATE=`date +%y%m%d`
BACKUPDIR=$HOME/backup
#########
# UTILS
#########
function ensureExistsAndEmpty() {
if [ -d $1 ] ; then
rm -rf $1
fi
mkdir -p $* # can also pass in subdirs
}
##############################
# GET CONTENT
##############################
mkdir $TMPDIR
if [ "$?" != "0" ] ; then exit ; fi
cd $TMPDIR
dumpValuableContent
if [ "$?" != "0" ] ; then exit ; fi
##############################
# PUT INTO PERIODIC DIRS
##############################
ensureExistsAndEmpty $BACKUPDIR/hourly
HOURDIR=$BACKUPDIR/hourly/$CURRENT_HOUR
ensureExistsAndEmpty $HOURDIR
cp $TMPDIR/* $HOURDIR
if [ "$CURRENT_HOUR" == "$DAILY_BACKUP_HOUR" ] ; then
DAYDIR=$BACKUPDIR/daily/$CURRENT_DATE
ensureExistsAndEmpty $DAYDIR
cp $TMPDIR/* $DAYDIR
fi
##############################
# CLEAN UP
##############################
rm -fr $TMPDIR
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment