Return to Snippet

Revision: 58240
at July 4, 2012 01:00 by _reydin_


Initial Code
#!/bin/bash
#This scrip is used for backing up SPCSERV. It will make a full backup and six incremental
#backups. This adds the date to the begining of the file's name. To recover data from the backup
#just go directly to the date you want to recover from and copy that file to another location.

## Make the following entry into the crontab -e. The only part the will need to be changed is where the
# rsync.sh file resides. For this example the rsync.sh file is in the /srv/ directory.
# 0 1 * * * /srv/rsync.sh 1>>/var/log/backup.log 2>>/var/log/backup.log


####### Backup Directory Variable ##########
bkdir=/mnt/intbackup/spcserv            #to save the backup somewhere else.

## DO NOT CHANGE THESE ENTRIES. Change the above bkdir entry to change the location of where the backup is saved.
tstamp=$(date +%Y-%m-%d_%H:%M:%S)       #Timestamp used for naming files.
bkdir0=${bkdir}/${tstamp}_backup.0/     #This starts the naming convention. It sets the name and directory for the current backup.
beforetstmp="$(date +%s)"               #Used to calculate the amount of time the backup process took.


#### DO NOT CHANGE THESE ENTRIES. ####
###### This section is used to move the backup files #######
# This gets the name for each backup so that it can be moved. Since the backups' name is constantly changing
# we need a way of getting the current name for each file.
bkup0=`ls $bkdir | grep backup.0`               #This gets the name of backup.0 so it can be moved to backup.1
bkup1=`ls $bkdir | grep backup.1`               #This gets the name of backup.1 so it can be moved to backup.2
bkup2=`ls $bkdir | grep backup.2`               #This gets the name of backup.2 so it can be moved to backup.3
bkup3=`ls $bkdir | grep backup.3`               #This gets the name of backup.3 so it can be moved to backup.4
bkup4=`ls $bkdir | grep backup.4`               #This gets the name of backup.4 so it can be moved to backup.5
bkup5=`ls $bkdir | grep backup.5`               #This gets the name of backup.5 so it can be moved to backup.6
bkup6=`ls $bkdir | grep backup.6`               #This gets the name of backup.6 so it can be moved to backup.tmp; which will
                                                #be moved to backup.0. We recycle this directory to reduce the amount of time
                                                #rsync has to run.

#### DO NOT CHANGE THESE ENTRIES. ####
## This sripts the ending digit so that file can be incremented to the next digit.
bkupStrp0=`echo ${bkup0} | awk -F'.' '{print $1}'`              #This removes the ending digit 0 from backup.0
bkupStrp1=`echo ${bkup1} | awk -F'.' '{print $1}'`              #This removes the ending digit 1 from backup.1
bkupStrp2=`echo ${bkup2} | awk -F'.' '{print $1}'`              #This removes the ending digit 2 from backup.2
bkupStrp3=`echo ${bkup3} | awk -F'.' '{print $1}'`              #This removes the ending digit 3 from backup.3
bkupStrp4=`echo ${bkup4} | awk -F'.' '{print $1}'`              #This removes the ending digit 4 from backup.4
bkupStrp5=`echo ${bkup5} | awk -F'.' '{print $1}'`              #This removes the ending digit 5 from backup.5
bkupStrp6=`echo ${bkup6} | awk -F'.' '{print $1}'`              #This removes the ending digit 6 from backup.6


#### DO NOT CHANGE THESE ENTRIES. ####
###### This section is where we start moving the file and renaming them. ######
mv ${bkdir}/${bkup6} ${bkdir}/backup.tmp                #This moves backup.6 to backup.tmp
mv ${bkdir}/${bkup5} ${bkdir}/${bkupStrp5}.6            #This moves backup.5 to backup.6
mv ${bkdir}/${bkup4} ${bkdir}/${bkupStrp4}.5            #This moves backup.4 to backup.5
mv ${bkdir}/${bkup3} ${bkdir}/${bkupStrp3}.4            #This moves backup.3 to backup.4
mv ${bkdir}/${bkup2} ${bkdir}/${bkupStrp2}.3            #This moves backup.2 to backup.3
mv ${bkdir}/${bkup1} ${bkdir}/${bkupStrp1}.2            #This moves backup.1 to backup.2
mv ${bkdir}/${bkup0} ${bkdir}/${bkupStrp0}.1            #This moves backup.0 to backup.1
mv ${bkdir}/backup.tmp ${bkdir0}                        #This moves backup.tmp to backup.0


###### This section is responsible for syncing all of the directories you want backed up to the backup directory. ########
## Edit this section if you want to add other directories to the backup. Here I am backing up 3 directories.
## When adding directories to this section do not use a trailing / after the directory's name.
## Example:  rsync -a --delete --link-dest=/srv/bkup/${bkupStrp0}.1 /srv/Share /etc /home ${bkdir0}
#rsync -a --delete --link-dest=${bkdir}/${bkupStrp0}.1 /etc /srv /home /var /ldaphome ${bkdir0}
rsync -a --delete --link-dest=${bkdir}/${bkupStrp0}.1 /etc /var /home /srv /ldaphome /ldap_data ${bkdir0}

# Calculate the time the whole backup process took
aftertstmp="$(date +%s)"
elapsed="$(expr $aftertstmp - $beforetstmp)"
hours=$(($elapsed / 3600))
elapsed=$(($elapsed - $hours * 3600))
minutes=$(($elapsed / 60))
seconds=$(($elapsed - $minutes * 60))

# echo "\n"
endtstamp=$(date +%Y-%m-%d_%H:%M:%S)       #Timestamp used for naming files.
##### Make an entry in the log file that the backup ran successfully #####
echo ${tstamp} Backup ran successfully. The backup process took: ${hours} hours ${minutes} minutes ${seconds} seconds to complete.
echo The backup ended at ${endtstamp}

Initial URL


Initial Description
This script will do a daily backup of directories. If the files has not changed then it creates a hard-link to the file. If the file has changed then it will copy the new file. The backup is not much larger than the directories being backed up.

Initial Title
Linux backup script using rsync and hard-links

Initial Tags
backup, linux

Initial Language
Bash