开发者

Shell Script Not Saving File Correctly

开发者 https://www.devze.com 2023-03-08 06:11 出处:网络
Hey all, I made a backup script and it will save the file but instead of the file being named 01_files.tar.gz or 01_databases.tar.gz all I get is a .tar.gz and then the rest of the script cannot work

Hey all, I made a backup script and it will save the file but instead of the file being named 01_files.tar.gz or 01_databases.tar.gz all I get is a .tar.gz and then the rest of the script cannot work properly.

Here is how I set the numbers I needs in my filename:

# Date Variables
DAY_OF_YEAR=$(date '+%j')
DAY_OF_MONTH=$(date '+%d')
DAY_OF_WEEK_RAW=$(dat开发者_JS百科e '+%w')
DAY_OF_WEEK=$((DAY_OF_WEEK_RAW + 1))
MONTH=$(date '+%m')
YEAR=$(date '+%Y')

Here is where I am making my folders:

# 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...'

However like I said I am ending up with .tar.gz instead of a proper name like 01_files.tar.gz and 01_databases.tar.gz so the folders are not being properly placed in the correct folders and the rest of the script doesn't work. Am I doing a data type wrong?

Thanks


Try it like this

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

Otherwise the shell will go looking for a variable called DAY_OF_WEEK_files. Same applies to DAY_OF_WEEK_databases.


use these constructs:

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

otherwise bash will assume you are looking for a variable named DAY_OF_WEEK_files and DAY_OF_WEEK_databases.

0

精彩评论

暂无评论...
验证码 换一张
取 消