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


Сценарии Shell

Один из простейших вариантов резервного копирования системы - использование shell сценария. Например, сценарий может быть использован для настройки какие каталоги требуют резервного копирования и для передачи этих каталогов в качестве аргументов утилите tar, которая создает архивные файлы. Архивный файл может быть затем перемещен или скопирован в другое место. Архив также может быть создан на удаленной файловой системе, такой как NFS.

Утилита tar создает один архивный файл из множества файлов и каталогов. tar может также пропускать файлы через утилиты сжатия, уменьшая таким образом размер архивного файла.

Простой Shell сценарий

Следующий shell сценарий использует tar для создания архивного файла на удаленно смонтированной файловой системе. Имя архива определяется с помощью дополнительных утилит командной строки.

#!/bin/sh
####################################
#
# Backup to NFS mount script.
#
####################################

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

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

# Create archive filename.
day=$(date +%A)
hostname=$(hostname -s)
archive_file="$hostname-$day.tgz"

# 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
  1. $backup_files: переменная для перечисления какие каталоги вы желаете сохранять. Список может быть изменен под ваши требования.

  2. $day: переменная, содержащая день недели. Она используется для создания архивных файлов на каждый день недели, обеспечивая историю резервного копирования на семь дней. Существуют иные способы получения такого результата, включая использование утилиты date.

  $hostname: variable containing the short hostname of the system. Using the hostname in the archive filename gives you the option of placing daily archive files from multiple systems in the same directory.
  $archive_file: the full archive filename.
  $dest: destination of the archive file. The directory needs to be created and in this case mounted before executing the backup script. See Network File System (NFS) for details of using NFS.
  status messages: optional messages printed to the console using the echo utility.
  tar czf $dest/$archive_file $backup_files: the tar command used to create the archive file.
      c: creates an archive.
      z: filter the archive through the gzip utility compressing the archive.
      f: output to an archive file. Otherwise the tar output will be sent to STDOUT.
  ls -lh $dest: optional statement prints a -l long listing in -h human readable format of the destination directory. This is useful for a quick file size check of the archive file. This check should not replace testing the archive file.

This is a simple example of a backup shell script; however there are many options that can be included in such a script. See References for links to resources providing more in-depth shell scripting information.

Выполнение сценария

Выполнение сценария из терминала

The simplest way of executing the above backup script is to copy and paste the contents into a file. backup.sh for example. Then from a terminal prompt:

sudo bash backup.sh

This is a great way to test the script to make sure everything works as expected.

Выполнение с помощью cron

The cron utility can be used to automate the script execution. The cron daemon allows the execution of scripts, or commands, at a specified time and date.

cron is configured through entries in a crontab file. crontab files are separated into fields:

# m h dom mon dow command

  m: minute the command executes on, between 0 and 59.
  h: hour the command executes on, between 0 and 23.
  dom: day of month the command executes on.
  mon: the month the command executes on, between 1 and 12.
  dow: the day of the week the command executes on, between 0 and 7. Sunday may be specified by using 0 or 7, both values are valid.
  command: the command to execute.

To add or change entries in a crontab file the crontab -e command should be used. Also, the contents of a crontab file can be viewed using the crontab -l command.

To execute the backup.sh script listed above using cron. Enter the following from a terminal prompt:

sudo crontab -e

Using sudo with the crontab -e command edits the root user's crontab. This is necessary if you are backing up directories only the root user has access to.

Add the following entry to the crontab file:

# m h dom mon dow command 0 0 * * * bash /usr/local/bin/backup.sh

The backup.sh script will now be executed every day at 12:00 am.

The backup.sh script will need to be copied to the /usr/local/bin/ directory in order for this entry to execute properly. The script can reside anywhere on the file system, simply change the script path appropriately.

For more in-depth crontab options see References.

Восстановление из архива

Once an archive has been created it is important to test the archive. The archive can be tested by listing the files it contains, but the best test is to restore a file from the archive.

  To see a listing of the archive contents. From a terminal prompt type:
  tar -tzvf /mnt/backup/host-Monday.tgz
  To restore a file from the archive to a different directory enter:
  tar -xzvf /mnt/backup/host-Monday.tgz -C /tmp etc/hosts
  The -C option to tar redirects the extracted files to the specified directory. The above example will extract the /etc/hosts file to /tmp/etc/hosts. tar recreates the directory structure that it contains.
  Also, notice the leading "/" is left off the path of the file to restore.
  To restore all files in the archive enter the following:
  cd /
  sudo tar -xzvf /mnt/backup/host-Monday.tgz

This will overwrite the files currently on the file system.

Сылки

  For more information on shell scripting see the Advanced Bash-Scripting Guide
  The book Teach Yourself Shell Programming in 24 Hours is available online and a great resource for shell scripting.
  The CronHowto Wiki Page contains details on advanced cron options.
  See the GNU tar Manual for more tar options.
  The Wikipedia Backup Rotation Scheme article contains information on other backup rotation schemes.
  The shell script uses tar to create the archive, but there many other command line utilities that can be used. For example:
      cpio: used to copy files to and from archives.
      dd: part of the coreutils package. A low level utility that can copy data from one format to another.
      rsnapshot: a file system snapshot utility used to create copies of an entire file system.
      rsync: a flexible utility used to create incremental copies of files.