使用日期和时间作为文件名生成备份文件

发布于 2024-10-08 22:29:15 字数 1106 浏览 0 评论 0原文

我该如何正确地做到这一点。我试图将 mysqldump 生成的 sql 文件命名为当前日期和时间。我已经在这个网站上进行了一些研究,并在这里找到了代码: 如何在 Windows 命令行上以适合在文件名中使用的格式获取当前日期时间?

尝试将其与我当前的代码混合,然后我来了就这个吧。该文件以当前日期和时间命名,但它只是一个 1kb 文件,并且不会生成 .sql 文件。 它应该是一个 7 kb 的 sql 文件。

@For /f "tokens=2-4 delims=/ " %%a in ('date /t') do @(set mydate=%%c-%%a-%%b)
@For /f "tokens=1-2 delims=/:" %%a in ('time /t') do @(set mytime=%%a%%b)    

@echo mydate= %mydate%
@echo mytime= %mytime%

mysqldump -u root -p --add-drop-table --create-options --password= onstor >c:\%mydate%_%mytime%.sql

更新 我不认为 mysqldump 命令有问题,因为当我这样做时它工作得很好。下面的代码仅使用日期作为文件名。

@For /F "tokens=2,3,4 delims=/ " %%A in ('Date /t') do @( 
    Set Month=%%A
    Set Day=%%B
    Set Year=%%C   
)    

@echo DAY = %Day%
@echo Month = %Month%
@echo Year = %Year%    

mysqldump -u root --add-drop-table --create-options --password= onstor >c:\%Day%-%Month%-%Year%.sql

请帮忙,谢谢。

How do I do this properly. I'm trying to name the sql file that is being produced by mysqldump into the current date and time. I've already some research in this site and found a code in here: How to get current datetime on Windows command line, in a suitable format for using in a filename?

Tried to mixed it up with my current code and I came up with this one. The file is named into the current date and time but its only a 1kb file and does not produce a .sql file.
It is supposed to be a 7 kb sql file.

@For /f "tokens=2-4 delims=/ " %%a in ('date /t') do @(set mydate=%%c-%%a-%%b)
@For /f "tokens=1-2 delims=/:" %%a in ('time /t') do @(set mytime=%%a%%b)    

@echo mydate= %mydate%
@echo mytime= %mytime%

mysqldump -u root -p --add-drop-table --create-options --password= onstor >c:\%mydate%_%mytime%.sql

UPDATE
I don't think there's a problem with the mysqldump command since it works well when I do it this way. The code below just uses the date as its filename.

@For /F "tokens=2,3,4 delims=/ " %%A in ('Date /t') do @( 
    Set Month=%%A
    Set Day=%%B
    Set Year=%%C   
)    

@echo DAY = %Day%
@echo Month = %Month%
@echo Year = %Year%    

mysqldump -u root --add-drop-table --create-options --password= onstor >c:\%Day%-%Month%-%Year%.sql

Please help, thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

寻找一个思念的角度 2024-10-15 22:29:15

在 Linux 上,只需输入 $(date +%Y-%m-%d-%H.%M.%S) 即可在文件名中显示日期和时间,如下所示

mysqldump -u <user> -p <database> | bzip2 -c > <backup>$(date +%Y-%m-%d-%H.%M.%S).sql.bz2

:(该命令还使用 bzip2 压缩文件)

On Linux, simply put $(date +%Y-%m-%d-%H.%M.%S) to show date and time in the file name, so it looks like:

mysqldump -u <user> -p <database> | bzip2 -c > <backup>$(date +%Y-%m-%d-%H.%M.%S).sql.bz2

(This command also compresses the file using bzip2)

笛声青案梦长安 2024-10-15 22:29:15

我认为你的 mysqldump 命令的语法是错误的;

mysqldump -u root -p --add-drop-table --create-options --password= onstor

您同时使用 -p--pasword=,您应该只使用一个选项。并且密码前有一个空格。

只需尝试在命令行上运行 mysqldump 命令即可查看错误消息。或者在批处理文件中的命令末尾添加 2>&1 。然后您还会在输出文件中看到错误消息。

mysqldump -u root --add-drop-table --create-options --password=onstor >c:\%mydate%_%mytime%.sql 2>&1

I think the syntax of your mysqldump command is wrong;

mysqldump -u root -p --add-drop-table --create-options --password= onstor

You use both -p and --pasword=, you should only use one option. And there is a space before the password.

Just try to run the mysqldump command on the commandline to see error messages. Alternatively add 2>&1 at the end of the command in the batchfile. Then you would also see error messages in the output file.

mysqldump -u root --add-drop-table --create-options --password=onstor >c:\%mydate%_%mytime%.sql 2>&1
淡紫姑娘! 2024-10-15 22:29:15

对于那些想要让它在 crontab 中工作的人来说,在这种情况下它有点不同:

30 01 * * * /opt/bitnami/mysql/bin/mysqldump -u root --password=MySecretPass database_name | gzip >  /path/backup/`date "+\%d-\%m-\%y_\%H:\%M"`.gz

For those who want to get this to work in crontab, in that case it's a bit different:

30 01 * * * /opt/bitnami/mysql/bin/mysqldump -u root --password=MySecretPass database_name | gzip >  /path/backup/`date "+\%d-\%m-\%y_\%H:\%M"`.gz
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文