Tac2
August 19th, 2012, 11:01 AM
Hi everyone.
I recently setup a Ubuntu 12.04 LAMP Server at home that I use to develop websites in corporation with clients. I've been using Ubuntu and other Linux distros for a few years, but I am new to servers and have been learning a lot. Especially about MySQL as I know nothing about it.
I am currently in the process of refining/rethinking my backup procedure, and I am having a problem figuring out how to backup the MySQL database, and I hope you can help me.
Both the MySQL data directory (/var/lib/mysql) and Apache root directory (/var/apache2) reside on /var, which is a LVM Logical Volume. What I want to do is take a snapshot of /var, mount this snapshot in another directory and use rsnapshot to backup /var/lib/mysql and /var/apache2 (as well as /var/log, etc). The snapshot should insure that the MySQL data directory and Apache root directory are in sync.
However, the backup of /var/lib/mysql is not going to do me any good unless the database has written everything to disk. I use the default Ubuntu implementation of MySQL which means it uses InnoDB as storage engine. In some places*, people say that it is safe to simply copy the /var/lib/mysql directory (just need to flush tables with read lock) as InnoDB always keeps the files in a consistent state. In other places**, people say that it is only safe to copy the files if mysqld is completely stopped.
* http://forums.mysql.com/read.php?22,184681,184700#msg-184700
** http://www.pythian.com/news/22185/mysql-backup-concepts-for-linux-system-administrators-part-1/
Currently I am following the last advice (just to be sure). I flush the tables, stop mysqld, take the LVM snapshot and start mysqld again (using the command: mysqladmin -uroot -ppassword flush-tables && service mysql stop && lvcreate -L 1G -s -n $SNAP_NAME $SNAP_SOURCE && service mysql start ; ). I then mount the snapshot in a different directory, take a backup of /var/lib/mysql, /var/apache2 and others using rsnapshot, after which I unmount and remove the LVM snapshot. This works fine for taking snapshots at 5 AM when no one is working on their sites.
However, I would really like to be able to take these snapshots every 1 or 2 hours during working hours, but that means I can't stop mysqld.
So my first question is: Will the following procedure create a consistent backup without interrupting service?
# mysql -uroot -ppassword << EOF
> FLUSH TABLES WITH READ LOCK;
> \! lvcreate -L 1G -s -n $SNAP_NAME $SNAP_SOURCE ;
> UNLOCK TABLES;
EOF
mount LVM snapshot
backup files from LVM snapshot using rsnapshot
unmount and remove LVM snapshot
If yes, my next line of questions then become: Once I have a backup of the files, how do I restore it?
I'm thinking the best idea is to spawn a second mysqld instance which loads its data from the backup, and perform a mysqldump from this second mysqld, selecting the specific databases/tables I want to restore. Would that work? Is it possible?
And lastly - in case the entire MySQL database blows up, can I simply stop mysqld, copy over the contents of /var/lib/mysql from my backup, and start mysqld again?
Any tips will be appreciated! Thank you.
I recently setup a Ubuntu 12.04 LAMP Server at home that I use to develop websites in corporation with clients. I've been using Ubuntu and other Linux distros for a few years, but I am new to servers and have been learning a lot. Especially about MySQL as I know nothing about it.
I am currently in the process of refining/rethinking my backup procedure, and I am having a problem figuring out how to backup the MySQL database, and I hope you can help me.
Both the MySQL data directory (/var/lib/mysql) and Apache root directory (/var/apache2) reside on /var, which is a LVM Logical Volume. What I want to do is take a snapshot of /var, mount this snapshot in another directory and use rsnapshot to backup /var/lib/mysql and /var/apache2 (as well as /var/log, etc). The snapshot should insure that the MySQL data directory and Apache root directory are in sync.
However, the backup of /var/lib/mysql is not going to do me any good unless the database has written everything to disk. I use the default Ubuntu implementation of MySQL which means it uses InnoDB as storage engine. In some places*, people say that it is safe to simply copy the /var/lib/mysql directory (just need to flush tables with read lock) as InnoDB always keeps the files in a consistent state. In other places**, people say that it is only safe to copy the files if mysqld is completely stopped.
* http://forums.mysql.com/read.php?22,184681,184700#msg-184700
** http://www.pythian.com/news/22185/mysql-backup-concepts-for-linux-system-administrators-part-1/
Currently I am following the last advice (just to be sure). I flush the tables, stop mysqld, take the LVM snapshot and start mysqld again (using the command: mysqladmin -uroot -ppassword flush-tables && service mysql stop && lvcreate -L 1G -s -n $SNAP_NAME $SNAP_SOURCE && service mysql start ; ). I then mount the snapshot in a different directory, take a backup of /var/lib/mysql, /var/apache2 and others using rsnapshot, after which I unmount and remove the LVM snapshot. This works fine for taking snapshots at 5 AM when no one is working on their sites.
However, I would really like to be able to take these snapshots every 1 or 2 hours during working hours, but that means I can't stop mysqld.
So my first question is: Will the following procedure create a consistent backup without interrupting service?
# mysql -uroot -ppassword << EOF
> FLUSH TABLES WITH READ LOCK;
> \! lvcreate -L 1G -s -n $SNAP_NAME $SNAP_SOURCE ;
> UNLOCK TABLES;
EOF
mount LVM snapshot
backup files from LVM snapshot using rsnapshot
unmount and remove LVM snapshot
If yes, my next line of questions then become: Once I have a backup of the files, how do I restore it?
I'm thinking the best idea is to spawn a second mysqld instance which loads its data from the backup, and perform a mysqldump from this second mysqld, selecting the specific databases/tables I want to restore. Would that work? Is it possible?
And lastly - in case the entire MySQL database blows up, can I simply stop mysqld, copy over the contents of /var/lib/mysql from my backup, and start mysqld again?
Any tips will be appreciated! Thank you.