Batch of copy directory or mysqldump backup for MySQL automatic backup under Windows
- 2020-05-13 03:36:02
- OfStack
Today, I have a requirement to implement automatic database backup under Windows, and I have pieced together a solution.
Implemented features
You can specify multiple databases
Organize backups by year/month/day
Optionally, use WinRAR to compress the backup
Implement scheduled backups using scheduled tasks
Specific code
The backup
Add planned tasks
One of the
:: planned frequency type, optional:
: : MINUTE minutes
: : DAILY days
: : HOURLY hours
: : WEEKLY weeks
: : MONTHLY month
:: frequency, corresponding to the planned frequency type above
:: MINUTE: 1 to 1439 minutes
:: HOURLY: 1-23 hours
:: DAILY: 1 to 365 days
:: WEEKLY: 1 to 52 weeks
:: MONTHLY: 1 through 12, or FIRST, SECOND, THIRD, FOURTH, LAST, LASTDAY
Method of use
Save the above two pieces of code as cron_backup.bat and cron_backup_add.bat, respectively, and modify the relevant parameters according to the comments
Once the directory is determined, run cron_backup_add.bat to add the scheduled task to the system.
Windows Server 2003 / XP can enter the planning task management interface through the start, control panel and planning task;
Windows Vista / 7 can be accessed through computer, right click, management, system tools, task scheduler, task scheduler library, and plan task management interface.
Run the task manually and check the backup directory to see if the backup was successful.
To improve
Added the ability to delete backups several days before
Implemented features
You can specify multiple databases
Organize backups by year/month/day
Optionally, use WinRAR to compress the backup
Implement scheduled backups using scheduled tasks
Specific code
The backup
@echo off & setlocal ENABLEEXTENSIONS
:: ---------- Configuration items ----------
:: Backup placement path, plus \
set BACKUP_PATH=D:\Backup\
:: The name of the database to be backed up, multiple separated by Spaces
set DATABASES=database1 database2 database3
:: MySQL The user name
set USERNAME=root
:: MySQL password
set PASSWORD=123456
:: MySQL Bin Directory, \
:: If you can use it directly mysqldump (added at installation MySQL Bin The directory goes to the environment variable), which is left blank
set MYSQL=D:\SERVER\MySQL\bin\
:: WinRAR Command line tool with the executable file path, long file name note Dos Long file name writing
set WINRAR=C:\Progra~1\WinRAR\Rar.exe
:: ---------- Please do not modify ----------
set YEAR=%date:~0,4%
set MONTH=%date:~5,2%
set DAY=%date:~8,2%
:: If the dos The input time It doesn't return 24 By the hour (no 0 Fill), please modify this by yourself
set HOUR=%time:~0,2%
set MINUTE=%time:~3,2%
set SECOND=%time:~6,2%
set DIR=%BACKUP_PATH%%YEAR%\%MONTH%\%DAY%\
set ADDON=%YEAR%%MONTH%%DAY%%HOUR%%MINUTE%%SECOND%
:: create dir
if not exist %DIR% (
mkdir %DIR% 2>nul
)
if not exist %DIR% (
echo Backup path: %DIR% not exists, create dir failed.
goto exit
)
cd /d %DIR%
:: backup
echo Start dump databases...
for %%D in (%DATABASES%) do (
echo Dumping database %%D ...
%MYSQL%mysqldump -u%USERNAME% -p%PASSWORD% %%D > %%D.%ADDON%.sql 2>nul
:: winrar
if exist %WINRAR% (
%WINRAR% a -k -r -s -m1 -ep1 %%D.%ADDON%.rar %%D.%ADDON%.sql 2>nul
del /F /S /Q %%D.%ADDON%.sql 2>nul
)
)
echo Done
:exit
Add planned tasks
@echo off
:: ------- Configuration items -------
:: The file name to execute
set FILE=D:\cron\cron_backup.bat
:: Planned frequency type
set FREQUENCY=DAILY
:: Frequency, corresponding to the planned frequency type above
set MODIFIER=1
:: Implementation time of the scheme ( 24 Hourly)
set DATETIME=00:30:00
:: The name of the plan
set NAME="Backup Cron Job"
:: Plan to execute user, do not recommend modification
set USER="System"
:: ------- Please do not modify -------
schtasks /Create /RU %USER% /SC %FREQUENCY% /MO %MODIFIER% /ST %DATETIME% /TN %NAME% /TR %FILE%
pause
One of the
:: planned frequency type, optional:
: : MINUTE minutes
: : DAILY days
: : HOURLY hours
: : WEEKLY weeks
: : MONTHLY month
:: frequency, corresponding to the planned frequency type above
:: MINUTE: 1 to 1439 minutes
:: HOURLY: 1-23 hours
:: DAILY: 1 to 365 days
:: WEEKLY: 1 to 52 weeks
:: MONTHLY: 1 through 12, or FIRST, SECOND, THIRD, FOURTH, LAST, LASTDAY
Method of use
Save the above two pieces of code as cron_backup.bat and cron_backup_add.bat, respectively, and modify the relevant parameters according to the comments
Once the directory is determined, run cron_backup_add.bat to add the scheduled task to the system.
Windows Server 2003 / XP can enter the planning task management interface through the start, control panel and planning task;
Windows Vista / 7 can be accessed through computer, right click, management, system tools, task scheduler, task scheduler library, and plan task management interface.
Run the task manually and check the backup directory to see if the backup was successful.
To improve
Added the ability to delete backups several days before