特殊:Badtitle/NS100:BackupYourSystem/TAR/zh:修订间差异

来自Ubuntu中文
跳到导航跳到搜索
Chenfengyuan留言 | 贡献
Chenfengyuan留言 | 贡献
 
第8行: 第8行:
|}
|}
{|border="1" cellspacing="0"
{|border="1" cellspacing="0"
|https://help.ubuntu.com/community/IconsPage?action=AttachFile&do=get&target=stop.png  如果你没有正确使用程序来备份,那你的资料可能就会被你抹去了。所以请在读完后再动手做
|https://help.ubuntu.com/community/IconsPage?action=AttachFile&do=get&target=stop.png  如果你没有正确使用程序来备份,那你的资料可能就会被你抹去了。所以请在读完后再动手
|}
|}
=== 介绍 ===
=== 介绍 ===

2009年6月25日 (四) 09:31的最新版本

{{#ifexist: :BackupYourSystem/TAR/zh/zh | | {{#ifexist: BackupYourSystem/TAR/zh/zh | | {{#ifeq: {{#titleparts:BackupYourSystem/TAR/zh|1|-1|}} | zh | | }} }} }} {{#ifeq: {{#titleparts:BackupYourSystem/TAR/zh|1|-1|}} | zh | | }}

  1. title

译注:文章已经经过改动。

用TAR来备份系统

IconsPage?action=AttachFile&do=get&target=note.png 这篇文章已经被改动过了,以便更好地刊登到wiki上。完整的版本可以看这里the original forum post.
IconsPage?action=AttachFile&do=get&target=stop.png 如果你没有正确使用程序来备份,那你的资料可能就会被你抹去了。所以请在读完后再动手

介绍

这篇文章是HOWTO系列中 备份系统 的一部分.

IconsPage?action=AttachFile&do=get&target=info.png This guide to backup your system using tar to create compressed archives is based upon a post from the Ubuntu Community Forums written by Heliode. See the original forum thread for discussion: http://www.ubuntuforums.org/showthread.php?t=35087 .

为备份系统做准备

IconsPage?action=AttachFile&do=get&target=IconHint2.png Just a quick note. You are about to back up your entire system. Don't forget to empty your Wastebasket, remove any unwanted files in your /home directory, and cleanup your desktop.
  • Depending on why you're backing up, you might want to:
  • Delete all your emails
  • Clear your browser search history
  • Wipe your saved browser personal details
IconsPage?action=AttachFile&do=get&target=IconNote.png If you are not worried about the security concerns, this step is not necessary. Many users explicitly want backups of their email and browser settings.
  • Unmount any external media devices, and remove any CDs/DVDs not needed for the backup process.
  • This will lessen the amount of exclusions you need to type later in the process.

Backing up

IconsPage?action=AttachFile&do=get&target=terminal.png

  • Some directories require root or superuser permissions to successfully backup. Gain superuser access by opening a terminal and entering:

sudo -s -H

  • Go to the root of your file system:

cd /

IconsPage?action=AttachFile&do=get&target=IconHint2.png We use the file system root in our example, but you use any target destination you want. You can use remote or removable drives as your backup destination.
  • Create a backup of your system:

tar -cvzf /backup.tgz --exclude=/proc --exclude=/lost+found --exclude=/backup.tgz --exclude=/mnt --exclude=/sys /

  • IconsPage?action=AttachFile&do=get&target=info.png Now, lets explain this a little bit:
  • 'tar' is the program used to do a backup
  • c - create a new backup archive
  • v - verbose mode, tar will print what it's doing to the screen
  • z - compress the backup file with 'gzip' to make it smaller
  • f <filename> - specifies where to store the backup, /backup.tgz is the file used in this example
  • Now come the directories we want to exclude. We don't want to backup everything since some directories aren't very useful to include.
  • Make sure you don't include the file itself, or else you'll get weird results.
  • Don't include the /mnt folder if you have other partitions mounted there.
  • If you have Partitions in /mnt that require backup, you will need to exclude the folders you do not want backed up.<
    >
IconsPage?action=AttachFile&do=get&target=example.png --exclude=/mnt/<unwanted_partition>
  • Make sure you don't have anything mounted in /media.
  • Remove CDs/DVDs and removable media that you don't need backed up. You can selectively exclude directories in /media if you want removable devices backed up.
  • After all of the options is the directory we want to backup. Since we want to backup everything we use / for the root directory.
  • If you want to exclude all other file systems you can use the --one-file-system option in addition to or instead of --exclude.
  • With the --one-file-system option, only the "local" file system is backed up. <
    >
IconsPage?action=AttachFile&do=get&target=info.png The "local" file system is the file system you have specified, not the file systems mounted under it in the file hierarchy. Use df to see which file systems you have mounted.

IconsPage?action=AttachFile&do=get&target=example.png

tar -cvzf /backup.tgz --one-file-system --exclude=/lost+found --exclude=/backup.tgz /
  • Relax while Tar creates a backup of your system. This make take awhile depending on the amount of data that is being backed up and the speed of your processor. When the process is complete you will have a file named backup.tgz in the root directory of your file system. This file may be burned to a CD/DVD, moved to another partition/drive, or even stored on another machine.
  • IconsPage?action=AttachFile&do=get&target=stop.png Files that are bigger than 2GB are not supported by some implementations of ISO9660 and may not be restorable. So don't simply burn a DVD with a huge .iso file on it. Split it up using the command split or use a different way to get it onto the DVD. See man split for further information on split.
  • A possible workaround is the following:

sudo tar --create --bzip2 --exclude /tmp --one-file-system --sparse / | growisofs -use-the-force-luke -Z /dev/hda=/proc/self/fd/0

  • Note that this only backs up one file system. You might want to use --exclude instead of --one-file-system to filter out the stuff you don't want backed up. This assumes your DVD drive is /dev/hda. This will not create a mountable DVD. To restore it you will reference the device file:

sudo tar --extract --bzip2 --file /dev/hda

IconsPage?action=AttachFile&do=get&target=IconHint2.png At the end of the process you might get a message along the lines of 'tar: Error exit delayed from previous errors' or something, but in most cases you can just ignore that.
  • Another workaround would be to Bzip2 to compress your backup. Bzip2 provides a higher compression ratio at the expense of speed. If compression is important to you, just substitute the z in the command with j, and change the file name to backup.tar.bz2.

That would make the command look like this:

tar -cvpjf /backup.tar.bz2 --exclude=/proc --exclude=/lost+found --exclude=/backup.tar.bz2 --exclude=/mnt --exclude=/sys /

Backup over a network

  • It is possible to use netcat to transfer the backup between computers.
  • On the receiving end you'll setup netcat to write the backup file like this:

nc -l -p 1024 > backup.tar.bz2

  • Then you pipe the tar command without the f flag through netcat on the sending end like this:

tar -cvj <all those other options> / | nc -q 0 <receiving host> 1024

  • In the above commands 1024 is just a random port number, anything from 1024 and up should work.
  • If all goes well the backup will be piped through the network without touching the file system being read.
  • You can also use SSH:

tar zcvf - /home | ssh <backuphost> "( cat > home_bkp.tar.gz )"

  • IconsPage?action=AttachFile&do=get&target=info.png In this example:
  • The directory to backup is /home
  • The backup destination is home_bkp.tar.gz on the machine called <backuphost>.
  • The hyphen before /home tells tar to send output to stdout rather than to a file.
  • Adding the 'p' option to tar would preserve file permissions.

Restoring

IconsPage?action=AttachFile&do=get&target=warning.png Please, for goodness sake, be careful here. If you don't understand what you are doing here you might end up overwriting stuff that is important to you, so please take care!

For the purpose of this tutorial we will assume your backup file is stored in the file system root. We will also assume that you have already gained superuser access through sudo.

  • Restore your backup:

tar -xvpzf /backup.tgz -C /

  • If you used bz2:

tar -xvpjf backup.tar.bz2 -C /

  • IconsPage?action=AttachFile&do=get&target=info.png A brief explanation:
  • The x option tells tar to extract the file.
  • The -C <directory> option tells tar to change to a specific directory before extracting. " / " in this example.
  • The p option preserves all file permissions. This is default action for tar when used by the superuser.
IconsPage?action=AttachFile&do=get&target=warning.png This will overwrite every single file on your partition with the one in the archive.
  • The restoration process may take awhile, depending on the size of the archive and the speed of your computer.
  • Once the extraction is complete, re-create the directories which were excluded.

mkdir /proc /lost+found /mnt /sys

  • Reboot and everything should be restored to the state of your system when you made the backup.
IconsPage?action=AttachFile&do=get&target=IconNote.png It may not be exactly the way it was when you made the backup, because files created after the backing up won't be deleted.

Restoring over a network

  • If you used nc to backup to another computer the commands to restore are:
  • On the receiving side:
  • Mount the disk (if you are running from a LiveCD)and type:

nc -l -p 1024 | tar -xvpjf - -C /mnt/disk

  • On the sender side, the side that has the backup file:

cat backup.tar.bz2 | nc -q 0 <receiving host> 1024

  • The - character will tell tar to accept the input from stdin, the pipe.
  • The backup file will be expanded without being saved on the disk on the receiver, like when the backup was made.
  • "-xvpjf is for a .bz2 file, change j to z if you used a tar.gz backup.

Reformatted Partitions

  • If you had to format partitions, update the /etc/fstab and /boot/grub/menu.lst files after restoring the backup.
  1. Mount the reformatted partitions on a LiveCD.
  2. Open a terminal and type
    blkid
  3. Making note of the UUIDs, edit the /etc/fstab and /boot/grub/menu.lst files in the restored root partition.
    sudo nano /mnt/disk/etc/fstab
    sudo nano /mnt/disk/boot/grub/menu.lst
    Change the UUIDs to match the results of your blkid command.

Restoring GRUB

  • In most cases restoring GRUB should not be necessary.
  • If when you boot the system you are left on a grub shell, just do this

find /boot/grub/stage1

root (hd?,?)
setup (hd?)

substitute the ? with the results of your find command.

press esc to restart the system.

Additional resources