使用日期和时间作为文件名生成备份文件
我该如何正确地做到这一点。我试图将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 Linux 上,只需输入
$(date +%Y-%m-%d-%H.%M.%S)
即可在文件名中显示日期和时间,如下所示:(该命令还使用 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:(This command also compresses the file using bzip2)
我认为你的 mysqldump 命令的语法是错误的;
您同时使用
-p
和--pasword=
,您应该只使用一个选项。并且密码前有一个空格。只需尝试在命令行上运行 mysqldump 命令即可查看错误消息。或者在批处理文件中的命令末尾添加
2>&1
。然后您还会在输出文件中看到错误消息。I think the syntax of your mysqldump command is wrong;
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.对于那些想要让它在 crontab 中工作的人来说,在这种情况下它有点不同:
For those who want to get this to work in crontab, in that case it's a bit different: