对于课堂,我们必须在学校托管的远程服务器上工作。到目前为止,我已经在服务器上制作了很多文件,我想备份它们,以防我想将它们传输到我的笔记本电脑,或者我不小心删除了一个目录或犯了一个愚蠢的错误。我找到了一个教程和一个脚本来备份文件,我决定对其进行修改,以便确定它所在的目录(这将是主用户的目录)和文档的 cd。如果目录不存在,它还会创建目录 Backups。我对这种脚本仍然很陌生,任何额外的建议或帖子链接将不胜感激。
代码:
#!/bin/bash
#######################################################
## Simple backup script..
## Created by Matthew Brunt: (openblue555@gmail.com)
## Licensed under GNU GPL v3 or later, at your option.
## http://www.gnu.org/licenses/gpl.html
##
## Further edited by Michael Garrison to backup the
## directory it is located in and print the contents.
#######################################################
mkdir -p Backup
#Defines our output file
OUTPUT= $( cd Backup && pwd )/backup_$(date +%Y%m%d).tar.gz
#Defines our directory to backup
BUDIR=$( cd Desktop && pwd )
#Display message about starting the backup
echo "Starting backup of directory $BUDIR to file $OUTPUT"
#Start the backup
tar -cZf $OUTPUT $BUDIR
#Checking the status of the last process:
if [ $? == 0 ]; then
#Display confirmation message
echo "The file:"
echo $OUTPUT
echo "was created as a backup for:"
echo $BUDIR
echo ""
echo "Items that were backed up include:"
for i in $BUDIR; do
echo $i
done
echo ""
else
#Display error message message
echo "There was a problem creating:"
echo $OUTPUT
echo "as a backup for:"
echo $BUDIR
fi
我知道原始脚本有效,并且在我更改$OUTPUT
变量之前一直有效。我目前得到以下结果:
./backup.sh
./backup.sh: line 15: /Users/mgarrison93/Backup/backup_20121004.tar.gz: No such file
or directory
Starting backup of directory /Users/mgarrison93/Desktop to file
tar: no files or directories specified
There was a problem creating:
as a backup for:
/Users/mgarrison93/Desktop
我可以看到它不接受文件名,但我不知道如何更正。
我只是尝试更改$OUTPUT
为/Backups/file-name.tar.gz
我最初拥有的并且效果很好。问题似乎是$( cd Backup && pwd )/backup_$(date +%Y%m%d).tar.gz
。只是不确定出了什么问题。