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/mkdirdate1
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;doechodb
if [ "db"="performanceschema"];thenMYSQLDUMP --force --opt --single-transaction --lock-tables=false --skip-events --user=USER−−password=PASSWORD --host=HOST−−routines −−databasesdb | 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
本文转自UltraSQL51CTO博客,原文链接:http://blog.51cto.com/ultrasql/1924916 ,如需转载请自行联系原作者