Management Of Backups With DAT Devices - Page 2
5 Dump the files to tape.
Once synchronized the servers on the local disk of the server connected to the DAT, we have to copy the data we are interested in, to a tape.
This guide aims to copy all the data on each copy. Possible other solution would be an incremental backup where, each day, we will copy only those files that have changed but I will not mention about that case here.
5.1 Periodicity
The proposal is to make a dump every day. This task will be automated using cron and we would have to put the tape in the DAT device once a day. The dump data to tape should be done after the servers synchronization, which, following this model, should be done once a day prior to dump data to tape.
In addition we will also copy a tape as monthly backup.
In short, we will have 5 + 12 tapes in a year, one for each working day of the week and one per month. The monthly tape will be on the first Monday of each month.
The process to run a cron on the first Monday of the month is not supported directly but is obtained as follows:
crontab -eAdd the following line:
50 10 1-7 * * [ "$(date +\%a)" == "Mon" ] && /usr/local/bin/monthly.sh
This will launches the first Monday of each month, the script /usr/local/bin/monthly.sh. In the example of this guide, the only thing different that we will do the first Monday of the month is to put the 'monthly' tape. However, the script monthly.sh contains a reminder message for the person responsible for putting the tape which is sent via cron.
#!/bin/bash echo "Hi!" echo echo "If I have been scripted well, today is the first Monday of the month" echo "and you must to put the monthly backup tape."
5.2 Script
The following script will copy the data to tape from the directory where the files are synchronized from the original server
#!/bin/bash # Dump files to tape # Variables # Base dir to copy SOURCE=/backups # Logs dir LOGS=/var/log/backup # Dir to copy DIRS="serverA/var/log serverA/var/lib/mysql serverA/home/httpd" # tape device TAPE=st0 # Date format FORMAT="[%Y/%m/%d %H:%M:%S]" # End Variables # Var for logs DAY=`date --date='1 day ago' +%a` MONTHDAY=`date --date='1 day ago' +%e` # First Monday of the month? if test "$DAY" = "Mon" -a $MONTHDAY -ge 1 -a $MONTHDAY -le 7; then DAY=`date +%b` fi date=`date "+$FORMAT"` echo "----------------------" >> $LOGS/$DAY.log echo "----------------------" >> $LOGS/$DAY.err echo "$date" >> $LOGS/$DAY.err echo "----------------------" >> $LOGS/$DAY.err echo "Message from Backup Server." echo echo "Below the summary of the nightly backup." echo echo "$date Start backup" |tee -a $LOGS/$DAY.log echo "$date Rewinding the tape..." |tee -a $LOGS/$DAY.log mt -f /dev/$TAPE rewind echo "$date Starting to copy" |tee -a $LOGS/$DAY.log for dir in $DIRS; do date=`date "+$FORMAT"` echo echo "$date Started: $SOURCE/$dir" |tee $LOGS/$dir.$DAY.log tar -vzcf /dev/n$TAPE -C $SOURCE $dir >> $LOGS/$dir.$DAY.log 2>>$LOGS/$DAY.err wait $! date=`date "+$FORMAT"` echo "$date Completed: $SOURCE/$dir" |tee -a $LOGS/$dir.$DAY.log echo done echo sleep 10 echo "$date Ejecting the tape..." |tee -a $LOGS/$DAY.log mt -f /dev/$TAPE rewoffl date=`date "+$FORMAT"` echo "$date End backup" |tee -a $LOGS/$DAY.log
A little analysis to understand what the script does:
SOURCE=/backups
The servers are synchronized in this directory (see section "3 Synchronize the servers")
LOGS=/var/log/backup
This is the directory where the script save a log about the operation.
DIRS="serverA/var/log serverA/var/lib/mysql serverA/home/httpd"
Within the directory defined in SOURCE, we choose what directories to save on tape. Each entry is separated by a space and being dumped to tape will create a brand (see Restoring files).
TAPE=st0
The tape device. In /dev there are two devices st0 and nst0. The first one, automatically rewind after each action. the second, ask for an explicit rewind. We wil use both of them that's why we reference the device in that way.
DAY=`date --date='1 day ago' +%a`
MONTHDAY=`date --date='1 day ago' +%e`
Variables used for "extracting" the backup log's names, for example.
tar -vzcf /dev/n$TAPE -C $SOURCE $dir >> $LOGS/$dir.$DAY.log 2>>$LOGS/$DAY.err
Is inside the loop which, for each directory, copy its contents to the tape in tar format and compressed (-z) in gzip format.
6 Restoring files
Restoring files is as follows:
- Enter the tape containing the files you want to restore in the DAT device.
-
go at the point (record) of the tape containing the file you need. The tape is divided into as many points (records) as directories specified in the DIRS entry into the script.
In the script the variable is:
DIRS="serverA/var/log serverA/var/lib/mysql serverA/home/httpd"
In this case, we have 3 records:- serverA/var/log
- serverA/var/lib/mysql
- serverA/home/httpd
mt -f /dev/st0 rewind
To access a file that is under the second directory:
mt -f /dev/nst0 fsf 1
Etc. -
Now, with the command:
tar xvf /dev/nst0 serverA/var/log/[filename]
for restoring a file, or
tar xvf /dev/nst0 serverA/var/log
for restoring all the directory log.
7 Summaries commands to use a tape device.
This small list shows only the most common options; however, it is highly recommended that you go through man pages of mt and tar command for more options/information. To use the following comands, you have to be root. Otherwise, you can use them with sudo.
Rewind tape drive:
mt -f /dev/st0 rewind
Backup directory /www and /home with tar command (z - compressed):
tar -czf /dev/st0 /www /home
Find out what block you are at with mt command:
mt -f /dev/st0 tell
Display list of files on tape drive:
tar -tzf /dev/st0
Restore /www directory:
cd /
mt -f /dev/st0 rewind
tar -xzf /dev/st0 www
Unload the tape:
mt -f /dev/st0 offline
Display status information about the tape unit:
mt -f /dev/st0 status
Erase the tape:
mt -f /dev/st0 erase
Goto previous record:
mt -f /dev/nst0 bsfm 1
Forward record:
mt -f /dev/nst0 fsf 1
Go to end of data:
mt -f /dev/nst0 eod
8 Conclusion
As always in this case, I can NOT assure that it works for everyone. I CAN assure, it works exactly in this way with my Ubuntu box for 16 months, now, without any big problems.
As I said at start, please, consider a starting guide to use a tape backup system.