计算机锁定时批处理文件 MySQLdump 出错
我试图每天晚上午夜运行一个批处理文件(通过任务计划程序),以便生成我的 mysql 数据库的备份(使用 mysqldump)、加密(使用 7-zip)并移动到适当的文件夹。我修改了在以下位置找到的代码版本:https: //sqlbackupandftp.com/blog/how-to-automate-mysql-database-backups-in-windows。以下是我的修改版本:(
rem credentials to connect to mysql server
set mysql_user=***********
set mysql_password=***********
rem archive password
set encryption_password=***********
rem backup storage period (in days)
set max_keep_days=7
rem path to backup compression utility
set seven_zip_path=C:\Program Files\7-Zip\
rem backup file name generation
set backup_name=C:\Users\**********\Documents\Temp\all-databases-%DATE:~-4%-%DATE:~4,2%-%DATE:~7,2%-%time::=.%
rem backup creation
mysqldump --user %mysql_user% --password=%mysql_password% --all-databases >> %backup_name%.sql
if %ERRORLEVEL% neq 0 eventcreate /ID 1 /L APPLICATION /T ERROR /SO mysql-backup-script /D "Backup failed: error during dump creation" && exit
rem backup compression
"%seven_zip_path%7z" a -p%encryption_password% %backup_name%.zip %backup_name%.sql
if %ERRORLEVEL% neq 0 eventcreate /ID 1 /L APPLICATION /T ERROR /SO mysql-backup-script /D " Backup failed: error during archive creation" && exit
rem delete temporary .sql file
del %backup_name%.sql
rem copy backup to storage
robocopy C:\Users\**********\Documents\Temp\ C:\Users\**********\Desktop\Backups\ /e
rem local backup copy
del %backup_name%.zip
eventcreate /ID 1 /L APPLICATION /T INFORMATION /SO mysql-backup-script /D "Backup successful"
我删除了网络路径部分,因为我首先尝试集中精力使其在本地运行,然后再重新合并这些部分。此外,backup_name变量在我使用完整路径之前不起作用)
当我自己运行此代码时,在登录等情况下,此代码确实可以正常运行。但是,如果我锁定电脑,就像我昨晚所做的那样,文件会运行,但一旦到达 mysqldump 行就会失败。
I am attempting to have a batch file run every night at midnight (via task scheduler) so that a backup of my mysql db is generated (using mysqldump), encrypted (using 7-zip), and moved to the appropriate folder. I modified a version of the code found at: https://sqlbackupandftp.com/blog/how-to-automate-mysql-database-backups-in-windows. The following is my modified version:
rem credentials to connect to mysql server
set mysql_user=***********
set mysql_password=***********
rem archive password
set encryption_password=***********
rem backup storage period (in days)
set max_keep_days=7
rem path to backup compression utility
set seven_zip_path=C:\Program Files\7-Zip\
rem backup file name generation
set backup_name=C:\Users\**********\Documents\Temp\all-databases-%DATE:~-4%-%DATE:~4,2%-%DATE:~7,2%-%time::=.%
rem backup creation
mysqldump --user %mysql_user% --password=%mysql_password% --all-databases >> %backup_name%.sql
if %ERRORLEVEL% neq 0 eventcreate /ID 1 /L APPLICATION /T ERROR /SO mysql-backup-script /D "Backup failed: error during dump creation" && exit
rem backup compression
"%seven_zip_path%7z" a -p%encryption_password% %backup_name%.zip %backup_name%.sql
if %ERRORLEVEL% neq 0 eventcreate /ID 1 /L APPLICATION /T ERROR /SO mysql-backup-script /D " Backup failed: error during archive creation" && exit
rem delete temporary .sql file
del %backup_name%.sql
rem copy backup to storage
robocopy C:\Users\**********\Documents\Temp\ C:\Users\**********\Desktop\Backups\ /e
rem local backup copy
del %backup_name%.zip
eventcreate /ID 1 /L APPLICATION /T INFORMATION /SO mysql-backup-script /D "Backup successful"
(I removed the network path sections, as I am first trying to focus on getting this to run locally before re-incorporating those sections. Also, the backup_name variable didn't work until I used a full path)
This code does properly run when I run this myself, while logged in, etc. However, if I lock the pc, as I did last night, the file ran but failed once it reached the mysqldump line.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当时间在中午 12 点之后时,%TIME% 变量的包含起作用,但是在午夜之后和早上,文件名中突然出现一个额外的空格,从而导致错误。
(有类似问题的朋友,请看Mofi的回复,他们会认真帮助你的)
The inclusion of %TIME% variable worked when the time was after 12pm, but after midnight and in the morning there suddenly was an extra space in the file name which caused an error.
(to those who are having similar issues, please see Mofi's replies as they will seriously help you out)