我尝试使用脚本gzip文件,但它不起作用并继续抛出错误。有人可以就这个脚本的问题给出一些指导吗?
DEFAULTDIRECTORY=”/Backup”
if [ -d "$DEFAULTDIRECTORY" ]; then
mkdir -p /backup
fi # Makes directory if the directory does not exist
# Set the timestamp for the backup
TIMESTAMP=`date +%Y%m%d.%H%M`
# let the user choose what they want to backup
echo -n "Select the file or directory you want to backup"
read Chosendata
# read the backup file name file
echo -n "Select the file name"
read FNAME
# start the backup.
echo -e "Starting backup"
# compress the directory and files, direct the tar.gz file to your destination directory
tar -vczf ${FNAME}-${TIMESTAMP}.tar.gz ${Chosendata} > ${DEFAULTDIRECTORY}
# end the backup.
echo -e "Backup complete"
答案 0 :(得分:0)
将tar命令的输出重定向到DEFAULTDIRECTORY
不会执行注释指定的内容。
我认为您要做的是将文件保存在DEFAULTDIRECTORY
。
更改行
tar -vczf ${FNAME}-${TIMESTAMP}.tar.gz ${Chosendata} > ${DEFAULTDIRECTORY}
到
tar -vczf $DEFAULTDIRECTORY/${FNAME}-${TIMESTAMP}.tar.gz ${Chosendata}
答案 1 :(得分:0)
您需要否定此测试if [ -d "$DEFAULTDIRECTORY" ]; then
- > if [ ! -d "$DEFAULTDIRECTORY" ]; then
。您不应该使用tar命令重定向。相反,你应该在tar.gz文件前加上它:
tar -vczf $DEFAULTDIRECTORY/${FNAME}-${TIMESTAMP}.tar.gz ${Chosendata}
答案 2 :(得分:0)
试试这个:
#!/bin/bash
DEFAULTDIRECTORY="/Backup"
# Makes directory if the directory does not exist
# Here you had an inverted statement: use "if [ ! -d ... ] then" or "[ -d ... ] ||"
[ -d "${DEFAULTDIRECTORY}" ] || mkdir -p "${DEFAULTDIRECTORY}"
# Set the timestamp for the backup
# For subshell command you can use `...` or $(...)
TIMESTAMP=$(date +%Y%m%d.%H%M)
# Let the user choose what they want to backup
# You can use the -p (prompt) option instead of using echo
read -p "Select the file or directory you want to backup: " CHOSENDATA
# Read the backup file name
read -p "Select the file name: " FILENAME
# Start the backup.
echo "Starting backup"
# Compress the directory, direct the tar.gz file to your destination directory
# tar 'create' ' zip' 'verbose' 'force' <The output filename> <The data you want to backup>
tar -czvf ${DEFAULTDIRECTORY}/${FILENAME}-${TIMESTAMP}.tar.gz ${CHOSENDATA}
# End the backup.
echo "Backup complete"