#!/bin/bash #backup.sh -- bash backup shell script # # by sp1r1t : tim sp1r1t de # do with this script whatever you want, but don't blame me (BSD license) # # history # # 2005-02-03 spirit : - changed .gz to .bz2 # - take lost+count directory into account when deleting files # - adapted wrong comments / maxbackup numbers # - fixed $PATH issue # to run this script from cron under the root account : # # as root, use "crontab -e" to edit your crontab. format is as follows : # # # minute(0-59) hour(0-23) day of month(1-31) month(1-12 or Jan-Dec) day of week(0-6 or Sun-Sat) command # # 0 22 * * * /root/backup.sh # # the last line expects this script to be located at /root/backup.sh and executes it at 10PM on every day. ######################################## # settings - adapt these to your needs # ######################################## # the directories this script should make backups of backupdirs="/etc /root /home" # path were the logfiles this script produces should be kept logpath="/backup/logs" # the path were the backups should be stored backuptarget="/backup" # maximal number of old backups to keep in the backup directory # if more backups (not single .tar files) are in there, the oldest ones will be deleted # example : if you backup 3 directories per day and set maxbyckups to 30, # backups for 10 days (3 x 10 .tar.bz2-files) will be kept maxbackups=60 ################################# # prepare files and directories # ################################# # make sure that PATH is set cause we need external programs from those dirs export PATH=/bin:/usr/bin:$PATH # get date in appropriate format to construct file names today=`date +"%Y-%m-%d"` # make sure the directories we need are present if [ ! -d $logpath ] then echo "backup.sh WARNING : $logpath does not exist, creating it" mkdir -p $logpath chmod 0700 $logpath fi # create logfile for today touch $logpath/$today.log # we are really lazy logfile="$logpath/$today.log" # check if logfile is there if [ ! -e $logfile ] then echo "backup.sh WARNING : could not create logfile at $logfile" fi ############################### # deal with old backups files # ############################### cd $backuptarget # count old backup files fcount=`echo *.tar.bz2 | wc -w | awk '{print $1}'` echo "$fcount old backup file(s) present" # rm the oldest files if $maxbackups is exceeded # example : if $maxbackups is 10 and there are 13 files, delete the oldest 3 of them if [ $fcount -gt $maxbackups ] then # get number of files that should be erased deathcount=$(expr $fcount - $maxbackups) echo "deathcount : $deathcount" if [ $deathcount -lt 0 ] then $deathcount=0 fi echo "corrected deathcount : $deathcount" deadfiles=`ls -Ac | tail -n $deathcount` for dfile in $deadfiles do if [ ! $dfile eq "lost+found" ] then rm $dfile echo "$dfile was deleted" fi done fi ########################### # deal with old log files # ########################### # old log files are simply left alone # rename existing backup files of today if they are already present # ( this should only be the case if this script is run more than once daily ) cd $logpath # make sure we start a fresh logfile if [ -f $today.log ] then # get current time in appropriate format to construct file names secs=`date +"%s"` # blare out echo "backup file with current date exists, renaming it to $secs" #| wall # rename old file to a unique name mv $today.log $secs.log fi ###################################### # prepare and perform backup process # ###################################### # what's a logfile without some info? cur_time=`date` echo "this file was created by the backup script $0" >>$logfile echo " " >>$logfile echo "starting backup at $cur_time" >>$logfile # something might be wrong if this doesn't exist if [ ! -d $backuptarget ] then echo "backup.sh WARNING : $backuptarget does not exist, creating it at $backuptarget" >>$logfile mkdir -p $backuptarget chmod 0700 $backuptarget fi # what about finally doing what we came for? for path in $backupdirs do echo "starting backup of $path..." >>$logfile $path=`echo $path | sed 's/\//-/g'` tar cjvf $backuptarget/$path-$today.tar.bz2 $path >>$logfile sleep 1 echo "backup of $path finished" >>$logfile done # let's get a little verbose again echo "...backup complete, status: $?" >>$logfile echo "verifying backups..." >>$logfile # fullfilling our promise : check the files you just produced, tar! for path in $backupdirs do echo "verifying $path...." >>$logfile tar tjvf $backuptarget/$path-$today.tar.bz2 >>$logfile && echo "$path verified, looks good" >>$logfile if [ $? -eq 0 ] then sleep 1 else echo "WARNING : errors occured in $path" >>$logfile fi done #end of script