Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Backup MySQL database

I have a MySQL Database of about 1.7GB. I usually back it up using mysqldump and this takes about 2 minutes. However, I would like to know the answers to the following questions:

  1. Does mysqldump block read and/or write operations to the database? Because in a live scenario, I would not want to block users from using the database while it is being backed up.

  2. It would be ideal for me to only backup the WHOLE database once in, say, a week, but in the intermediate days only one table needs to be backed up as the others won't change. Is there a way to achieve this?

  3. Is mysqlhotcopy a better alternative for these purposes?

like image 414
TMM Avatar asked Aug 18 '09 08:08

TMM


2 Answers

mysqlhotcopy does not work in certain cases where the readlock is lost, and does not work with INNODB tables.

mysqldump is more used because it can back up all kinds of tables.

From MySQL documentation

mysqlhotcopy is a Perl script that was originally written and contributed by Tim Bunce. It uses LOCK TABLES, FLUSH TABLES, and cp or scp to make a database backup quickly. It is the fastest way to make a backup of the database or single tables, but it can be run only on the same machine where the database directories are located. mysqlhotcopy works only for backing up MyISAM and ARCHIVE tables. It runs on Unix and NetWare

The mysqldump client is a backup program originally written by Igor Romanenko. It can be used to dump a database or a collection of databases for backup or transfer to another SQL server (not necessarily a MySQL server). The dump typically contains SQL statements to create the table, populate it, or both. However, mysqldump can also be used to generate files in CSV, other delimited text, or XML format.

Bye.

like image 62
RRUZ Avatar answered Nov 12 '22 18:11

RRUZ


1) mysqldump only blocks when you ask it to (one of the --lock-tables, --lock-all-tables, --single-transaction). but if you want your backup to be consistent then mysqldump should block (using --single-transaction or --lock-all-tables) or you might get an inconsistent database snapshot. Note: --single-transaction works only for InnoDB.

2) sure, just enumerate the tables you want to be backed up after the database name:

mysqldump OPTIONS DATABASE TABLE1 TABLE2 ...

Alternatively you can exclude the tables you don't want:

mysqldump ... --ignore-table=TABLE1 --ignore-table=TABLE2 .. DATABASE

So you can do a whole database dump once a week and backup only the changing tables once a day.

3) mysqlhotcopy inly works on MyISAM tables and in most applications you are better off with InnoDB. There are commercial tools (quite expensive) for hotbackup of innodb tables. Lately there is also the new opensource one for this purpose - Xtrabackup

Also, to automate the process you can use astrails-safe. It supports database backup with mysqldump and filesystem with tar. +encryption +upload to S3, +many other goodies. There is no xtrabackup support yet, but it should be easy to add if this is what you need.

like image 21
Vitaly Kushner Avatar answered Nov 12 '22 18:11

Vitaly Kushner