Created
December 2, 2009 14:46
-
-
Save mahemoff/247241 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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