Created
March 16, 2016 16:03
-
-
Save kevashcraft/0ad1fe2f3c3b08ee026c to your computer and use it in GitHub Desktop.
Kevin's Bash Backup Program
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 | |
# Kevin's BASH Backup Program | |
# - runs every $interval | |
# - at midnight moves most recent to daily folder | |
# - on first day of the week moves most recent to weekly folder | |
# - on first day of the month moves most recent to monthly folder | |
# - deletes oldest interval backups after $keepinterval | |
# - deletes oldest weekly backups after $keepweekly | |
# - deletes oldest daily backups after $keepdaily | |
# CONFIG VARS | |
interval=15 | |
keepinterval=96 | |
keepdaily=31 | |
keepweekly=52 | |
busource=/testsource | |
budest=/testbackups | |
# SETUP | |
# make backup directories | |
if [ ! -d $budest ]; then | |
mkdir $budest | |
fi | |
if [ ! -d $budest/interval ]; then | |
mkdir $budest/interval | |
fi | |
if [ ! -d $budest/daily ]; then | |
mkdir $budest/daily | |
fi | |
if [ ! -d $budest/weekly ]; then | |
mkdir $budest/weekly | |
fi | |
if [ ! -d $budest/monthly ]; then | |
mkdir $budest/monthly | |
fi | |
# GET DATE INFO | |
# set argument as date, used for testing | |
if [ ! -z "$1" ]; then | |
givendate="-d $1" | |
fi | |
datetime=`date +%Y-%m-%d-%H-%M $givendate` | |
hourminute=`date +%H-%M $givendate` | |
dayofmonth=`date +%d $givendate` | |
dayofweek=`date +%w $givendate` | |
lastdate=`ls -l $budest/interval/ | grep -o '[0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\}-[0-9]\{2\}-[0-9]\{2\}$' | tail -n 1` | |
# BACKUP COMMANDS | |
rsync -avh --link-dest=$budest/interval/$lastdate $busource/ $budest/interval/$datetime/ > /dev/null | |
# MOVE/DELETE FOLDERS | |
# if it's a new day... | |
if [ "$hourminute" = "00-00" ]; then | |
# MOVING... | |
# move the most recent interval to daily | |
if [ -d $budest/interval/$lastdate ]; then | |
mv $budest/interval/$lastdate $budest/daily/ | |
fi | |
# on the first day of the week, move the most recent daily to weekly | |
if [ "$dayofweek" = "0" ]; then | |
lastdaily=`ls -l $budest/daily/ | grep -o '[0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\}-[0-9]\{2\}-[0-9]\{2\}$' | tail -n 1` | |
if [ -d $budest/daily/$lastdaily ]; then | |
mv $budest/daily/$lastdaily $budest/weekly/ | |
fi | |
fi | |
# on the first day of the month, move the most recent weekly to monthly | |
if [ "$dayofmonth" = "01" ]; then | |
lastweekly=`ls -l $budest/weekly/ | grep -o '[0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\}-[0-9]\{2\}-[0-9]\{2\}$' | tail -n 1` | |
if [ -d $budest/weekly/$lastweekly ]; then | |
mv $budest/weekly/$lastweekly $budest/monthly/ | |
fi | |
fi | |
# DELETING... | |
# remove oldest daily (if more than $keepdaily) | |
dailycount=`ls -l $budest/daily/ | grep -o '[0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\}-[0-9]\{2\}-[0-9]\{2\}$' | wc -l` | |
if [ "$dailycount" -gt "$keepdaily" ]; then | |
oldestdaily=`ls -l $budest/daily/ | grep -o '[0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\}-[0-9]\{2\}-[0-9]\{2\}$' | head -n 1` | |
rm -rf /$budest/daily/$oldestdaily | |
fi | |
# remove oldest weekly (if more than $keepweekly) | |
weeklycount=`ls -l $budest/weekly/ | grep -o '[0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\}-[0-9]\{2\}-[0-9]\{2\}$' | wc -l` | |
if [ "$weeklycount" -gt "$keepweekly" ]; then | |
oldestweekly=`ls -l $budest/daily/ | grep -o '[0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\}-[0-9]\{2\}-[0-9]\{2\}$' | head -n 1` | |
rm -rf /$budest/weekly/$oldestweekly | |
fi | |
fi | |
intervalcount=`ls -l $budest/interval/ | grep -o '[0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\}-[0-9]\{2\}-[0-9]\{2\}$' | wc -l` | |
# remove oldest interval (if more than $keepinterval) | |
if [ "$intervalcount" -gt "$keepinterval" ]; then | |
oldestinterval=`ls -l $budest/interval/ | grep -o '[0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\}-[0-9]\{2\}-[0-9]\{2\}$' | head -n 1` | |
rm -rf /$budest/interval/$oldestinterval | |
fi | |
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 | |
rm -rf /testbackups/* | |
rm -rf /testsource/* | |
for month in `seq 1 3`; do | |
dim=`date -d "2006/$month/1 + 1 month - 1 day" +%d` | |
echo "DIM: $dim" | |
for day in `seq 1 $dim`; do | |
touch /testsource/$day.test | |
for hour in `seq 0 23`; do | |
for minute in `seq 0 59`; do | |
if [ `expr $minute % 15` = "0" ]; then | |
echo "testing day-hour-minute: $day-$hour-$minute" | |
/BPConnect/bin/bpcbackup "2006-$month-${day}T$hour:$minute:00-0600" | |
fi | |
done | |
done | |
done | |
done | |
echo "RESULTS" | |
echo "Interval: Count ($(ls -l /testbackups/interval | wc -l))" | |
ls /testbackups/interval | |
echo "Daily: Count ($(ls -l /testbackups/daily | wc -l))" | |
ls /testbackups/daily/ | |
echo "Weekly: Count ($(ls -l /testbackups/weekly | wc -l))" | |
ls /testbackups/weekly | |
echo "Monthly: Count ($(ls -l /testbackups/monthly | wc -l))" | |
ls /testbackups/monthly |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment