Ubuntu tar命令备份

官网 https://help.ubuntu.com/community/BackupYourSystem/TAR

 

备份前准备

  • 清除游览器缓存
  • 卸载所有挂载盘
  • 删除所有不需要的文件,包括视频图片、下载文件

使用命令查看当前路径各文件大小

du -h --max-depth=1

 

备份

tar -cvpzf backup.tar.gz --exclude=/backup.tar.gz --one-file-system /
  • tar - 创建归档
  • c - 创建资料归档
  • v - 输出过程在屏幕
  • p - 保留文件权限
  • z - 格式gzip
  • f - 指定存放的路径
  • --exclude=/example/path 意识是不包括的文件,即不希望备份的文件
  • --one-file-system - 复制文件或目录存放的文件系统必须和tar命令当前执行的文件系统相同,否则不执行;也就是讲,不处理其他分区的文件。如果要备份其他文件系统,如/home分区或装入/media中的外部介质,则需要单独备份它们,或者省略此标志。如果省略此标志,则需要再添加几个--exclude=参数,以避免出现不需要的文件系统。这些目录是根目录中的/proc、/sys、/mnt、/media、/run和/dev目录。/proc和/sys是虚拟文件系统,为运行内核的变量提供windows,因此您不想尝试备份或还原它们。/dev是一个tmpfs,其内容由udev动态创建和删除,因此您也不希望备份或还原它。同样,/run是一个tmpfs,它保存关于运行系统的不需要备份的变量。
  • 需要注意的是,这些排除是递归的。这意味着位于排除的文件夹中的所有文件夹也将被忽略。在本例中,排除/media文件夹会排除所有已安装的驱动器和介质。
  • 如果您希望备份位于/media中的某些分区,只需删除排除项并编写一个新的分区,排除不希望备份的分区存储在其中。例如:
--exclude=/media/unwanted_partition
  • / - 所有选项之后是要备份的目录。因为我们想备份系统上的所有内容,所以我们使用/作为根目录。与排除类似,它递归地包含根目录下未在排除或其他选项中列出的每个文件夹。

 

一旦对命令满意,就执行它并等待它完成。操作的持续时间取决于文件数量和压缩选项。完成后,检查您设置的查找存档文件的目录。在我们的示例中,backup.tar.gz将在完成后位于/目录中。然后,可以将此存档移动到任何其他目录以进行长期存储。

注意:在进程结束时,您可能会收到一条类似“tar:Error exit delayed from previous errors”之类的消息,但在大多数情况下,您可以忽略它。

 

 

备份备选方案

--one-file-system选项的“问题”是,您必须包含/boot/home或其他分区。

下面是一个tar示例,对excludes提出了一些额外的建议,以使生成的归档文件更小。请在使用此示例之前查看并理解排除项,并根据需要进行修改。

cd / # THIS CD IS IMPORTANT THE FOLLOWING LONG COMMAND IS RUN FROM /
tar -cvpzf backup.tar.gz \
--exclude=/backup.tar.gz \
--exclude=/proc \
--exclude=/tmp \
--exclude=/mnt \
--exclude=/dev \
--exclude=/sys \
--exclude=/run \ 
--exclude=/media \ 
--exclude=/var/log \
--exclude=/var/cache/apt/archives \
--exclude=/usr/src/linux-headers* \ 
--exclude=/home/*/.gvfs \
--exclude=/home/*/.cache \ 
--exclude=/home/*/.local/share/Trash /

 

  • /var/log 日志文件,你或许不需要备份
  • /var/cache/apt/archives 包括所有下载的dep包
  • /usr/src/linux-headers* again a space saver.
  • /home/*/{.cache,.gvfs,.local/share/Trash} 你获取不需要备份这些文件

 

 

