Ask HN: Archive a MySQL database
How do you archive a database? I have this database and it has been receiving data for the last 3 years and I would like to archive the old data WITHOUT affecting the current data (current transactions, etc.). The database is heavily used and is close to a million records and on some cases it takes a little longer for the complicated queries to complete...
What is the best way to do this? I have no experience on this. In fact this is the first time I have handled a database with this volume. Perhaps you can direct me to a good resource, etc.
I am using MySQL5 on a LAMP environment. If it requires downtime, I can sacrifice a day for this.
EDIT: If I backup the entire database, I would still have "old" records and the database would still be at the same size.
19 comments
[ 3.7 ms ] story [ 182 ms ] threadGo to the "export" tab and use the "Save as file" option
http://blog.dbadojo.com/2007/09/mysql-backups-using-lvm-snap...
(Haven't done it myself, though.)
http://www.maatkit.org/doc/mk-archiver.html
If you already happen to be using LVM, check out this post: http://www.mysqlperformanceblog.com/2006/08/21/using-lvm-for...
Otherwise, you're going to have some downtime, which could range from a few seconds to a lot longer depending (mostly) on the size of your data. The simplest way is using mysqldump, but that may be too slow (probably not though, unless you have really low-end hardware, 1m records isn't that many).
There are faster (and less straightforward) ways, which basically involve: -stop the server -copy the data directory somewhere else -restart the server
Google is your friend here. If you have a spare server, you might also consider setting up replication so that the next time you want to back up your data, you can take it from a slave and it won't require any downtime.
mysqldump --single-transaction --skip-lock-tables --all-databases | gzip > "/some/directory/backup-`hostname`-hourly-`date +%H`.sql.gz"
These two options are important: --single-transaction and --skip-lock-tables. At least in mysql 5.1, mysqldump defaults to locking each table before dumping it (not good if you have a busy app and large db).
I run the above in a cron once per hour. It gives me 24 hours worth of hourly backups. I then use rsnapshot (http://www.rsnapshot.org/) to store daily, weekly, and monthly backups offsite.
I also run my backups with a low priority (nice -n 19) so that my application gets higher priority than the backup script.
-w, --where=name Dump only selected records; QUOTES mandatory!
You can probably use this to backup just the rows you are interested in and then delete them once you are sure the are properly archived.
You should of course still backup your data since the archive is really just a reference.
Perhaps if I had foreseen this, I would have made my database schema easier to archive. :-(
For example, the Header-to-detail relationship between tables.
Anyway, I will be trying out the Tools found in this link: http://www.maatkit.org/doc/mk-archiver.html