HowTo Make & Schedule Daily & Weekly Backup Scripts
General Overview
| Daily Scripts | Weekly Scripts | mySQL Script | Scheduling Backups |
| Back To Help Files Index | Back To www.dweimer.org |
Weekly Scripts
Overview:
These scripts are essentially the same as the daily scripts.
The Work:
The only changes to the first script is the changing of every occurance of the word daily to weekly.
#!/bin/sh
#
# Format Mail Header To Allow Piped Output Mailed To User
echo "From root@MyDomain.Org"
echo "From: Weekly Backup"
echo "To: root@MyDomain.Org"
echo "Subject: Weekly BackUp Log"
echo "MIME-Version: 1.0"
echo "Content-Type: TEXT/PLAIN; charset=US-ASCII"
echo ""
# Begin Body of Mail Message, and begin to do the work
echo "Backup Job Started....";date
# Set PATH Variable For Shell Script, and export it to subsequent
# called scripts.
PATH=/usr/bin:/usr/local/bin:/bin ; export PATH
# Set Script Variables, exporting some to subsequent called scripts.
DIR=/data2/backups ; export DIR # Change this to the directory you want to
# send your scripts to.
DIR2=/data2/backups/scripts ; export DIR2 # Change this to the directory
# where you are keeping the
# scripts.
TAR=bk.weekly.tar ; export TAR
BZ2=$TAR.bz2
CMDC="tar -cvf" ; export CMDC
CMD="tar -rvf" ; export CMD
# The count variable tells the script how many old backups to save.
# This must be 3 or greater for this script file to work.
COUNT=3
# The following derivations are necessary to adjust the case
# statement later to account for different values of the count
# variable.
CASE1=$COUNT
CASE2=$(($CASE1-1))
# Check For Directory to write backup files to. If it doesn't exist,
# create it.
echo ""
echo "Created Directories...."
if [ -d $DIR ]
then
else
mkdir $DIR
chmod 0760 $DIR
fi
# Delete Oldest Weekly If Exists.
# And Rename Susbiquent Weeklies to Older Weeklies.
echo ""
echo "Removed Files...."
until [ $COUNT -eq 0 ]
do
case "$COUNT" in
$CASE1)
if [ -e $DIR/$BZ2.old$COUNT ]
then
rm -v $DIR/$BZ2.old$COUNT
fi
;;
$CASE2)
echo ""
echo "Renamed Files...."
until [ $COUNT -lt 1 ]
do
if [ -e $DIR/$BZ2.old$COUNT ]
then
mv -v $DIR/$BZ2.old$COUNT $DIR/$BZ2.old$(($COUNT+1))
fi
COUNT=$(($COUNT-1))
done
COUNT=$(($COUNT+2))
;;
1)
if [ -e $DIR/$BZ2 ]
then
mv -v $DIR/$BZ2 $DIR/$BZ2.old$COUNT
fi
;;
esac
COUNT=$(($COUNT-1))
done
# Get Files For Archive and Archive
echo ""
echo "Creating Archive File and Adding Files To Archive File....";date
# Execute the script file that contains the list of files to backup.
/$DIR2/weekly.bk
# BZip2 The Newly Created Archive
echo ""
echo "Compressing $DIR/$TAR With BZip2....";date
# Use the bzip2 compression algorithm to compress your backup file.
bzip2 -8 $DIR/$TAR
# Stamp the end of the Log with the completion time.
echo ""
echo "Archive Completed";date
The second script file like on the daily script file uses exported variables from the first script, the bigest difference here is the inclussion of the second script from the daily backup. Then it includes more files that are not in the daily backups. Now it is not only nessecary to call this script from the first file, but nessecary to call the second script from the daily backup before backing up any of the new weekly files. This is due to the fact that we only want one file, and in order to accomplish that, the daily script first creates the file, and the weekly continues to append the new files to that archive.
#!/bin/sh # # Imported Variables # CMD is the command to add a new file or group of files to an existing # tar file. # DIR is the directory where your backup files are being saved. # TAR is the name of the tar file to create (this file will be removed # after it has been compressed). # DIR2 is the location of the backup script files. # Get The Daily Script Backup Files First. $DIR2/daily.bk # Files To Archive Weekly In Addition To Daily $CMD $DIR/$TAR /etc $CMD $DIR/$TAR /sys/i386/conf/FreeBSD-SMP $CMD $DIR/$TAR /usr/local/etc $CMD $DIR/$TAR /www_root $CMD $DIR/$TAR /usr/home