How to prevent directory contents from changing while doing a backup?

0

I have a web-application that stores data into a MySQL database and on the filesystem. Some of the database records reference some of the files on the filesystem.

Now I want to create a snapshot of the whole lot. Of course, I want to make sure that aftwards, all files referenced in the database snapshot are present in the filesystem snapshot. And all files in the filesystem snapshot are properly referenced in the database snapshot.

If I would run mysqldump first and then tar up the files (or the other way around), there is a certain race condition. On top of that, I doubt anything is stopping other processes from changing files while I run my backup.

My first thought was to simply chmod 444 the directory before starting the backup process. But I'm not sure about all the negative implications that would bring to a live system (I can imagine a few).

Obviously, in this case, I could simply shut down Apache and, thus, prevent any changes while I do my backup. But shutting off the web server isn't the most elegant solution either.

Der Hochstapler

Posted 2012-02-22T12:03:41.750

Reputation: 77 228

Answers

1

If you store the files inside an LVM volume, you could lock the database and then both do an mysqldump and make an LVM snapshot, and finally unlock the database. Then backup from the LVM snapshot. In that way, you'd have synchronized DB and FS contents with minimal locking time.

Jaap Eldering

Posted 2012-02-22T12:03:41.750

Reputation: 7 596

Yeah, LVM would be awesome. I was wondering if this problem could be solved from the "shell level". – Der Hochstapler – 2012-02-22T12:27:59.517

1No, you'll need some sort of transaction to make a point-in-time snapshot of the filesystem. Whether that comes from LVM, btrfs, ZFS, VSS (on Windows), or something completely different -- it often relies on OS or filesystem-level support. There's no hacky shell tricks you can pull that involve keeping the site online otherwise, unless you rearchitect the entire site to allow for backup windows where all writes go to a staging area until the backup completes, then the site migrates all those temporary writes back into the main storage. – afrazier – 2012-02-22T14:08:29.553

To add to your answer. There are existing methods to achieve what I want. These methods exist in the form of LVM. There is no simpler approach and if you want to do it properly, do it properly and don't try to hack it :P – Der Hochstapler – 2012-05-03T12:22:20.363