Rsync服务器与MySQL自动分库分表备份安装shell脚本


使用环境:CentOS6.x  

服务端:1.1.1.1  客户端:1.1.1.2

通过rsync和mysql分库分表备份,实现网站文件和数据库文件定时备份到异地。

#!/bin/bash
#部署mysql备份脚本
mkdir -p /server/shell_scripts/mysql_back_scripts 
cat >> /server/shell_scripts/mysql_back_scripts/mysql_back_schame_table.sh <<"EOF"
#!/bin/bash
cd /server/shell_scripts/mysql_back_scripts
DBPATH="/server/shell_scripts/mysql_back_scripts/backup_file"
MYUSER=root
MYPASS=18859224109
SOCKET=/tmp/mysql.sock
MYCMD="/usr/bin/mysql -u$MYUSER -p$MYPASS -S $SOCKET"
MYDUMP="/usr/bin/mysqldump -u$MYUSER -p$MYPASS -S $SOCKET"
DATADIR="/www/server/data"
DB_CONFIG="/etc/my.cnf"
BACK_LOG="/server/shell_scripts/mysql_back_scripts/backup_file"
[ ! -d "$DBPATH"  ]&& mkdir -p  $DBPATH
cd $DBPATH
[ -f "./mysql_back_schame_table.log" ] || touch $BACK_LOG/mysql_back_schame_table.log


echo $(echo `$MYCMD -e "show databases;"|sed '1,2d'|egrep -v "mysql|information_schema|performance_schema|test"`)>/tmp/mysql_backup


echo $($MYCMD -e "flush logs")
#bin_log_name=$(cat $DB_CONFIG | grep "^log_bin"|awk -F "=" '{print $2}')
bin_log=`tail -n 1 $DATADIR/mysql-bin.index | awk -F "/" '{print $2}'`
echo "flush logs after the bin_log file is $DATADIR/$bin_log">>$BACK_LOG/mysql_back_schame_table.log


for dbname in `cat /tmp/mysql_backup`
do
SUB_DIR="${dbname}_$(date +%F-%H)"
mkdir $DBPATH/$SUB_DIR -p
echo $(echo `$MYCMD -e "show tables from $dbname;"|sed '1d'`)>/tmp/mysql_table
        for table in `cat /tmp/mysql_table`
            do
echo "$(date +%F_%H-%M-%S)-Start to Backup dbname:$dbname -->Table:$table ....">>$BACK_LOG/mysql_back_schame_table.log
                $MYDUMP --add-drop-table  --flush-privileges    $dbname $table|gzip>$DBPATH/$SUB_DIR/${table}.sql.gz
echo "$(date +%F_%H-%M-%S)-Backup dbname:$dbname -->Table:$table success">>$BACK_LOG/mysql_back_schame_table.log
sleep 1
            done
    done
echo "$(date +%F_%H-%M-%S)-Backup ALL Dababase success">>$BACK_LOG/mysql_back_schame_table.log
exit 0
EOF
chmod 755 /server/shell_scripts/mysql_back_scripts/mysql_back_schame_table.sh




#添加自动备份crontab
echo "00 00 * * * /server/shell_scripts/mysql_back_scripts/mysql_back_schame_table.sh  &>>/dev/null" >> /var/spool/cron/root 


#安装rsync服务端
cd /usr/local/src/
wget https://download.samba.org/pub/rsync/src/rsync-3.1.2.tar.gz  && tar -zxvf rsync-3.1.2.tar.gz
cd rsync-3.1.2
mkdir -p /usr/local/rsync
./configure --prefix=/usr/local/rsync && make && make install && mkdir -p /etc/rsyncd


#添加启动文件
cat >>/etc/init.d/rsyncd <<"EOF"
#!/bin/bash
#rsyncd This shell script takes care of starting and stopping standalone rsync.
#chkconfig: - 99 50
#description:rsync is a file transport daemon
#processname:rsync
#config:/etc/rsyncd.conf
#Source function library
. /etc/rc.d/init.d/functions
RETVAL=0
prog="rsync"
rsync="/usr/local/rsync/bin/rsync"
pid="/var/run/rsyncd.pid"
lock="/var/run/rsync.lock"
CFILE="/etc/rsyncd/rsyncd.conf"


start() {
#Start daemons.
[ -x $rsync ] || \
{ echo "FATAL:No such programme"; exit 4; }
[ -f $CFILE ] || \
{ echo "FATAL:config file does not exist"; exit 6; }
echo -n $"Starting $prog daemon..."
$rsync --daemon --config=$CFILE
RETVAL=$?
echo -n "OK!!!"
echo
return $RETVAL
}
stop() {
#Stop daemons
echo -n $"Stopping $prog:..."
kill -9 `cat /var/run/rsyncd.pid`
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && rm -f $pid && rm -f $lock
echo -n "OK!!!"
echo
return $RETVAL
}


status(){
if [ -f $pid  ];then
echo "Rsync is running,pid(`cat $pid`)"
else
echo "Rsync is not running"
fi



}


#call the function we defined
case "$1" in 
start)
start
;;
stop)
stop
;;
restart|reload)
stop
start
RETVAL=$?
;;
status)
status
        RETVAL=$?
;;
*)
echo $"Usage:$0 {start|stop|restart|reload|status}"
exit 2
esac
exit $RETVAL
EOF


chmod 755 /etc/init.d/rsyncd


#添加rsync配置文件
cd /etc/rsyncd
cat >>/etc/rsyncd/rsyncd.conf <<"EOF"
uid = 0 
gid = 0 
max connections = 4 
read only = true 
hosts allow = 1.1.1.2
transfer logging = true 
log format = %h %o %f %l %b  
log file = /var/log/rsyncd.log 
slp refresh = 300 
log file = /var/log/rsyncd.log 
pid file = /var/run/rsyncd.pid 
lock file = /var/run/rsyncd.lock


[web] 
path = /www/wwwroot
comment = Mirror to Hk server 
read only = false 
list = false 
auth users = rsync_user
secrets file = /etc/rsyncd/rsync.server.password




[db] 
path = /server/shell_scripts/mysql_back_scripts
comment = Mirror to Hk server 
read only = false 
list = false 
auth users = rsync_user
secrets file = /etc/rsyncd/rsync.server.password
EOF


cat >>/etc/rsyncd/rsync.client.password<<EOF
rsync123
EOF
chmod 600 /etc/rsyncd/rsync.client.password


cat >>/etc/rsyncd/rsync.server.password<<EOF
rsync_user:rsync123
EOF


chmod 600 /etc/rsyncd/rsync.server.password
chkconfig rsyncd on
service rsyncd start 

备份服务器拉取服务端的数据:

[root@node10 rsync_client]# cat rsync_.sh
#!/bin/bash
/usr/bin/rsync -avz rsync_user@1.1.1.1::db  /Web_File_Backup/db_file/ --password-file=/etc/rsyncd/rsync.client.password
/usr/bin/rsync -avz rsync_user@1.1.1.1::web  /Web_File_Backup/web_file/ --password-file=/etc/rsyncd/rsync.client.password


YWSOS.COM 平台代运维解决方案
 评论
 发表评论
姓   名:

Powered by AKCMS