Success, failure of the tar command recorded with time and date stamp

I needed to determine the amount of time it would take to archive large amount of data on my unix server. If I had infinite amount of free time I could sit in front of the screen with a stop watch. But the better way is to create a script that would record the start time, stop time, success and failures. Below is the script I created in the bash shell.

#!/bin/sh
# This script archive directories and create a log file with the time and date stamp.
# Andrew Lin
# www.GamesCheat.ca
# 27 January, 2010

cd /backup/archive
echo “Backup start: ” > backup.log 2>&1
date >> backup.log 2>&1

cd /export/home

# tar cvf /backup/archive/backup.tar dir1 dir2 >> /backup/archive/backup.log 2>&1

tar cvf /backup/archive/backup.tar dir1 dir2 2>>/backup/archive/backup.log

echo “Backup Stop: ” >> /backup/archive/backup.log 2>&1
date >> /backup/archive/backup.log 2>&1

#!/bin/sh
This indicates that the script is written to be executed in the Bash shell.

The # sign indicates the line is a comment.

cd /backup/archive
Change the path /backup/archive to where this script is located on your machine.

echo “Backup start: ” > backup.log 2>&1
Create a file backup.log. The text Backup start: is inserted into this file. The file will be created in the directory
/backup/archive.

date >> backup.log 2>&1
The date will be appended into the file backup.log. This is useful if you wanted to determine the time of execution of the script.

cd /export/home
Go to the directory /export/home. This directory contains the files or directories you wish to backup.

# tar cvf /backup/archive/backup.tar dir1 dir2 >> /backup/archive/backup.log 2>&1
This line is commented out with the # sign. Uncomment this line if you want a detailed log of the tar command. Success and failure will be recorded in the log. Depending on the amount of files being archived this log could become very long.

tar cvf /backup/archive/backup.tar dir1 dir2 2>>/backup/archive/backup.log
This command will copy the directories dir1 and dir2. The copies will be stored in /backup/archive/backup.tar. Any error will be recorded in the file backup.log. If you wanted both error and success recorded then comment this line out and uncomment the line above it.

echo “Backup Stop: ” >> /backup/archive/backup.log 2>&1
After the completion of the tar command, insert the line Backup Stop: into the file backup.log.

date >> /backup/archive/backup.log 2>&1
Insert the date and time into the file backup.log. This is useful for determining the completion time/date of the script.

Save this file with the name of backup-script. Make sure that you make the file executable, with the chmod command, e.g.
chmod 777 backup-script.log