Batch processing for Mysql automatic backup under Windows (copy directory or mysqldump backup) _mysql

Source: Internet
Author: User
Tags modifier rar mysql automatic backup
There is a need today to implement automatic database backup under Windows, piecing together a solution.

Characteristics of the implementation
Multiple databases can be specified
Organize backups on a yearly/month/day basis
Optional use of WinRAR compressed backup
Using scheduled tasks to implement scheduled backups

Specific code
Backup
Copy Code code as follows:

@echo Off & setlocal enableextensions

::----------Configuration Items----------

:: Backup placement path, plus \
Set Backup_path=d:\backup\

:: Database name to back up, multiple spaces separated
Set Databases=database1 database2 database3

:: MySQL User Name
Set Username=root

:: MySQL Password
Set password=123456

:: MySQL Bin directory, plus \
:: If you can use Mysqldump directly (add MySQL Bin directory to environment variable at installation), leave this blank
Set mysql=d:\server\mysql\bin\

:: WinRAR with command-line tools executable path, long file name note in Dos long file name writing method
Set Winrar=c:\progra~1\winrar\rar.exe

::----------Do not modify the following----------

Set year=%date:~0,4%
Set month=%date:~5,2%
Set day=%date:~8,2%
:: If the input time in DOS to return is not 24-hour system (no 0 fill), please modify this here
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
del/f/s/q%%d.%addon%.sql 2>nul
)
)
Echo Done

: Exit

Increase Scheduled Tasks
Copy Code code as follows:

@echo off

::-------Configuration Items-------

:: File name to execute
Set File=d:\cron\cron_backup.bat

:: Scheduling Frequency type
Set frequency=daily

:: Frequency, corresponding to the planned frequency type above
Set Modifier=1

:: Time of execution of the plan (24-hour system)
Set datetime=00:30:00

:: The name of the plan
Set name= "Backup Cron Job"

:: Scheduled to execute user, not recommended for modification
Set user= "System"

::-------Do not modify the following-------

Schtasks/create/ru%user%/sc%frequency%/mo%modifier%/st%datetime%/tn%name%/tr%FILE%
Pause

of which

:: Scheduling frequency type, optional:
:: MINUTE minutes
:: Daily Day
:: Hourly Hours
:: WEEKLY Week
:: 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 to 12, or SECOND, third, fourth, last, Lastday

How to use
Save the two paragraphs above as Cron_backup.bat and Cron_backup_add.bat, and modify the relevant parameters according to the annotations
After you have identified a good directory, run Cron_backup_add.bat to add the scheduled tasks to the system.
Windows Server 2003/xp can start, control Panel, scheduled tasks into the scheduled task management interface;
Windows VISTA/7 can access the planning task management interface through the computer, right key, management, System Tools, Task Scheduler, Task Scheduler Library.
Run the task manually and view the backup directory to confirm that the backup was successful.

Pending improvement
Increase the number of days before you delete a backup function
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.