额外提示

  • 使用Bzip2的格式比gzip的格式压缩效率更高,意味着备份包的体积更小,将选择z替换为j,后缀名为.tar.bz2
  • 如果要排除当前装载以外的所有其他装载(我指的是装载到目录的分区),请在排除规则之前附加“-one file system”选项。这会阻止tar进入任何目录(包括/mnt或/media)中的任何其他装载。例如,许多用户为`/home'创建一个单独的挂载,以使用户文件夹与根文件夹分开,将此选项添加到原始示例中会完全排除home的内容。

 

 

分节归档

如果要将存档文件刻录到光盘上,或将其传输到最大文件大小有限的文件系统(如每个文件限制4GB的FAT32),则必须在创建存档文件期间或之后拆分该文件。

可使用split命令

tar -cvpz <put options here> / | split -d -b 3900m - /name/of/backup.tar.gz.

 

  • 使用管道输出,否则将会变为标准输出
  • -d - 在文件的最后面添加数字标号,每个拆分将从01开始按顺序排列,并随着每个新拆分文件而增加。
  • -b - 指定要拆分的大小,如果大于3900m(兆),则拆分,如3901m=3900m+1m
  • - - 连字符是输入文件(通常是已经创建的实际文件)的占位符,并指示split使用标准输入。
  • /name/of/backup.tar.gz. 指定存放路径,第一个文件将为/name/of/backup.tar.gz.01

 

 

解压缩

重建完整的归档文件很容易,首先将“cd”放入保存分割归档文件的目录中。然后简单地使用“cat”将所有存档文件写入一个文件中,并通过标准输出发送到tar以提取到指定的目录。

cat *tar.gz* | tar -xvpzf - -C /

 

  • 在tar.gz之前和之后使用*作为通配符,告诉cat从第一个匹配的文件开始,然后每隔一个匹配的文件添加一个被称为catenation的进程,该命令是如何获得其名称的。
  • 之后,它只需将所有这些通过标准输出传递给tar,然后在本例中提取到根目录中。
     

 

通过网络备份

tar命令无法通过网络传输,但是ssh和netcat可以

Netcat

命令“nc”是一个通用的网络工具。它在两台联网的机器之间建立了一个简单的连接。此链接只有当人为取消才中断,这与正常连接(如tcp)不同,后者在文件完成时终止。

 

接收端

在接收端,您将设置netcat来编写备份文件,如下例所示。此命令将设置一台机器,以便从网络接收到端口1024的标准输入,然后将其写入文件backup.tar.gz。端口的选择完全取决于用户,只要它是1024或更大。一个简单的例子:

输入后等待发送端发送

nc -l 1024 > backup.tar.gz

 

发送端

在要备份的机器上,tar命令将通过管道传输到nc,nc将通过网络将备份发送到要写入文件的端口。请注意,其中显示<receiving host>替换为网络上计算机的名称。f选项被省略,因为我们不是在写入本地文件,而是通过标准输出移动存档。下面是一个例子:

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

 

SSH

使用SSH更加简单,但是需要输入密码,并且对方安装ssh服务

tar -cvpz <all those other options like above> / | ssh <backuphost> "( cat > ssh_backup.tar.gz )"

In the example:

  • tar命令还是与上方一样,通过管道传递给ssh
  • ssh_backup.tar.gz 生成的文件名
  • <backuphost> - 应替换为网络上有问题的计算机的名称。

 

 

Restoring

 

You will want to restore from a Live CD. If needed, first partition and format the drive. You can do this with gparted. Then simply mount the partition you are going to restore somewhere. If you open the drive in nautilus, it will be auto mounted somewhere under /media. Take a look to find out where with:

ls /media

 

Restore Your Backup

sudo tar -xvpzf /path/to/backup.tar.gz -C /media/whatever --numeric-owner

 

A brief explanation:

  • x - Tells tar to extract the file designated by the f option immediately after. In this case, the archive is /home/test/backup.tar.gz
  • -C - This option tells tar to change to a specific directory before extracting. In this example, we are restoring to the root (/) directory.
  • --numeric-owner - This option tells tar to restore the numeric owners of the files in the archive, rather than matching to any user names in the environment you are restoring from. This is due to that the user id:s in the system you want to restore don't necessarily match the system you use to restore (eg a live CD).

This will overwrite every single file and directory on the designated mount with the one in the archive. Any file created after the archive, will have no equivalent stored in the archive and thus will remain untouched

Allow the restoration the time it needs to complete. Once extraction is completed, you may need to recreate directories that were not included in the original archive because you excluded them with --exclude. This does not apply to filesystems excluded with --one-file-system. This can be done with the following command:

mkdir /proc /sys /mnt /media

 

Once finished, reboot and everything should be restored to the state of your system when you made the backup.

 

Restoring GRUB

 

For the system to boot, you will need to restore grub. To do this, you will need to reconfigure it in a chroot:

sudo -s
for f in dev dev/pts proc ; do mount --bind /$f /media/whatever/$f ; done
chroot /media/whatever
dpkg-reconfigure grub-pc

 

You will get a menu asking you what drive(s) grub should be installed on. Choose whatever drive(s) the computer will be booting from.

For more information on repairing grub, see GrubHowto

 

Restoring Over a Network

 

This short guide, assumes you employed nc to make the original backup as described above.

Receiving Computer Ensure the disk has been mounted and use the following command to accept input over the network that will then be extracted to the path indicated. In this example, the directory /mnt/disk will be extracted to.

nc -l 1024 | sudo tar -xvpzf - -C /media/whatever

 

Sending Computer On the computer with the archive to send, use the following command:

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

 

A few comments:

  • The - character in the first command will tell tar to accept input from standard input rather than a file. In this case, input comes from the pipe.
  • The backup file will be expanded without being saved on the disk of receiving computer, the same as when the backup was made.