Frequently Asked Questions

The NearlyFreeSpeech.NET FAQ (*)

MySQL (*)

Q. How can I make automatic backups of MySQL?

Ordinarily, we prohibit unattended access to our systems. However, there's an exception for backups, and this definitely qualifies.

There are basically three parts:

  1. Something on your local computer to schedule/run the backups. On a Unix system or Mac, this would be "cron." Windows has a way to do the same thing, but we are not familiar with it. So you set this up to run a task every time you want the backup to happen. (E.g. once a week.)

  2. Something to connect to our system and cause the backups to happen. This is a programmable or command line ssh client.

  3. Something on our side to do the backups from our shell. This is the "mysqldump" command line utility. This is typically invoked like:

    Run from our system:

    YourPrompt$ mysqldump --user=youradminusername --password=yourpassword --host=yourprocess.db

    This command spews the backup directly to standard output, so you generally want to send it somewhere after that. If you happened to be using a Unix machine on your side and the OpenSSH client, you could do something like:

    Run from your system:

    YourPrompt$ ssh mysqldump --user=youradminusername --password=yourpassword --host=yourprocess.db | gzip >backup-yourprocess-20120627.sql.gz

That would create a compressed SQL file on your system containing a full backup of your MySQL process on our system. To do this in an unattended way, you would need an ssh public key installed on our system that has no passphrase on the private key on your local machine.

Since the MySQL password you're using will be in the above command, we recommend creating a separate MySQL user with the minimum privileges necessary (i.e. SELECT) to do backups. You could do that from phpMyAdmin or with SQL similar to:

MySQL> CREATE USER backup IDENTIFIED BY 'correct-horse-battery-staple';

Then use backup and correct-horse-battery-staple (being sure to use a fresh random password and not this thoroughly-popularized example) in lieu of your normal MySQL username and password in the mysqldump commands above.

This will take some (or a lot of) tweaking depending on what you've got on your end, but that's the gist of how it can work.