How to Backup MySQL databases without interrupting MySQL?

Normally when we create a backup of our MySQL using Mysqldump it creates a Read Lock on the database, so no changes can be made to the database at that time. It may take from a second to few minutes to take up backup of your database. So, sysadmin either prefer to stop MySQL or issue a READ lock on the MySQL table, so that database remains consistent. In this howto, i will demonstrate a method so that you can take up backup of your database without interrupting MySQL.

In this howto i use a method that i discussed in my previous howto how to replicate your MySQL database on Linux. Now once we created a slave MySQL server you can create a cronjob that can create a take regular backups of your database using shell script.

Key Idea: We will stop the slave server and then take the backup of the server. Once backup is done we will start the slave server again and it will catch up with the mysql master server and no data will be lost.

We will create a shell script that will stop the mysql slave, and then take a dump of that slave server and after that is completed again start the slave server.

#!/bin/sh

date = `date +%Y-%h-%d-%H`

mysqladmin --user=root --password=rootpassword stop-slave

mysqldump --user=root --password=rootpassword dbname > ~/backupsql/backup_dbname-${date}.sql

mysqladmin --user=root --password=rootpassword start-slave

Give the shell script executable permission.

[root]# chmod +x mysqlbackup.sh

Add this on cron:

[root]$ crontab -e

0 */6 * * *  /path/to/shellscript/mysqlbackup.sh > /dev/null 2>&1

Read this for more details on How to set a cron job.

This was one of the method that i tested out for creating backup. This works great if your database updates very frequently or you need frequent distributed backups.

4 Comments

Anon Linuxer (not verified)
May 14th, 2010 01:33 am
Hi, If your tables are in innodb you can use the --single-transaction option. You can do your dump with your slave active with this. --single-transaction Creates a consistent snapshot by dumping all tables in a single transaction. Works ONLY for tables stored in storage engines which support multiversioning (currently only InnoDB does); the dump is NOT guaranteed to be consistent for other storage engines. Option automatically turns off --lock-tables.
Arul (not verified)
June 9th, 2010 02:10 pm
i think this is path/to/shellscript/mysqlbackup.sql change to like this path/to/shellscript/mysqlbackup.sh thanks for the great blog.. i mentioned about your blog in my twitter http://twitter.com/arulraj1985
June 11th, 2010 02:21 am

Thanks, its fixed :)

likeuclinux (not verified)
August 25th, 2010 04:59 am
in one scenario: two table updated in master; master sync a table update to slave at this time, your cronjob is running, which will stop slave, and create backup then this backup will only contain one table update then after your backup, the slave will get another table update then disaster come, both your master and slave db are gone. now you only have one backup to recover the database, thus you will lost one table update

Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <img> <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <blockquote> <h1> <h2> <h3> <h4> <h5> <h6> <p> <br>
  • Image links with 'rel="lightbox"' in the <a> tag will appear in a Lightbox when clicked on.
  • Search Engines will index and follow ONLY links to allowed domains.

More information about formatting options

Mollom CAPTCHA (play audio CAPTCHA)
Type the characters you see in the picture above; if you can't read them, submit the form and a new image will be generated. Not case sensitive.