对于类,我们有一个远程服务器,学校的主机上运行。到目前为止,我已经做了很多的文件在服务器上,我想对它们进行备份的情况下,我想将它们传输到我的笔记本电脑或万一我不小心删除一个目录或作出一个愚蠢的错误。我发现了一个教程和一个脚本来备份文件,我决定修改它,这样它会确定它就是目录(这将是主要用户)和CD的文件。如果备份不存在,它还会创建目录。对于这类脚本,我仍然很陌生,任何额外的建议或帖子链接都将不胜感激。脚本将无法识别的文件/目录
代码:
#!/bin/bash
#######################################################
## Simple backup script..
## Created by Matthew Brunt: ([email protected])
## 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
。只是不知道什么是错的。
尝试删除'OUTPUT ='后面的空格,这是否做了什么? (如果修复它,我会发布解释原因的答案) –
现在完美地工作! –