代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
MYSQL
】
MySql 自动化备份脚本
作者:
Dezai.CN
/ 发布于
2013/5/28
/
1746
-a: backup all database #全库备份 -e: backup each database#分库备份 -d: backup single/multi database#备份指定的一个库或者多个库 -t: backup single/multi table of single database#备份一个库下面的一个表或者多个表 -b: backup binlog#备份binlog日志,备份过程,每次记录最后的二进制文件号,将之前范围内的binlog打包(bz2格式),放在$DIR_BACKUP目录下 -r: recover all database(!require password!)#恢复全库,为保安全,恢复时需要输入密码 -o: recover single database/talbe,you should be designation database name(!require password!)#恢复单库或单表 -p: create connect mysql password#创建连接mysql的密码文件,存放位置$DIR_MySQL/etc目录下,权限是600 -s: configuration rsyncd#创建rsyncd服务(需要时,可修改参数创建,因backup机上有rsyncd服务,故不需要在每台DB server上创建rsyncd服务) If you want ceate a rsyncd, you should enter 'bakrec_mysql.sh -s cet' If you want restart rsyncd, you should enter 'bakrec_mysql.sh -s rst' -c: sync to backup center#同步到backup1 #!/bin/bash # email: lianjie.ning@qunar.com # last change time: 2011-08-03 set -e set -u TIME=`date +%Y%m%d%H%M%S` TIME_7=`date -d '7 days ago' +%Y%m%d%H%M%S` TIME_YM=`date +%Y%m` DIR_MYSQL='/usr/local/mysql' DIR_BACKUP="/tmp/backup" DIR_DATA="$DIR_MYSQL/data" DIR_PASSWD="$DIR_MYSQL/etc" FILE_PASSWD="$DIR_PASSWD/passwordfile" BINLOG_NAME='mysql-bin' CMD_MYSQLBINLOG="$DIR_MYSQL/bin/mysqlbinlog" CMD_MYSQLDUMP="$DIR_MYSQL/bin/mysqldump" CMD_MYSQL="$DIR_MYSQL/bin/mysql" LIST_EXCLUDE_DB='(test|information_schema|performance_schema)' if [ ! -d $DIR_BACKUP/$TIME_YM ]; then mkdir -p $DIR_BACKUP/$TIME_YM fi cd $DIR_BACKUP/$TIME_YM function result_status() { if [ $? -eq 0 ]; then echo "[`date +%Y%m%d%H%M%S`] SUCCESS! "|tee -a log.$TIME_YM else echo "[`date +%Y%m%d%H%M%S`] ERROR! "|mail -s "backup error $HOSTNAME" ning_lianjie@163.com|tee -a log.$TIME_YM fi } function usage_error() { echo "Usage: $0 RUN ERROR" echo " -a: backup all database -e: backup each database -d: backup single/multi database -t: backup single/multi table of single database -b: backup binlog -r: recover all database(!require password!) -o: recover single database/talbe,you should be designation database name(!require password!) -p: create connect mysql password -s: configuration rsyncd If you want ceate a rsyncd, you should enter '$0 -s cet' If you want restart rsyncd, you should enter '$0 -s rst' -c: sync to backup center " exit 0 } function read_pwd() { read USER PASSWD < $FILE_PASSWD } function backup() { read_pwd LOGBIN_STATUS=`$CMD_MYSQL -u$USER -p$PASSWD -N -s -e "SHOW VARIABLES LIKE 'log_bin'" | gawk '{print $2}'` if [ $LOGBIN_STATUS = "ON" ]; then MASTER='--master-data=2' else MASTER=' ' fi } function backup_all() { backup $CMD_MYSQLDUMP -u$USER -p$PASSWD -x -R -A --add-drop-database $MASTER |gzip >$HOSTNAME.all.$TIME.sql.gz } function backup_each() { backup for db in $($CMD_MYSQL -u$USER -p$PASSWD -N -s -e "SHOW DATABASES"|egrep -v $LIST_EXCLUDE_DB) do $CMD_MYSQLDUMP -u$USER -p$PASSWD -x -R $MASTER $db --databases |gzip >$HOSTNAME.$db.$TIME.sql.gz done # delete 7 days ago for db in $($CMD_MYSQL -u$USER -p$PASSWD -N -s -e "SHOW DATABASES"|egrep -v $LIST_EXCLUDE_DB) do if [ ! -f $HOSTNAME.$db.$TIME_7.sql.gz ]; then echo else rm $HOSTNAME.$db.$TIME_7.sql.gz -f fi done } function backup_db() { shift backup $CMD_MYSQLDUMP -u$USER -p$PASSWD -x -R $MASTER --databases $@| gzip>$HOSTNAME.$OPTARG.$TIME.sql.gz } function backup_dt() { shift if [ $# -ge 2 ]; then backup $CMD_MYSQLDUMP -u$USER -p$PASSWD -x -R $MASTER $@| gzip>$HOSTNAME.$OPTARG.$TIME.sql.gz else usage_error fi } function backup_binlog() { if [ -s $DIR_BACKUP/mysql-bin.queue ]; then read POS < $DIR_BACKUP/mysql-bin.queue cd $DIR_DATA tar -jcvf $DIR_BACKUP/$TIME_YM/$HOSTNAME.$POS.$TIME.bz2 `gawk -F'/' '{print $2}' $BINLOG_NAME.index |sed -n "/$POS/,//p"` cd - if [ -f $DIR_BACKUP/$TIME_YM/$HOSTNAME.$POS.$TIME_7.bz2 ]; then rm $DIR_BACKUP/$TIME_YM/$HOSTNAME.$POS.$TIME_7.bz2 -f fi fi # write last pos gawk -F'/' '{print $2}' $DIR_DATA/$BINLOG_NAME.index | tail -n 1 >$DIR_BACKUP/mysql-bin.queue } function recover_all() { read_pwd shift $CMD_MYSQL -u$USER -p -e "source $@" } function recover_dt() { read_pwd shift if [ $# -eq 2 ]; then $CMD_MYSQL -u$USER -p -D $1 -e "source $2" else usage_error fi } function passwd_create() { if [ ! -e "$DIR_PASSWD" ]; then mkdir -p $DIR_PASSWD fi echo -n "Please enter MySQL(user=root)'s password:" read -s MYSQL_FASSWD cat >$FILE_PASSWD <<+ root $MYSQL_FASSWD + chmod 600 $FILE_PASSWD } function rsyncd() { shift if [ $# -eq 0 ]; then usage_error else DIR_RSYNCD='/usr/local/rsync' FILE_RSYNCD_PASSWORD="$DIR_RSYNCD/rsyncd.password" case "$1" in 'cet') if [ ! -d $DIR_RSYNCD ]; then mkdir -p $DIR_RSYNCD fi if [ ! -e "$DIR_RSYNCD/rsyncd.conf" ]; then touch $DIR_RSYNCD/rsyncd.conf fi mv $DIR_RSYNCD/rsyncd.conf $DIR_RSYNCD/rsyncd.conf.$TIME.bak cat >$DIR_RSYNCD/rsyncd.conf <<+ uid = root gid = root use chroot = no max connections = 5 lock file = $DIR_RSYNCD/rsyncd.lock log file = $DIR_RSYNCD/rsyncd.log pid file = $DIR_RSYNCD/rsyncd.pid hosts allow = 192.168.250.251 hosts deny = * ignore errors read only = yes list = no auth users = backupdbuser secrets file = $DIR_RSYNCD/rsyncd.password [BINLOG] path = $DIR_DATA include = $BINLOG_NAME.* exclude = * [DUMPDB] path = $DIR_BACKUP + cat >$FILE_RSYNCD_PASSWORD <<+ username:password + chmod 600 $FILE_RSYNCD_PASSWORD exit 0 ;; 'rst') if [ -s "$DIR_RSYNCD/rsyncd.pid" ]; then rsyncd_pid=`cat "$DIR_RSYNCD/rsyncd.pid"` if (kill -0 $rsyncd_pid 2>/dev/null); then echo "Shutting down rsyncd" kill $rsyncd_pid else echo "rsyncd #$rsyncd_pid is not running!" rm "$DIR_RSYNCD/rsyncd.pid" fi fi sleep 2 rsync --daemon --config=$DIR_RSYNCD/rsyncd.conf --port=873 echo "rsync --daemon --config=$DIR_RSYNCD/rsyncd.conf --port=873" echo "netstat -tunlp | grep rsync" netstat -tunlp | grep rsync ;; *) usage_error ;; esac fi } #main if [ $# -eq 0 ]; then usage_error else while getopts :aed:t:r:o:bpsc varname do case $varname in a) backup_all ;; e) backup_each ;; d) backup_db $@ ;; t) backup_dt $@ ;; b) backup_binlog ;; r) recover_all $@ ;; o) recover_dt $@ ;; p) passwd_create ;; s) rsyncd $@ ;; c) # rsync -czrptgoD --password-file=/tmp/.passwd $HOSTNAME.*.$TIME.sql.gz backupdbuser@192.168.250.251::DUMPDB/$HOSTNAME/$TIME_YM rsync -czrpt --password-file=/tmp/.passwd $DIR_BACKUP/$TIME_YM backupdbuser@192.168.250.251::DUMPDB/$HOSTNAME result_status ;; :) echo "$varname: 缺少参数" usage_error ;; \?) echo "$varname: 非法选项" usage_error ;; esac done fi
试试其它关键字
自动化备份
同语言下
.
MySQL按日期分组统计(按天统计,按月统计)
.
mysql 获取某个库下面所有表的所有数据(
.
查看表当前自增值
.
查看碎片较多的表
.
查看数据表较大的表
.
查看非InnoDB引擎表
.
根据某一字段值按照某一特定符转化为列数据
.
按时间段分组查询当天小时内数据
.
mysql json 使用 类型 查询 函数
.
按天数据统计
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
Dezai.CN
贡献的其它代码
(
4037
)
.
多线程Socket服务器模块
.
生成随机密码
.
清除浮动样式
.
弹出窗口居中
.
抓取url的函数
.
使用base HTTP验证
.
div模拟iframe嵌入效果
.
通过header转向的方法
.
Session操作类
.
执行sqlite输入插入操作后获得自动编号的ID
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3