對於類,我們有一個遠程服務器,學校的主機上運行。到目前爲止,我已經做了很多的文件在服務器上,我想對它們進行備份的情況下,我想將它們傳輸到我的筆記本電腦或萬一我不小心刪除一個目錄或作出一個愚蠢的錯誤。我發現了一個教程和一個腳本來備份文件,我決定修改它,這樣它會確定它就是目錄(這將是主要用戶)和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 ='後面的空格,這是否做了什麼? (如果修復它,我會發布解釋原因的答案) –
現在完美地工作! –