Shell 脚本未正确保存文件
大家好,我制作了一个备份脚本,它将保存该文件,但我得到的不是名为 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,因此文件夹没有正确放置在正确的文件夹中,并且脚本的其余部分不起作用。我的数据类型是否错误?
谢谢
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=$(date '+%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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试这样
否则 shell 将寻找名为
DAY_OF_WEEK_files
的变量。同样适用于DAY_OF_WEEK_databases
。Try it like this
Otherwise the shell will go looking for a variable called
DAY_OF_WEEK_files
. Same applies toDAY_OF_WEEK_databases
.使用这些结构:
否则 bash 会假设您正在寻找名为
DAY_OF_WEEK_files
和DAY_OF_WEEK_databases
的变量。use these constructs:
otherwise bash will assume you are looking for a variable named
DAY_OF_WEEK_files
andDAY_OF_WEEK_databases
.