参考以下网页:
http://blog.csdn.net/daniel_ustc/article/details/9395971
工作原理是使用mysql的mysqldump工具来导出数据库为.sql文件,然后将所有导出的文件打包归档。
然后我们在shell脚本中使用 scp命令把备份文件复制到另外一台备份机器。
一,备份脚本
-
#!/bin/sh
-
# mysql_backup.sh: backup mysql databases and keep newest 5 days backup.
-
#
-
# db_user is mysql username
-
# db_passwd is mysql password
-
# db_host is mysql host
-
# —————————–
-
db_user="root"
-
db_passwd="ocs"
-
db_host="localhost"
-
-
# the directory for story your backup file.
-
backup_dir="/home/backup/"
-
-
# date format for backup file (dd-mm-yyyy)
-
time="$(date +"%Y-%m-%d_%H_%M_%S")"
-
today="$(date +"%Y-%m-%d")"
-
fpath=$backup_dir$today
-
echo $fpath
-
if [ ! -d $fpath ];then
-
mkdir $fpath
-
fi
-
-
# mysql, mysqldump and some other bin's path
-
MYSQL="/usr/bin/mysql"
-
MYSQLDUMP="/usr/bin/mysqldump"
-
MKDIR="/bin/mkdir"
-
RM="/bin/rm"
-
MV="/bin/mv"
-
GZIP="/bin/gzip"
-
-
# the directory for story the newest backup
-
test ! -d "$backup_dir/bk/" && $MKDIR "$backup_dir/bk/"
-
-
# check the directory for store backup is writeable
-
test ! -w $backup_dir && echo "Error: $backup_dir is un-writeable." && exit 0
-
-
# get all databases
-
all_db="$($MYSQL -u $db_user -h $db_host -p$db_passwd -Bse 'show databases')"
-
for db in $all_db
-
do
-
$MYSQLDUMP -u $db_user -h $db_host -p$db_passwd $db --single-transaction | $GZIP -9 > "$fpath/$db.$time.gz"
-
done
-
-
#
-
cd $backup_dir
-
tar czf Mysql.$time.tar.gz $today
-
rm -rf $today
-
mv Mysql.$time.tar.gz $backup_dir/bk/
-
-
#scp to other server
-
scp $backup_dir/bk/Mysql.$time.tar.gz root@172.16.86.1:/var/www/html/work/bak/
-
-
# delete the oldest backup
-
#find $backup_dir -type f -mtime +4 -name "*.gz" -exec rm -f {} \;
-
find $backup_dir/bk -name "*.gz" -type f -mtime +5 -exec rm -f {} \; > /dev/null 2>&1
-
-
exit 0;
二,排程
由于机器本身没有安装cron服务,先透过yum先安装
# yum install vixie-cron
# yum install crontabs
说明:
vixie-cron软件包是cron的主程序;
crontabs软件包是用来安装、卸装、或列举用来驱动 cron 守护进程的表格的程序。
安装vixie-cron时,一般会自动加载安装crontabs
加入开机自动启动:
#chkconfig --level 35 crond on
#/sbin/service crond start
三,设置排程
#vi /etc/crontab
#backup mysql db
3 0 * * * root /home/backup/mysql_backup.sh
四,设置scp
本机执行
# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
/root/.ssh/id_rsa already exists.
Overwrite (y/n)? y
Enter passphrase (empty for no passphrase): 回车空密码
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
56:68:73:d4:be:a1:fe:4f:f6:9b:d9:a2:75:o1:d0:a0 root@ng
The key's randomart image is:
+--[ RSA 2048]----+
...
+-----------------+
#cd ~/.ssh
# scp id_rsa.pub root@172.16.1.1:/root/.ssh/authorized_keys
五,恢复数据库
恢复数据备份文件:
非压缩备份文件恢复:
#mysql -u root -p databasename < name2008010103.sql
从压缩文件直接恢复:
# zcat mrbs.2014-04-22_16_16_32.gz | mysql -uroot -ppassword -Dmrbs
其它问题:
# ./mysql_backup.sh
mysqldump: Got error: 1044: Access denied for user 'root'@'localhost' to database 'information_schema' when using LOCK TABLES
指令增加如下:
$MYSQLDUMP -u $db_user -h $db_host -p$db_passwd $db --single-transaction | $GZIP -9 > "$fpath/$db.$time.gz"
--single-transaction
或
--skip-lock-tables
阅读(3218) | 评论(0) | 转发(0) |