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.