rsync backup - incremental backup script
State: active
The script is used to make LOCAL incremental backups of one directory. Note, in this version only LOCAL2LOCAL is supported!
In the versions I will implemenet are max. age of backups and exclusive ignore patterns as well as the possibility to run remotly.
Download the current version now
#!/bin/bash
###############################################################################
### Release notes
###############################################################################
# Title: rsync incremental backup script (LOCAL)
# version: 2008-07-27
# License: GnuGPL v2
#
# Description: The script is used to make LOCAL incremental backups of one
# directory. Note, in this version only LOCAL2LOCAL is supported!
#
# Author: Per Lasse Baasch (c)
# Website: http://www.skycube.net
#
# Requirements: rsync
#
# Sample Usage: ./rsync_backup_v2.sh /home/sampleuser /media/usbbackup/backup
#
# ToDo: - Remote RUN
# - LifeTime (Timeout where old backups automaticly will be deleted)
# - Exclude files and folders
#
###############################################################################
### Configuration
###############################################################################
# Source directory MUST ALLREADY EXIST
DIRA="$1"
# Destination directory MUST ALLREADY EXIST
DIRB="$2"
###############################################################################
### Don't change anything below this line if you don't have errors!
###############################################################################
# Path to rsync
RSYNC=/usr/bin/rsync
# File for the logs
LOGFILE="rsynctransfer.log"
# File to save name of last backup
CURRENTSTATEFILE="currentstate.txt"
# START the timer
TIMER1=`date +%s`
# Check if the currentstate exist else, create it
TESTCURRENTFILE=`ls $DIRB/ | grep $CURRENTSTATEFILE`
if [ "$TESTCURRENTFILE" != "$CURRENTSTATEFILE" ]
then
tty=`echo empty > $DIRB/$CURRENTSTATEFILE`
mkdir $DIRB/empty
tty=`echo nolog > $DIRB/empty/$LOGFILE`
fi
# Get the old backup directory
OLDBACKUPDIR=`cat $DIRB/$CURRENTSTATEFILE`
# Set the new backup directory
NEWBACKUPDIR=`date +%F_%H-%M-%S`
# Temporary directory on destination
TMP=$DIRB/TMP
# rsync options
# -o // preserve owner (root only)
# -g // preserve group
# -p // preserve permissions
# -t // preserve times
# -r // recurse into directories
# -l // copy symlinks as symlinks
# -c // always checksum
# -v // increase verbosity
# --delete // delete files that don't exist on sender
# --force // force deletion of dirs even if not empty
# --backup // make backups
# --backup-dir // make backups into this directory
# --ignore-errors // delete even if there are I/O errors
OPTIONS="-ogptrlcv
--delete
--force
--backup
--backup-dir=$TMP
--ignore-errors"
# --exclude=vms/"
# Create TMP directory for deleted files
mkdir $TMP
# Create new logfile
tty=`echo Start of rsynctransfer logfile > $DIRB/$LOGFILE`
# Execute the rsync
# sync the old backup state with the new state
# and backup deleted files to the TMP directory
tty=`echo -------------------------------------------- >> $DIRB/$LOGFILE`
tty=`echo STARTING BACKUP WITH THE NAME $NEWBACKUPDIR >> $DIRB/$LOGFILE`
$RSYNC $OPTIONS $DIRA $DIRB/$OLDBACKUPDIR >> $DIRB/$LOGFILE
tty=`echo END OF BACKUP WITH THE NAME $NEWBACKUPDIR >> $DIRB/$LOGFILE`
# Rename old backup directory to new backup directory
mv $DIRB/$OLDBACKUPDIR $DIRB/$NEWBACKUPDIR
# Rename TMP directory to oldbackup
mv $TMP $DIRB/$OLDBACKUPDIR
# Move logfiles to the correct directories
mv $DIRB/$NEWBACKUPDIR/$LOGFILE $DIRB/$OLDBACKUPDIR/
mv $DIRB/$LOGFILE $DIRB/$NEWBACKUPDIR/
# Write file with the name of current state
tty=`echo $NEWBACKUPDIR > $DIRB/$CURRENTSTATEFILE`
# Delete the directory "empty" after FIRST run
EMPTY=`ls $DIRB/ | grep empty`
if [ "$EMPTY" = "empty" ]
then
rm -fr $DIRB/empty
fi
# STOP the timer
TIMER2=`date +%s`
# Calculate timer result
let TIMER=$TIMER2-$TIMER1
# Get human readable result, print it and save it to the log
HOURS='0'
MINUTES='0'
while [ $TIMER -gt 59 ]; do
if [ $TIMER -ge 3600 ]; then
let HOURS=HOURS+1; let TIMER=TIMER-3600;
else
let MINUTES=MINUTES+1; let TIMER=TIMER-60;
fi
done
TIMERRESULT="DURATION OF BACKUP: $HOURS hours $MINUTES minutes $TIMER seconds"
echo $TIMERRESULT
tty=`echo $TIMERRESULT >> $DIRB/$NEWBACKUPDIR/$LOGFILE`