docs:mysql:mysqldump

This is an old revision of the document!


mysqldump

text-based client for dumping or backing up mysql databases, tables, and or data.

mysqldump [OPTIONS] database [tables]
OR mysqldump [OPTIONS] --databases [OPTIONS] DB1 [DB2 DB3...]
OR mysqldump [OPTIONS] --add-databases [OPTIONS]

The most normal use of mysqldump is probably for making a backup of whole databases. See Mysql Manual section 21.2 Database Backups.

mysqldump --opt database > backup-file.sql

Note: --opt is the same as: --add-drop-table --add-locks --all --extended-insert --quick --lock-tables

Most likely, you will need to provide user credentials:

mysqldump --opt -u user -p database > backup-file.sql

You can read this back into MySQL with:

mysql database < backup-file.sql

or

mysql -e 'source /path-to-backup/backup-file.sql' database

More options and examples are available in the man page and in the MySQL manual

#!/bin/bash

# user must have select and lock tables privileges

destination='/path-to-dest-dir/MySQL Backups/'

databases="database1
database2
database3"

user=username
pass=password

# make the destination folder, if it doesn't exist
if [ ! -d "$destination" ]; then
	mkdir -p "$destination"
fi

# continue if the directory was successfully created
if [ -d "$destination" ]; then
	for db in $databases
	do
		echo "$db..."
		mysqldump --opt -u$user -p$pass "$db" > "$destination$db".sql
	done
fi
exit 0
  • docs/mysql/mysqldump.1186687558.txt.gz
  • Last modified: 2008/08/03 00:25
  • (external edit)