How can we help?

My Cases

Automate MySQL Backups on Windows
Last Updated: Jun 08, 2015 11:57AM EDT

This article applies to:  Echo360 Admins

Summary

This article describes how to schedule automatic backups of the ESS database in MySQL on Windows using the mysqldump utility.

Relevance

This article is intended for ESS system administrators or database administrators responsible for the maintenance of the ESS database.

Procedure

Create a new batch file (*.bat) using your favorite text editor, and paste in the following text:

@echo off
echo Executing mysqldump...
"C:\Program Files\MySQL\MySQL Server 5.5\bin\mysqldump.exe" -e -q -uusername -pc3p0 -hlocalhost --single-transaction --result-file="%DATE:~10,4%%DATE:~4,2%%DATE:~7,2%.sql" -B database echo Culling old dumps... forfiles /s /m *.sql /d -7 /c "cmd /c del /q @path" 
‚Äčecho Complete.

Line-by-line explanation

  1. Prevents command output from being printed to the console (stdout).
  2. Prints friendly informational text about what we're doing.
  3. Creates a mysqldump output file. Has components:
    1. "C:\Program Files\MySQL\MySQL Server 5.1\bin\mysqldump.exe" — The absolute path to mysqldump.
    2. -e — Use extended insert to reduce output file complexity and size.
    3. -q — Return rows one at a time instead of buffering large tables in memory to complete the operation faster.
    4. -uusername — The username for authenticating to the database server.
    5. -pc3p0 — The password for authenticating to the database server.
    6. -hlocalhost — The address of the database server host.
    7. --single-transaction — Complete the operation in a single transaction so that the output file is consistent and the operation can be completed without locking tables against concurrent use.
    8. --result-file="%DATE:~10,4%%DATE:~4,2%%DATE:~7,2%.sql" — The name of the output file. If no path is specified, it will be created in the working directory of the batch file. This string uses Windows command-line variables to create a file with a date stamp for its name.
    9. -B database — Database names follow. More than one may be listed here, however, they will all be contained in the same output file.
  4. Prints more friendly informational text about what we're doing.
  5. Deletes output files older than 7 days. Has components:
    1. forfiles — The Windows command used to execute a batch operation on multiple files.
    2. /s — Search into subdirectories recursively.
    3. /m *.sql — Match only files of the pattern *.sql.
    4. /d -7 — Match only files older than 7 days.
    5. /c "cmd /c del /q @path" — Execute the quoted command. Has components:
      1. cmd /c del — Invoke the Windows command shell to run the delete command.
      2. /q — Do not prompt for delete confirmation.
      3. @path — Select the file matched by the forfiles command.
  6. Prints even more friendly information to indicate we're done.‚Äč

Go to Start -> Administrative Tools -> Task Scheduler to create a scheduled task for executing the batch file on a nightly basis:

  1. In the Action pane, select Create Basic Task...
  2. Give your task a name like "Database Backup" and click Next.
  3. Trigger the task on a daily basis and click Next.
  4. Start the task beginning "today" to occur at 2:00:00 AM (or some other convenient low-load time), every 1 days. Click Next.
  5. Set the action to start a program. Click Next.
  6. Use the Browse... button to locate your batch file or enter its absolute path.
  7. Populate the Start in field with the directory in which you want the database backups to be created (e.g., the directory containing the batch file). Click Next, then click Finish.
c9f5f1d87ac29bd0c146e9565da3c739@echo360.desk-mail.com
https://cdn.desk.com/
false
desk
Loading
seconds ago
a minute ago
minutes ago
an hour ago
hours ago
a day ago
days ago
about
false
Invalid characters found
/customer/en/portal/articles/autocomplete