有时我们需要备份 Linux 机器并将其复制到其他位置,但同时又不想丢失机器中的所有符号链接。
如果我们进行正常的压缩/解压缩,那么我们将丢失所有符号链接,这对于生产环境来说并不是一个好主意。
通过查看tar 的手册页,我们可以看到有这个选项。
# man tar -h, --dereference 遵循符号链接; 归档并转储它们指向的文件
所以,我们需要将 -h 参数与 tar 命令一起使用,但是如何以及何时使用?
假设我想将主目录复制到其他位置,并且我的主目录包含几个符号链接,如下所示
# cd /home/test # ls -l -rw-r--r--. 1 root root 0 Sep 28 12:45 1 lrwxrwxrwx. 1 root root 1 Sep 28 12:46 10 -> 5 -rw-r--r--. 1 root root 0 Sep 28 12:45 2 -rw-r--r--. 1 root root 0 Sep 28 12:45 3 -rw-r--r--. 1 root root 0 Sep 28 12:45 4 -rw-r--r--. 1 root root 0 Sep 28 12:45 5 lrwxrwxrwx. 1 root root 1 Sep 28 12:46 6 -> 1 lrwxrwxrwx. 1 root root 1 Sep 28 12:46 7 -> 2 lrwxrwxrwx. 1 root root 1 Sep 28 12:46 8 -> 3 lrwxrwxrwx. 1 root root 1 Sep 28 12:46 9 -> 4
现在让我们压缩并复制到其他位置。
首先 在没有使用“-h”参数的情况下压缩目录,如下所示:
[root@test home]# tar -czvf test.tar.gz test/ test/ test/2 test/4 test/.gnome2/ test/1 test/.bash_history test/8 test/.emacs test/5 test/.bash_logout test/9 test/10 test/.bashrc test/.bash_profile test/.mozilla/ test/.mozilla/extensions/ test/.mozilla/plugins/ test/6 test/7 test/3
将压缩文件移动到另一个位置
[root@test home]# mv test.tar.gz /tmp/ [root@test home]# cd /tmp/
解压缩目录时,请确保使用“-h”参数来保存符号链接,就像它们在压缩时一样
[root@test tmp]# tar -xhzvf test.tar.gz test/ test/2 test/4 test/.gnome2/ test/1 test/.bash_history test/8 test/.emacs test/5 test/.bash_logout test/9 test/10 test/.bashrc test/.bash_profile test/.mozilla/ test/.mozilla/extensions/ test/.mozilla/plugins/ test/6 test/7 test/3 [root@test tmp]# cd test
现在让我们验证它是否有效
[root@test test]# ls -l total 0 -rw-r--r--. 1 root root 0 Sep 28 12:45 1 lrwxrwxrwx. 1 root root 1 Sep 28 12:49 10 -> 5 -rw-r--r--. 1 root root 0 Sep 28 12:45 2 -rw-r--r--. 1 root root 0 Sep 28 12:45 3 -rw-r--r--. 1 root root 0 Sep 28 12:45 4 -rw-r--r--. 1 root root 0 Sep 28 12:45 5 lrwxrwxrwx. 1 root root 1 Sep 28 12:49 6 -> 1 lrwxrwxrwx. 1 root root 1 Sep 28 12:49 7 -> 2 lrwxrwxrwx. 1 root root 1 Sep 28 12:49 8 -> 3 lrwxrwxrwx. 1 root root 1 Sep 28 12:49 9 -> 4
日期:2020-06-02 22:17:03 来源:oir作者:oir