如何使用 bash 脚本用 tar 备份文件系统?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/11467698/
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
How to backup filesystem with tar using a bash script?
提问by ghoti
I want to backup my ubuntu filesystem, and I wrote this little script. It is very basic, but being my first try I am afraid to do mistakes. And since it will take few hours to complete to see results, I think it is better to ask you as experienced programmers if I did something wrong.
我想备份我的 ubuntu 文件系统,我写了这个小脚本。这是非常基本的,但作为我的第一次尝试,我害怕犯错误。并且由于完成需要几个小时才能看到结果,我认为最好向有经验的程序员询问我是否做错了什么。
I'm particularly interested in >will that record output of mvor will it output also results of tar?
Also variables inside tar command is it correct way?
我特别感兴趣的>是记录输出mv还是输出结果tar?tar 命令中的变量也是正确的方法吗?
#!/bin/bash
mybackupname="backup-fullsys-$(date +%Y-%m-%d).tar.gz"
{ time tar -cfpzv $mybackupname --exclude=/$mybackupname --exclude=/proc --exclude=/lost+found --exclude=/sys --exclude=/mnt --exclude=/media --exclude=/dev / && ls -gh $mybackupname && mv -v $mybackupname backups/filesystem/ ; } > backup-system.log
exit
Anything I should know before I run this?
在我运行这个之前我应该知道什么?
回答by ghoti
Sandro, you might want to consider spacing things out in your script and producing individual errors. Makes things much easier to read.
Sandro,您可能需要考虑将脚本中的内容分开并产生个别错误。使事情更容易阅读。
#!/bin/bash
mybackupname="backup-fullsys-$(date +%Y-%m-%d).tar.gz"
# Record start time by epoch second
start=$(date '+%s')
# List of excludes in a bash array, for easier reading.
excludes=(--exclude=/$mybackupname)
excludes+=(--exclude=/proc)
excludes+=(--exclude=/lost+found)
excludes+=(--exclude=/sys)
excludes+=(--exclude=/mnt)
excludes+=(--exclude=/media)
excludes+=(--exclude=/dev)
if ! tar -czf "$mybackupname" "${excludes[@]}" /; then
  status="tar failed"
elif ! mv "$mybackupname" backups/filesystem/ ; then
  status="mv failed"
else
  status="success: size=$(stat -c%s backups/filesystem/$mybackupname) duration=$((`date '+%s'` - $start))"
fi
# Log to system log; handle this using syslog(8).
logger -t backup "$status"
If you wanted to keep debug information (like the stderr of taror mv), that could be handled with redirection to a tmpfile or debug file.  But if the command is being run via cron and has output, cron should send it to you via email.  A silent cron job is a successful cron job.
如果您想保留调试信息(如tar或的标准错误mv),可以通过重定向到 tmpfile 或调试文件来处理。但是如果命令是通过 cron 运行的并且有输出,cron 应该通过电子邮件将它发送给你。无声的 cron 作业是成功的 cron 作业。
The series of ifs causes each program to be run as long as the previous one was successful.  It's like chaining your commands with &&, but lets you run other code in case of failure.
if只要前一个程序成功,这一系列s 就会使每个程序都运行。这就像用 链接你的命令&&,但让你在失败的情况下运行其他代码。
Note that I've changed the order of options for tar, because the thing that comes after -fis the file you're saving things to.  Also, the -poption is only useful when extractingfiles from a tar.  Permissions are always saved when you create (-c) a tar.
请注意,我更改了 的选项顺序tar,因为后面的内容-f是您要将内容保存到的文件。此外,该-p选项仅在从 tar 中提取文件时才有用。创建 ( -c) tar时,始终会保存权限。
Others might wish to note that this usage of the statcommand works in GNU/Linux, but not other unices like FreeBSD or Mac OSX.  In BSD, you'd use stat -f%z $mybackupname.
其他人可能希望注意到该stat命令的这种用法适用于 GNU/Linux,但不适用于 FreeBSD 或 Mac OSX 等其他 unice。在 BSD 中,你会使用stat -f%z $mybackupname.
回答by Paused until further notice.
The file redirection as you have it will only record the output of mv.
您拥有的文件重定向只会记录mv.
You can do
你可以做
{ tar ... && mv ... ; } > logfile 2>&1
to capture the output of both, plus any errors that may occur.
捕获两者的输出,以及可能发生的任何错误。
It's a good idea to always be in the habit of quoting variables when they are expanded.
始终养成在扩展时引用变量的习惯是个好主意。
There's no need for the exit.
不需要exit.

