MySQL单个数据库备份:
mysqldump --force --opt --single-transaction --lock-tables=false --skip-events --user=root --password=xxxx --routines --databases testdb > singledb.sql
MySQL所有数据库备份:
mysqldump --force --opt --single-transaction --lock-tables=false --skip-events --user=root --password=xxxx --routines --all-databases > all_databases.sql
MySQL数据库还原:
mysql -u root -predhat dbname < /mnt/dbname.sql
MySQL数据库备份的Shell脚本:
# mkdir /backups/db_backup/
# vi /backups/mysqlbackup.sh
#!/bin/bash
export path1=/backups/db_backup
date1=`date +%y%m%d_%H%M%S`
/usr/bin/find /backups/db_backup/* -type d -mtime +30 -exec rm -r {} \; 2> /dev/null
cd $path1/
mkdir $date1
USER="root"
PASSWORD="redhat123"
OUTPUTDIR="$path1/$date1"
MYSQLDUMP="/usr/bin/mysqldump"
MYSQL="/usr/bin/mysql"
HOST="localhost"
databases=`$MYSQL --user=$USER --password=$PASSWORD --host=$HOST \
-e "SHOW DATABASES;" | tr -d "| " | grep -v Database`
echo "` for db in $databases; do
echo $db
if [ "$db" = "performance_schema" ] ; then
$MYSQLDUMP --force --opt --single-transaction --lock-tables=false --skip-events --user=$USER --password=$PASSWORD --host=$HOST --routines \
--databases $db | gzip > "$OUTPUTDIR/$db.gz"
else
$MYSQLDUMP --force --opt --single-transaction --lock-tables=false --events --user=$USER --password=$PASSWORD --host=$HOST --routines \
--databases $db | gzip > "$OUTPUTDIR/$db.gz"
fi
done `"
:wq
在crontab里配置调度:
#The script will run every night at 12 A.M
#crontab -e
0 0 * * * /backups/mysqlbackup.sh > /dev/null
本文出自 “SQL Server Deep Dive” 博客,请务必保留此出处http://ultrasql.blog.51cto.com/9591438/1924916