Shell 脚本未正确保存文件

发布于 2024-11-09 01:22:01 字数 1162 浏览 0 评论 0原文

大家好,我制作了一个备份脚本,它将保存该文件,但我得到的不是名为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

反差帅 2024-11-16 01:22:01

尝试这样

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

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

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.

只怪假的太真实 2024-11-16 01:22:01

使用这些结构:

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 的变量。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文