Shell脚本无法正确保存文件

时间:2011-05-23 16:22:30

标签: linux shell backup

嘿所有,我制作了一个备份脚本,它将保存文件但是我得到的文件名为01_files.tar.gz或01_databases.tar.gz,而不是文件名为.tar.gz,然后是其余的脚本无法正常工作。

以下是我在文件名中设置所需数字的方法:

# Date Variables
DAY_OF_YEAR=$(date '+%j')
DAY_OF_MONTH=$(date '+%d')
DAY_OF_WEEK_RAW=$(date '+%w')
DAY_OF_WEEK=$((DAY_OF_WEEK_RAW + 1))
MONTH=$(date '+%m')
YEAR=$(date '+%Y')

这是我制作文件夹的地方:

# Make Weekly Folder
mkdir tmp/weekly
echo 'Made weekly folder...'

# Make Folder For Current Year
mkdir tmp/$YEAR
echo 'Made folder for current year...'

# Make Folder For Current Month
mkdir tmp/$YEAR/$MONTH
echo 'Made folder for current month...'

# Make Biannual Folder For Current Year
mkdir tmp/$YEAR/biannual
echo 'Made biannual folder for current year...'

# Make The Weekly Backup
tar -zcvf tmp/weekly/$DAY_OF_WEEK_files.tar.gz $SOURCE
mysqldump -Av -u $DATABASE_USER -p$DATABASE_PASSWORD > $DAY_OF_WEEK.sql
tar -zcvf tmp/weekly/$DAY_OF_WEEK_databases.tar.gz $SOURCE
rm -rf $DAY_OF_WEEK.sql
echo 'Made weekly backup...'

但就像我说的那样,我最终会使用.tar.gz而不是像01_files.tar.gz和01_databases.tar.gz那样的正确名称,因此文件夹没有正确放置在正确的文件夹中,其余的脚本不起作用。我做错了数据类型吗?

由于

2 个答案:

答案 0 :(得分:2)

尝试这样

tar -zcvf "tmp/weekly/${DAY_OF_WEEK}_files.tar.gz" $SOURCE

否则shell将寻找名为DAY_OF_WEEK_files的变量。同样适用于DAY_OF_WEEK_databases

答案 1 :(得分:1)

使用这些结构:

tar -zcvf tmp/weekly/${DAY_OF_WEEK}_files.tar.gz $SOURCE
mysqldump -Av -u $DATABASE_USER -p$DATABASE_PASSWORD > $DAY_OF_WEEK.sql
tar -zcvf tmp/weekly/${DAY_OF_WEEK}_databases.tar.gz $SOURCE
rm -rf $DAY_OF_WEEK.sql

否则bash会假设您正在寻找名为DAY_OF_WEEK_filesDAY_OF_WEEK_databases的变量。