Batch File Example Code To Backup MySQL Database To A Particular Folder, Append Date and Time, Delete Backups Older Than X Days

If we very with out problems favor to output an sql record utilizing sqldump command we can very with out problems write a sqldump command akin to the one built in a textual content record and put it aside as a batch record. That will work.
Example:
Suppose, username = root, password = root, server = localhost, database title = dbdemo

Suppose you love to create the batch record and supply it to other customers to apply it. And you love the directory in which it is shop to be the identical. And you furthermore favor delete MySQL backups which can be older than "X" days. So,
Let's unravel if a directory already exists. And if now not, create one. Let's append date and time to MySQL backup place of job work. Let's unravel if there are MySQL backups older than X days and delete them. Show the MySQL backup folder to the user on completion Algorithm:
Check if a directory called C:\MYSQLBACKUPS already exits. If no: Create. And visit step 2 If certain: Continue to step 2 Define the date and time format Execute mysqldump command Check and delete place of job work older than 60 days Show the backup folder to the user conclusion The code for the above mysqldump algorithm to unravel for folder, append date and time to sql record title and delete place of job work older than 60 days is as given lots diminish than.In the batch record code lots diminish than:
REM = Comments. Does now not occur on the screen
ECHO. = New Line@echo off
REM ----------------------------------------------------------------
REM Create a directory to buy mysql backup place of job work if now not already exists REM ----------------------------------------------------------------IF NOT EXIST "C:\MYSQLBACKUPS" mkdir C:\MYSQLBACKUPSREM ----------------------------------------------------------------
REM append date and time to mysqldump place of job work
REM ----------------------------------------------------------------SET dt=%date:~-4%_%date:~3,2%_%date:~0,2%_%time:~0,2%_%time:~3,2%_%time:~6,2%



set bkupfilename=%dt%.sqlREM ----------------------------------------------------------------
REM Display practically a message on the screen linked to the backup
REM ----------------------------------------------------------------
ECHO Starting Backup of MySQL Database
ECHO Backup is going to buy in C:\MYSQLBACKUPS\ folder.
ECHO Please wait ...REM ----------------------------------------------------------------
REM mysqldump backup command. append date and time in filename
REM ----------------------------------------------------------------"C:\Program Files\MySQL\MySQL Server 5.5\bin\mysqldump.exe" --routines -u root -proot dbdemo > C:\MYSQLBACKUPS\"mysqldb_%bkupfilename%"REM ----------------------------------------------------------------
REM delete mysqldump backups older than 60 days
REM ----------------------------------------------------------------ECHO.
ECHO Trying to find out and delete backups older than 90 days if realized.
ECHO And the quit result is:
forfiles /p C:\MYSQLBACKUPS /s /m *.* /d -3 /c "cmd /c del @record : date >= 60days"ECHO.
ECHO Backup comprehensive!
ECHO Backup saved in C:\MYSQLBACKUPS\
ECHO Thank You for backing up!
ECHO - Regards, Admin!
ECHO.
ECHO I am practically to divulge you the backup place of job work.PAUSEREM Show user the backup place of job work
EXPLORER C:\MYSQLBACKUPS\
EXITREM Author: JK
REM Kohima, Nagaland
REM Modified: OCtober 7, 2016 Copy the above code to a notepad, and put it aside as "anyname.bat".
Run it.Note: This is an instance code. Review the code your self and run it at your very own chance. The author shall now not be guilty for any trouble delivered about by running this code if any.

In this submit we will see the appropriate methodology to make a batch record to take backup of MySQL database (single database) to a special folder on the identical laptop. As that you just could also already know, there are a range of variants of mysqldump command. But for this workout we will use this command:

Then

Sponsored Links

Save as "mysqlbackup.bat"
Run it
The backup will be exported to C Drive

Let's elevate the above batch record to add greater sources!
MYSQLDUMP Batch command to buy to a special folder, append date and time, delete older mysqldump place of job work, open backup folder

mysqldump --routines -u -p >
Simple Batch File to backup MySQL Database:

Open Notepad or any plain textual content editor
Type within the command:

mysqldump --routines -uroot -proot dbdemo > C:\mysqlbackup.sql
Batch File Example

Image source: http://technico.qnownow.com/files/2012/07/2.jpg

Labels: Blogging Tips

Thanks for reading Batch File Example Code To Backup MySQL Database To A Particular Folder, Append Date and Time, Delete Backups Older Than X Days. Please share...!

0 Comment for "Batch File Example Code To Backup MySQL Database To A Particular Folder, Append Date and Time, Delete Backups Older Than X Days"

Back To Top