Linux 在创建 tarball 时保留硬链接和符号链接、权限,并在解压 tarball 时执行相同操作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11047919/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
to preserve hard and symbolic link, permissions while creating tar ball and doing same while untar the tarball
提问by samantha
Task: To provide facility to upgrade the system remotely or add new features.
任务:提供远程升级系统或添加新功能的设施。
What I am supposed to doCreate a back up of current environment of target machine and if upgrading fails at any stage, then revert back to original environment.
我应该做什么创建目标机器当前环境的备份,如果在任何阶段升级失败,则恢复到原始环境。
Say my directory structures are something like this:
说我的目录结构是这样的:
/home/user/project1/....bla bla
/home/user/project1/....bla bla
project 1 contains symbolic links, hard links, executable files of software and firmware etc.
项目 1 包含符号链接、硬链接、软件和固件的可执行文件等。
My dilemma
我的困难
Should I use strategy 1 or 2?
我应该使用策略 1 还是策略 2?
Should I copy the whole current environment and revert back if upgrading fails.
example-> cp -p -r /home/user/project1/* /home/user/project1_backup/
if upgrading fails -->
mv /home/user/project1_backup/ /home/user/project1
Should I tarball the whole environment and untar it if upgrading fails. To create tar ball, I'm a bit skeptical about preserving symbolic links and hard links .. and same while untar it.
如果升级失败,我是否应该复制整个当前环境并恢复。
示例-> cp -p -r /home/user/project1/* /home/user/project1_backup/
如果升级失败-->
mv /home/user/project1_backup/ /home/user/project1
如果升级失败,我是否应该压缩整个环境并解压缩它。为了创建焦油球,我对保留符号链接和硬链接有点怀疑......在解压它时也是如此。
Could some please provide concrete answer which method I should follow and if I go with tar ball approach what will be the bash command.
请一些人提供具体的答案,我应该遵循哪种方法,如果我使用 tar ball 方法,那么 bash 命令将是什么。
As far as I know tar -cvfz
for creating tar gunzip will notpreserve the links and permissions and similarly while untarring the tar ball.
Please throw some light?
据我所知 tar -cvfz
,创建 tar gunzip不会保留链接和权限,并且在解压 tar 球时类似。请放点灯?
采纳答案by neam
I would use the second option: create a tarball; because tarballs have some positive points:
我会使用第二个选项:创建一个 tarball;因为 tarball 有一些优点:
- You can preserve permission/specials files across filesystems (usefull when you backup your a ext* folder to an NTFS filesystem)
- A copy of one single big file will be faster than a thousand small files
- You can compress it.
- 您可以跨文件系统保留权限/特殊文件(当您将 ext* 文件夹备份到 NTFS 文件系统时很有用)
- 一个大文件的副本将比一千个小文件快
- 你可以压缩它。
And here is the command:
这是命令:
tar --preserve-permissions --preserve-order -jc /path/to/your/folder > /path/to/your/backup_file.tar.bz2
This shall preserve your permission, your symlinks. And for the hardlinks, I give you this link (http://www.gnu.org/software/tar/manual/html_node/hard-links.html)
这将保留您的许可,您的符号链接。对于硬链接,我给你这个链接(http://www.gnu.org/software/tar/manual/html_node/hard-links.html)
(but by default, tar preserve hardlinks)
(但默认情况下,tar 保留硬链接)
Don't forget to test your tarball before upgrading your system !!
在升级系统之前不要忘记测试你的压缩包!!
(you will avoid almost all lose of data in the case of the archive isn't correctly created)
(在未正确创建存档的情况下,您将避免几乎所有数据丢失)