Created
April 5, 2015 02:17
-
-
Save kika/c487380dd2403d7efa2c to your computer and use it in GitHub Desktop.
This file contains hidden or 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/sh | |
# this is written in sh not bash because I run it in the FreeBSD jail | |
# on a FreeNAS box | |
# directories 1 level under this root would be backed up, one backup file | |
# per firectory. Backup files would be named 'name of the directory'-'date' | |
# f.e. /mnt/data would be backed up as data-04-04-2015 | |
# backup ignores all directories where it finds a file .gnutar.nobackup and all | |
# directories under it | |
ROOT=/mnt | |
# date format to name files | |
DATE=`date +\%G-\%m-\%d` | |
# every CYCLEDAYS the incremental backup starts from generation 0 | |
CYCLEDAYS=31 | |
FLAG=/var/run/s3backup.flag | |
# Tar program. Requires GNU tar | |
TAR=gtar | |
# S3 bucket to backup to | |
BUCKET=backup_bucket | |
do_backup() { | |
local dir=$1 | |
local incrfile=$dir/.gnutar.inc.file | |
local incrcount=$dir/.gnutar.inc.count | |
local count=0 | |
if [ -e "$incrcount" ] | |
then | |
local count=`cat $incrcount` | |
if [ $count -gt $CYCLEDAYS ] | |
then | |
count=0 | |
rm $incrfile | |
fi | |
fi | |
local bfile=`basename $dir`-$DATE-$count.tar.bz | |
echo "$DATE: Backing up $dir generation $count to $bfile" | |
if $TAR --exclude-tag-under=.gnutar.nobackup -g $incrfile -cjf - $dir | s3cmd -v put - s3://$BUCKET/$b | |
file | |
then | |
echo "Done" | |
count=$((count+1)) | |
echo $count > $incrcount | |
else | |
echo "Failed" | |
fi | |
} | |
if [ -e "$FLAG" ] | |
then | |
date=`cat $FLAG` | |
echo "Backup from $date already running, exiting" | |
exit 1 | |
else | |
echo $DATE > $FLAG | |
for d in $ROOT/* | |
do | |
if [ -d $d ] | |
then | |
if [ "x$1" = "x$d" -o "x$1" = "x" ] | |
then | |
do_backup $d | |
fi | |
fi | |
done | |
rm $FLAG | |
fi | |
exit 0 | |
# vim: ts=4:sw=4:et | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment