Это старая версия документа.


Ротация архивов

Shell сценарий в разделе Сценарии Shell позволяет создавать только 7 различных архивов. Для сервера, данные на котором меняются нечасто, этого может быть достаточно. Если же сервер содержит большой объем данных, требуется использовать более комплексную схему ротации архивов.

Ротация NFS архивов

В этой секции наш shell сценарий будет немного модифицирован с целью осуществления схемы ротации 'дед-отец-сын' (ежемесячно-еженедельно-ежедневно):

  1. ротация будет выполнять ежедневное резервное копирование с воскресенья по пятницу.

  2. в субботу будет еженедельное копирование, обеспечивая четыре недельных архива в месяц.

  3. ежемесячное копирование выполняется в первый день месяца, обеспечивая ротацию двух ежемесячных архивов, на основе четности месяца.

Здесь приведен сценарий:

#!/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
        month_file="$hostname-month1.tgz"
fi

# 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 Executing the Script.

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.

Устройства на магнитной ленте

A tape drive attached to the server can be used instead of an NFS share. Using a tape drive simplifies archive rotation, and makes taking the media off-site easier as well.

When using a tape drive, the filename portions of the script aren't needed because the data is sent directly to the tape device. Some commands to manipulate the tape are needed. This is accomplished using mt, a magnetic tape control utility part of the cpio package.

Here is the shell script modified to use a tape drive:

#!/bin/bash #################################### # # Backup to tape drive script. # ####################################

# What to backup. backup_files=«/home /var/spool/mail /etc /root /boot /opt»

# Where to backup to. dest=«/dev/st0»

# Print start status message. echo «Backing up $backup_files to $dest» date echo

# Make sure the tape is rewound. mt -f $dest rewind

# Backup the files using tar. tar czf $dest $backup_files

# Rewind and eject the tape. mt -f $dest rewoffl

# Print end status message. echo echo «Backup finished» date

The default device name for a SCSI tape drive is /dev/st0. Use the appropriate device path for your system.

Restoring from a tape drive is basically the same as restoring from a file. Simply rewind the tape and use the device path instead of a file path. For example to restore the /etc/hosts file to /tmp/etc/hosts:

mt -f /dev/st0 rewind tar -xzf /dev/st0 -C /tmp etc/hosts