Linux backup




written by Marco Ferretti on March 09, 2012, at 09:12 AM

Today I would like to share an old backup script that has been serving me for three years. It is not entirely the product of my brain : I have modified a script that I found on the internet that was created back in the year 2000.

The script is pretty well commented and self explanatory : it creates a full backup on Sundays and an incremental backup the other days. Every beginning of month it creates a full backup.

#!/bin/bash
# full and incremental backup script
# created 07 February 2000
# Based on a script by Daniel O'Callaghan <danny@freebsd.org>
# and modified by Gerhard Mourani <gmourani@videotron.ca>
# modified by Marco Ferretti <marco.ferretti@gmail.com> on 16 Aug 2008

#Change the 5 variables below to fit your computer/backup

#COMPUTER=server				                                                                # name of this computer
PROJECTS="/home/marco/Projects/Workspaces /home/marco/Projects/Nitro /home/marco/Projects/Readytec"             # projects directory
CVS="/home/marco/dev /home/cvs"                                                                                 # CVS directory
DOCUMENTS="/etc /home/marco/Documents"                                                                          # documents directory
MAIL="/home/marco/.mozilla-thunderbird"				                                                # email
USERS="/home/bruno "                           	                                                                # users to fully backup
DIRECTORIES="$DOCUMENTS $USERS $PROJECTS $MAIL $CVS"                                                            # directoris that will be passed to tar
BACKUPDIR=/media/backup-disk/backup/archives	                                                                # where to store the backups
TIMEDIR=/media/backup-disk/backup/log		                                                                # where to store time of full backup
TMPFILE=/tmp/backup.local.tmp
TAR=/bin/tar					                                                                # name and locaction of tar
LOGFILE=/media/backup-disk/backup/log/backup.log                                                                # log file 

#You should not have to change anything below here
PATH=/usr/local/bin:/usr/bin:/bin
DOW=`date +%a`              		                                                                        # Day of the week e.g. Mon
DOM=`date +%d`              		                                                                        # Date of the Month e.g. 27
DM=`date +%d%b`             		                                                                        # Date and Month e.g. 27Sep

# On the 1st of the month a permanet full backup is made
# Every Sunday a full backup is made - overwriting last Sundays backup
# The rest of the time an incremental backup is made. Each incremental
# backup overwrites last weeks incremental backup of the same name.
#
# if NEWER = "", then tar backs up all files in the directories
# otherwise it backs up files newer than the NEWER date. NEWER
# gets it date from the file written every Sunday.


echo "`date` Starting backup backup script" > $LOGFILE

# Monthly full backup
if [ $DOM = "01" ]; then
	echo "`date +%d-%b-%y` Starting montly full backup" >> $LOGFILE
        NEWER=""
#        $TAR $NEWER -cfj $BACKUPDIR/$COMPUTER-$DM.tar.bz2 $DIRECTORIES
#	$TAR $NEWER -cjf $TMPFILE $DIRECTORIES
#Version that creates one big file
#        $TAR $NEWER -cjf $BACKUPDIR/monthly/$DM-full.tar.bz2 $DIRECTORIES 
#Version that splits the file in 4Gb parts (DVD burn ready
	$TAR $NEWER cjf - $DIRECTORIES | split --bytes=4G -d - $BACKUPDIR/monthly/$DM-full.tar.bz2_
        #Remove the big file ...
	rm BACKUPDIR/monthly/$DM-full.tar.bz2
#	cp $TMPFILE $BACKUPDIR/monthly/$DM-full.tar.bz2
fi

# Weekly full backup
if [ $DOW = "Sun" ]; then
	echo "`date +%d-%b-%y` Starting weekly full backup" >> $LOGFILE
        NEWER=""
        NOW=`date +%d-%b`

        # Update full backup date
#        echo $NOW > $TIMEDIR/$COMPUTER-full-date
#        $TAR $NEWER -cjf $BACKUPDIR/$COMPUTER-$DOW.tar $DIRECTORIES
        echo $NOW > $TIMEDIR/full-date
        $TAR $NEWER -cjf $BACKUPDIR/weekly/$DOW-full.tar.bz2 $DIRECTORIES
#        $TAR $NEWER -cjf $TMPFILE $DIRECTORIES        
#	cp $TMPFILE $BACKUPDIR/weekly/$DOW-full.tar.bz2        

# Make incremental backup - overwrite last weeks
else
        # Get date of last full backup
#        NEWER="--newer `cat $TIMEDIR/$COMPUTER-full-date`"
#        $TAR $NEWER -cf $BACKUPDIR/$COMPUTER-$DOW.tar $DIRECTORIES
	echo "`date +%d-%b-%y` Starting incremental backup" >> $LOGFILE
        NEWER="--newer `cat $TIMEDIR/full-date`"
        $TAR $NEWER -cjf $BACKUPDIR/weekly/$DOW.tar.bz2 $DIRECTORIES
#        $TAR $NEWER -cjf $TMPFILE $DIRECTORIES        
#	cp $TMPFILE $BACKUPDIR/weekly/$DOW.tar.bz2                
fi
echo "`date` end of backup backup script" >> $LOGFILE

Leave a comment

Name (required)
E-mail (required, will not be published)
Website
Comment

Enter value: Captcha