Back up MySQL server

1) Yes, that will dump the contents of all databases as a series of INSERT sql commands so you can restore the whole lot with mysql < backup.sql. Purely for wieldability's sake, I'd recommend you do this per database rather than using --all-databases as you get one honking big file containing EVERYTHING and it's not easily searchable.

You may want to look at the mysqldump manual page to see if there are any other options you'd like to use.

2) User information and access control is stored in the mysql database; --all-databases does include that one.


If you are running mysqldump remotely and want a separate dump file for each database, you will have to maintain a list of the databases you want to back up. If you run mysqldump locally on the database server, you can loop through the databases that exist in the mysql data directory (such as /var/lib/mysql). If mysqldump has to be run remotely, you could write a script that connects via SSH, retrieves a list of the databases, then disconnects and runs mysqldump with the database names.

I would not use --all-databases without specifying individual database names and would additionally use the option --add-drop-database. This will also include SQL statements in the dump file to create the database if it doesn't exist, or drop the database if it does exist and recreate it. From man 1 mysqldump:

--add-drop-database
  Add a DROP DATABASE statement before each CREATE DATABASE statement.      
  This option is typically used in conjunction with the --all-databases 
  or --databases option because no CREATE DATABASE statements are written
  unless one of those options is specified.

This is a quick and dirty script designed to run locally on the database server that will backup the databases specified in the $DB variable resulting in a dump file for each database. It also removes any previous dumps files that are older than 15 days.

#!/bin/sh

PATH=/bin:/usr/bin
DATE="`date +%Y%m%d`"
DB="mysql drupal drupaldev"
DUMP_DIR="/u01/mysql_dump"

for db in `echo $DB`
 do
  mysqldump -u root --password=password --databases --add-drop-database $db > $DUMP_DIR/$db.$DATE.dmp
  gzip -9 $DUMP_DIR/$db.$DATE.dmp
  find $DUMP_DIR/* -mtime +14 -exec rm -f '{}' \;
done

Tags:

Mysql

Backup