LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 685|回复: 1

用rsync进行备份

[复制链接]
发表于 2004-9-22 13:17:16 | 显示全部楼层 |阅读模式
Backing up your Linux desktop with rsync
http://www.linux.com/article.pl?sid=04/09/15/1931240

By: Brice Burgess

Rsync is a command line utility traditionally used in synchronizing files between two computers, but rsync can also be used as an effective backup tool. This free and powerful tool is simple enough for anyone to use on their Linux desktop.

This article explain how to use rsync to backup your computer to a drive attached to your system. You
can use a removable drive, such as an external USB hard drive, so that you can store the backups in a safe place away from your working environment.

First, make sure you have rsync by entering rsync --version at the command line. If you see rsync version 2.X.X protocol version X, you have it. If you see "command not found" or a similar message, you need to download and install rsync. Use your distribution's package management system to do this, or else download and install the source from the rsync Web site. Make sure your version is greater than 2.6.0.

Now it's time to consider what to back up, to where, and when?

What should be backed up? Do you want to run a full system or a partial system backup? A full system backup creates a second copy of everything on your hard drive. This has the advantage of providing a means to quickly restore your system to the exact state it was in when you made the backup. Full system backups take a long time to complete, take up a lot of disk space, and are often unnecessary. When you run full system backups, make sure to use rsync's --exclude parameter. Certain directories, such as /proc, should not be backed up. See the backup.sh script below as an example.

Partial system backups are faster and more space-efficient, because you copy only important hand-selected data. For instance, you may want to backup only the /home directory, which contains users' documents, music, and program settings. The operating system files, such as those under /usr (programs) and /var (log files, email, etc.) can be easily reinstalled and don't need to be backed up.

Where should it be backed up? Your imagination is the limit when it comes to rsync's backup destination options. The scope of this article, however, is limited to local disk drives. Ideally you'll want to store your backups on a separate disk so that they'll be accessible if your hard drive fails. Once you create a backup, you can copy it to CDs, tapes, or other removable storage to increase chances of recovery.

When should it be backed up? Automated daily backups are a good choice for most Linux desktop scenarios. You can use Linux's built in scheduler, the cron daemon, with shell scripts to automate your backups.

Using rsync

The basic implementation of rsync is: rsync -a source/ target/. This command copies the source directory to the target as if you were executing cp -a source/. target/. Unlike cp, rsync uses the rsync algorithm to check for differences between source and destination files. Since it copies only new changes, a technique known as incremental backup, rsync provides a very fast method for updating your backups.

Making exact copies using the --delete flag. You can apply the --delete flag when making system backups, which causes rsync to delete any files found in the target that are not present in the source. This ensures that the target is an exact copy of the source, so that if you delete an unwanted document, it is also removed from your backup. Rsync preserves files found in the target and not in the source by default, allowing for multiple sources to be added to a single target destination. To get around this behavior, use a command like the following:
rsync -a --delete source/ target/

Keeping multiple backups. It is a good idea to keep a few days' worth of backups so that you can return to a particular day if necessary. You can do this by rotating the oldest backup to the current one and updating it using rsync. The following code demonstrates a three-day backup rotation:

  1. # change to target directory holding backups
  2. cd /target_directory

  3. # move oldest backup to temporary name
  4. mv day3 oldest

  5. # increment backups, rotate oldest backup to todays
  6. mv yesterday day3
  7. mv today yesterday
  8. mv oldest today

  9. # update todays backup
  10. cp -al yesterday/. today
  11. rsync -a --delete source/ today/
复制代码


Making your first backup. Whether you backup to an external USB or FireWire hard drive, a secondary internal hard drive, or even the same hard drive that Linux is installed on, make sure the target destination has enough space for your backed up information. To get an idea of how much space a directory takes up, use the command du -sh /directory.

Create a backup.sh script by copying the code below into your favorite text editor. Decide on the files and directories you'd like to backup and add them to the $SOURCES variable in the script:

  1. #!/bin/sh
  2. # backup.sh -- backup to a local drive using rsync

  3. # Directories to backup. Separate with a space. Exclude trailing slash!
  4. SOURCES="/home/wendy /home/daisy /var/mail"

  5. # Directory to backup to. This is where your backup(s) will be stored.
  6. TARGET="/mnt/usb-harddrive/backup/"

  7. # Your EXCLUDE_FILE tells rsync what NOT to backup. Leave it unchanged if you want
  8. # to backup all files in your SOURCES. If performing a FULL SYSTEM BACKUP, ie.
  9. # Your SOURCES is set to "/", you will need to make use of EXCLUDE_FILE.
  10. # The file should contain directories and filenames, one per line.
  11. # An example of a EXCLUDE_FILE would be:
  12. # /proc/
  13. # /tmp/
  14. # /mnt/
  15. # *.SOME_KIND_OF_FILE

  16. EXCLUDE_FILE="/path/to/your/exclude_file.txt"

  17. # Comment out the following line to disable verbose output
  18. VERBOSE="-v"
  19. ###########################

  20. if [ -f $EXCLUDE_FILE ]; then
  21. EXCLUDE="--exclude-from=$EXCLUDE_FILE"
  22. fi

  23. for source in $SOURCES; do
  24. if [ ! -d $TARGET/$source ]; then
  25. mkdir -p $TARGET$source
  26. fi

  27. rsync $VERBOSE $EXCLUDE -a --delete $source/ $TARGET/$source/
  28. done
复制代码

Change the $TARGET variable to the path where you'd like your backups to be saved. Save the script (as backup.sh) to your computer and make it executable with the command chmod +x backup.sh.

You're now ready to make your first backup. Type ./backup.sh to start the process. The script will take a long time to complete the first time you run it, because rsync must make a copy of each file rather than update just changed files. Later runs will complete much faster. If you notice something is wrong, press Ctrl-C to stop the process. Upon completion of the script, you should have a replica of your $SOURCES in your $TARGET.

Automating the process. Assuming backup.sh ran successfully, and that you now have a copy of your important files in the $TARGET directory, it is time to automate the process. We'll use Linux's built-in scheduler, the cron daemon, to do this. The cron daemon uses "crontab" files to schedule tasks. The system's main crontab file can be accessed by becoming the superuser (either by logging in as root or typing su at the command line) and executing crontab -e.

You'll want to schedule a time for your backup.sh to execute. Crontab syntax is:
[minute] [hour] [day] [month] [dayofweek] [command]

Thus, adding the line:
0 4 * * * /path/to/backup.sh
will execute backup.sh at 4:00am every day. When you're finished adding the line, save the file and exit.

That's all there is to it. Rsync is a very powerful tool, and you should pat yourself on the back for applying some of its potential. In the future we'll cover how to backup to a remote machine, show examples on how to keep multiple backups in rotation, and even run rsync within Microsoft Windows. In the meantime, check out Mike Rubel's excellent resource on rsync to learn how to perform daily and even hourly backups.
发表于 2004-9-22 16:06:40 | 显示全部楼层
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表