Sizzly
01-21-2005, 03:25 AM
What's the best way to backup a large MySQL database?
I've tried things like CPanel, DirectAdmin, phpMyAdmin, backup functions in the scripts like phpBB - all are very slow or time out.
Is there something I should do beforehand - such as optimize?
There are some scripts (PHP) that can optimize the database, but they're slow and sometimes time out.
How can I do this over SSH?
MySQL server is on the same machine as the web server (localhost).
Thanks
John[H4Y]
01-21-2005, 03:41 AM
If you have root access, the answer is simple.
1) Shut down the mysql server:
As root, type "service mysql stop", or "service mysqld stop", or "/etc/rc.d/init.d/mysql stop" or "/etc/rc.d/init.d/mysqld stop", or "/etc/init.d/mysql stop", or "/etc/init.d/mysqld stop"... whatever works and doesn't give you an error. It depends on how MySQL was installed and varies by distribution and/or control panel that you use.
2) Type "cd /var/lib/mysql/" (this is generally where your MySQL data directory is located, at least on most common setups - other places to try would be /usr/local/mysql/data or /var/lib/mysql/data). If you type "ls" you should see the directory that corresponds to the database you want to backup.
3) Type "tar -cvzf /root/mydatabasebackup.tgz databasename/*" (replace /root/mydatabasenamebackup.tgz with another path and/or filename if you prefer and replace "databasename" with your actual database name of course).
4) Start the MySQL server again - use the same command you did in Step #1 but replace "stop" with "start".
You can also (only safe if MySQL server is shut down again) quickly transfer the files via ftp or better yet, SSH by typing:
scp /var/lib/mysql/data/databasename/* root@anotherserverwithsshd:/path/to/backup/location/
Replace "anotherserverwithsshd" with a valid host name or IP address of your preferred backup server. This will work assuming your database server has scp installed (likely) and the backup server allows incoming SSH connections. scp is a very useful utility!
Kevin Phair
01-21-2005, 10:58 AM
If you have shell access which you seem to be saying that you do, you are probably best of using mysqldump. This doesn't require a shutdown of the mysql server, and (usually) doesn't require root access either.
The MySQL Folks (http://dev.mysql.com/doc/mysql/en/mysqldump.html) give much better instructions then I could come up with (make sure read the bit pertaining to large databases and older versions of MySQL).