Created
July 15, 2011 20:35
-
-
Save justincjahn/1085503 to your computer and use it in GitHub Desktop.
Simple Incremental Backups using rsync.
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 | |
unset PATH | |
# | |
# VARIABLES | |
# | |
BACKUP_DIR='/path/to/backup/location' # The backup directory | |
SOURCE_LOC=('/path/to/backup1' '/path/to/files/to/backup') # The source backup | |
SOURCE_DIR=('backup1' 'backup2') # The directory name in backup for SOURCE_LOC | |
EXCLUDE=() # Directories to exclude | |
# | |
# BINARY VARIABLES | |
# | |
RSYNC=/usr/bin/rsync | |
CP=/bin/cp | |
MKDIR=/bin/mkdir | |
LOGGER=/usr/bin/logger | |
# | |
# DATE VARIABLES | |
# | |
YEAR=`/bin/date +%Y` | |
MONTH=`/bin/date +%m` | |
DAY=`/bin/date +%d` | |
YESTERDAY=`/bin/date -d '-1 day' +%d` | |
# Make sure that the year folder exists | |
if [ ! -d $BACKUP_DIR/$YEAR ]; then | |
$MKDIR $BACKUP_DIR/$YEAR | |
fi | |
# Make sure that the month folder exists | |
if [ ! -d $BACKUP_DIR/$YEAR/$MONTH ]; then | |
$MKDIR $BACKUP_DIR/$YEAR/$MONTH | |
fi | |
# Make sure that the day folder exists | |
if [ ! -d $BACKUP_DIR/$YEAR/$MONTH/$DAY ]; then | |
$MKDIR $BACKUP_DIR/$YEAR/$MONTH/$DAY | |
fi | |
# Make the exclude list | |
EXCLUDE_STRING="" | |
for ((i=0;i<${#EXCLUDE[*]};i++)); do | |
EXCLUDE_STRING="${EXCLUDE_STRING} --exclude '${EXCLUDE[${i}]}'" | |
done | |
# If it is the first day of the month, create a full backup. | |
if [ $DAY -eq 1 ]; then | |
# Loop though each directory to backup | |
for ((i=0;i<${#SOURCE_LOC[*]};i++)); do | |
# Make sure the backup directory is created | |
if [ ! -d $BACKUP_DIR/$YEAR/$MONTH/$DAY/${SOURCE_DIR[${i}]} ]; then | |
$MKDIR $BACKUP_DIR/$YEAR/$MONTH/$DAY/${SOURCE_DIR[${i}]} | |
fi | |
# RSYNC | |
$RSYNC -a $EXCLUDE_STRING ${SOURCE_LOC[${i}]}/ $BACKUP_DIR/$YEAR/$MONTH/$DAY/${SOURCE_DIR[${i}]}/ | |
# Log that we've done the backup | |
$LOGGER -p daemon.info [BACKUP] Performed full backup of ${SOURCE_DIR[${i}]}. | |
done | |
else | |
# INCREMENTAL | |
# Loop though each directory to backup | |
for ((i=0;i<${#SOURCE_LOC[*]};i++)); do | |
# Create a copy with hard links | |
$CP -al $BACKUP_DIR/$YEAR/$MONTH/$YESTERDAY/${SOURCE_DIR[${i}]} $BACKUP_DIR/$YEAR/$MONTH/$DAY/${SOURCE_DIR[${i}]} | |
# RSYNC | |
$RSYNC -aHx --numeric-ids --delete $EXCLUDE_STRING --delete-excluded \ | |
${SOURCE_LOC[${i}]}/ $BACKUP_DIR/$YEAR/$MONTH/$DAY/${SOURCE_DIR[${i}]}/ | |
# Log that we've done the backup | |
$LOGGER -p daemon.info [BACKUP] Performed incremental backup of ${SOURCE_DIR[${i}]}. | |
done | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment