Free Hosting Online for WorkStations

< Previous | Contents | Next >

2.1. Rotating NFS Archives


In this section, the shell script will be slightly modified to implement a grandfather-father-son rotation scheme (monthly-weekly-daily):

• The rotation will do a daily backup Sunday through Friday.

• On Saturday a weekly backup is done giving you four weekly backups a month.

• The monthly backup is done on the first of the month rotating two monthly backups based on if the month is odd or even.


Here is the new script:


#!/bin/bash

####################################

#

# Backup to NFS mount script with

# grandfather-father-son rotation.

#

####################################


# What to backup.

backup_files="/home /var/spool/mail /etc /root /boot /opt"


# Where to backup to. dest="/mnt/backup"


# Setup variables for the archive filename. day=$(date +%A)

hostname=$(hostname -s)


# Find which week of the month 1-4 it is. day_num=$(date +%-d)

if (( $day_num <= 7 )); then week_file="$hostname-week1.tgz"

elif (( $day_num > 7 && $day_num <= 14 )); then week_file="$hostname-week2.tgz"

elif (( $day_num > 14 && $day_num <= 21 )); then week_file="$hostname-week3.tgz"

elif (( $day_num > 21 && $day_num < 32 )); then week_file="$hostname-week4.tgz"

fi



# Find if the Month is odd or even. month_num=$(date +%m)

month=$(expr $month_num % 2) if [ $month -eq 0 ]; then

month_file="$hostname-month2.tgz"

else


fi


month_file="$hostname-month1.tgz"


# Create archive filename. if [ $day_num == 1 ]; then archive_file=$month_file

elif [ $day != "Saturday" ]; then archive_file="$hostname-$day.tgz"

else archive_file=$week_file

fi


# Print start status message.

echo "Backing up $backup_files to $dest/$archive_file" date

echo


# Backup the files using tar.

tar czf $dest/$archive_file $backup_files


# Print end status message. echo

echo "Backup finished" date


# Long listing of files in $dest to check file sizes. ls -lh $dest/


The script can be executed using the same methods as in Section 1.2, “Executing the Script” [p. 325].


It is good practice to take backup media off-site in case of a disaster. In the shell script example the backup media is another server providing an NFS share. In all likelihood taking the NFS server to another location would not be practical. Depending upon connection speeds it may be an option to copy the archive file over a WAN link to a server in another location.


Another option is to copy the archive file to an external hard drive which can then be taken off-site. Since the price of external hard drives continue to decrease, it may be cost-effective to use two drives for each archive level. This would allow you to have one external drive attached to the backup server and one in another location.


Top OS Cloud Computing at OnWorks: