Shell Scripts

3 篇文章 0 订阅

https://help.ubuntu.com/lts/serverguide/backup-shellscripts.html


Shell Scripts

One of the simplest ways to backup a system is using a shell script. For example, a script can be used to configure which directories to backup, and pass those directories as arguments to the tar utility, which creates an archive file. The archive file can then be moved or copied to another location. The archive can also be created on a remote file system such as an NFS mount.

The tar utility creates one archive file out of many files or directories. tar can also filter the files through compression utilities, thus reducing the size of the archive file.

Simple Shell Script

The following shell script uses tar to create an archive file on a remotely mounted NFS file system. The archive filename is determined using additional command line utilities.

#!/bin/bash
####################################
#
# 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
  • $backup_files: a variable listing which directories you would like to backup. The list should be customized to fit your needs.

  • $day: a variable holding the day of the week (Monday, Tuesday, Wednesday, etc). This is used to create an archive file for each day of the week, giving a backup history of seven days. There are other ways to accomplish this including using the date utility.

  • $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 网络文件系统 (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 参考资料 for links to resources providing more in-depth shell scripting information.

Executing the Script

Executing from a Terminal

The simplest way of executing the above backup script is to copy and paste the contents into a file. backup.sh for example. The file must be made executable:

chmod u+x backup.sh

Then from a terminal prompt:

sudo ./backup.sh

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

Executing with 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 thecrontab -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 参考资料.

Restoring from the Archive

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 torestore 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/hoststar 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.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值