baiyunjn 发表于 2018-1-12 15:52:26

rsync数据备份解决方案

  rsync命令是一个远程数据同步工具,可通过LAN/WAN快速同步多台主机间的文件。rsync使用所谓的“rsync算法”来使本地和远程两个主机之间的文件达到同步,这个算法只传送两个文件的不同部分,而不是每次都整份传送,因此速度相当快。
  一、项目规则:
  1、系统环境
  

# cat /etc/redhat-release
  
CentOS Linux>  
# uname -r
  
3.10.0-327.el7.x86_64
  

  2、更改主机名和/etc/hosts文件 (客户端和服务端同时操作)
  

# tail -2 /etc/hosts  
192.168.30.137 zabbix-server
  
192.168.30.136 zabbix-client
  

  

  测试:
  

# ping rsync-node1  
PING rsync-node1 (192.168.30.142) 56(84) bytes of data.
  
64 bytes from rsync-node1 (192.168.30.142): icmp_seq=1 ttl=64 time=0.788 ms
  
64 bytes from rsync-node1 (192.168.30.142): icmp_seq=2 ttl=64 time=0.392 ms
  

  二、安装rsync软件
  1、 安装rsync软件(客户端和服务端同时操作)
  

# yum install rsync -y  
#rpm -qa rsync
  
rsync-3.0.9-17.el7.x86_64
  

  2、修改配置文件,设置备份模块(服务端操作)
  

# cat /etc/rsyncd.conf  
sync server
  
#created by nulige 2017-7-9
  
##rsyncd.conf start##
  
uid = rsync
  
gid = rsync
  
use chroot = no
  
max connections = 2000
  
timeout = 600
  
pid file = /var/run/rsyncd.pid
  
lock file = /var/run/rsync.lock
  
log file = /var/log/rsyncd.log
  
ignore errors
  
read only = false
  
list = false
  
hosts allow = 10.0.100.1/24
  
hosts deny = 0.0.0.0/32
  
auth users = rsync_backup
  
secrets file = /etc/rsync.password
  
##################################################
  

  
comment = backup server by nulige at 207-7-9
  
path = /data
  

  

  3、编写服务启动脚本
  

# vi /etc/init.d/rsync  
#
!/bin/bash  
#author:nulige
  
#this script
for start|stop rsync daemon service  
#
date:2017/7/8  

  
status1
=$(ps -ef | egrep "rsync --daemon.*rsyncd.conf" | grep -v 'grep')  
pidfile
="/var/run/rsyncd.pid"  
start_rsync
="rsync --daemon --config=/etc/rsyncd.conf"  

  
function rsyncstart() {
  

  
if [ "${status1}X" == "X" ];then
  

  
rm -f $pidfile
  

  
${start_rsync}
  

  
status2=$(ps -ef | egrep "rsync --daemon.*rsyncd.conf" | grep -v 'grep')
  

  
if ["${status2}X" != "X"];then
  

  
echo "rsync service start.......OK"
  

  
fi
  

  
else
  

  
echo "rsync service is running !"
  

  
fi
  
}
  

  
function rsyncstop() {
  

  
if [ "${status1}X" != "X" ];then
  

  
kill -9 $(cat $pidfile)
  

  
status2=$(ps -ef | egrep "rsync --daemon.*rsyncd.conf" | grep -v 'grep')
  

  
if [ "${statusw2}X" == "X" ];then
  

  
echo "rsync service stop.......OK"
  
fi
  
else
  

  
echo "rsync service is not running !"
  

  
fi
  
}
  

  

  
function rsyncstatus() {
  

  

  
if [ "${status1}X" != "X" ];then
  

  
echo "rsync service is running !"
  

  
else
  

  
echo "rsync service is not running !"
  

  
fi
  

  
}
  

  
function rsyncrestart() {
  

  
if [ "${status1}X" == "X" ];then
  

  
echo "rsync service is not running..."
  

  
rsyncstart
  
else
  

  
rsyncstop
  

  
rsyncstart
  

  
fi
  
}
  

  
case $1 in
  

  
"start")
  
rsyncstart
  
;;
  

  
"stop")
  
rsyncstop
  
;;
  

  
"status")
  
rsyncstatus
  
;;
  

  
"restart")
  
rsyncrestart
  
;;
  

  
*)
  
echo
  
echo"Usage: $0 start|stop|restart|status"
  
echo
  
esac
  

  #给脚本添加执行权限
# chmod a+x /etc/init.d/rsync
  4、启动rsync
# service rsync start
  rsync service start.......OK
  5、检查端口是否启动
  

#检查用户配置文件  
# ss -lntup|grep rsync
  
tcp    LISTEN   0      5         *:873       *:*                   users:(("rsync",pid=2344,fd=3))
  
tcp    LISTEN   0      5      :::873       :::*                  users:(("rsync",pid=2344,fd=5))
  

  
# ps -ef|grep rsync|grep -v grep
  
root       2344      10 06:40 ?      00:00:00 rsync --daemon --config=/etc/rsyncd.conf
  

  

  6、添加用户并授权
  

# useradd rsync -s /sbin/nologin -M
  
#>  
uid=1000(rsync) gid=1000(rsync) groups=1000(rsync)
  
# mkdir -p /backup
  
# chown -R rsync /backup
  
# ls -ld /backup/
  
drwxr-xr-x 2 rsync root 6 Jul 10 06:44 /backup/
  

  

  7、添加认证的密码 
  

# echo "rsync_backup:nulige" >/etc/rsync.password  
# cat /etc/rsync.password
  
rsync_backup:nulige   #nulige指的是认证的密码
  

  

  8、修改权限
  

# chmod 600 /etc/rsync.password  
# ll /etc/rsync.password
  
-rw------- 1 root root 20 Jul 10 06:47 /etc/rsync.password
  

  

  9、添加开机启动项
  

# echo "rsync --daemon" >>/etc/rc.local  
# tail -1 /etc/rc.local
  
rsync --daemon
  

  三、客户端配置
  1、添加密码并授权
  

# echo "nulige" >/etc/rsync.password  
[iyunv@rsync
-node1 ~]# chmod 600 /etc/rsync.password  
[iyunv@rsync
-node1 ~]# cat /etc/rsync.password  
nulige
  

  四、配置mailx发送邮件
  1、进入目录,下载软件
  

cd /home/nulige/tools/  

  
wget ftp://ftp.iij.ad.jp/pub/linux/centos-vault/6.2/os/x86_64/Packages/mailx-12.4-6.el6.x86_64.rpm
  

  

  2、安装mailx
  

rpm -ivh mailx-12.4-6.el6.x86_64.rpm   

  3、查看版本
  

# mailx -V  
12.4 7/29/08  
  

  4、配置mailx,添加发送邮件账号和密码。
  示例1
  vi /etc/mail.rc
  

#添加在结尾  

#send mail  
set from
=*****@hotdoor.cn  
set smtp
=mail.gxycloud.com  
set smtp
-auth-user=******@hotdoor.cn  
set smtp
-auth-password=*******  
set smtp
-auth=login  

  示例2
  

#send mail  
set from
=zhhotdoor@163.com  
set smtp
=smtp.163.com  
set smtp
-auth-user=zhhotdoor@163.com  
set smtp
-auth-password=hotdoor123  
set smtp
-auth=login  

  5、发送邮件测试
  

echo Gitlab data backup successful! | mailx -v -s " gitlab data backup" ****@hotdoor.cn  

  

  6、编写备份脚本,备份完发送邮件
  

# mkdir -p /server/script  
# cat send_gitlab_backup.sh
  
#!/bin/sh
  
#author:nulige
  
#date:2017-7-10
  
#gitlab data backup script
  
/usr/bin/rsync -avz /data/gitlab_backup rsync_backup@10.0.100.208::backup --password-file=/etc/rsync.password
  
sleep 3000
  
echo Gitlab data backup successful! | mailx -v -s " gitlab data backup" huzh@hotdoor.cn,zhouw@hotdoor.cn
  

  给脚本授可执行权限
  

chmod a+x send_gitlab_backup.sh   

  7、测试是否能备份和发送邮件
  

sh send_gitlab_backup.sh  

  8、查看备份结果
  

# cd /data/gitlab_backup  

  
# ll
  
总用量 12537924
  
-rw------- 1 rsync rsync      40960 6月27 19:52 1498564341_gitlab_backup.tar
  
-rw------- 1 rsync rsync      40960 6月29 08:41 1498696909_gitlab_backup.tar
  
-rw------- 1 rsync rsync   43601920 6月30 14:00 1498802413_gitlab_backup.tar
  
-rw------- 1 rsync rsync 1198592000 7月   1 14:00 1498888815_gitlab_backup.tar
  
-rw------- 1 rsync rsync 1198592000 7月   2 14:00 1498975214_gitlab_backup.tar
  
-rw------- 1 rsync rsync621619200 7月   3 14:00 1499061614_gitlab_backup.tar
  
-rw------- 1 rsync rsync998338560 7月   4 14:00 1499148015_gitlab_backup.tar
  
-rw------- 1 rsync rsync 1457510400 7月   5 14:01 1499234416_gitlab_backup.tar
  
-rw------- 1 rsync rsync 1463951360 7月   6 14:01 1499320874_gitlab_backup.tar
  
-rw------- 1 rsync rsync 1463951360 7月   7 14:00 1499407215_gitlab_backup.tar
  
-rw------- 1 rsync rsync 1463971840 7月   8 14:00 1499493615_gitlab_backup.tar
  
-rw------- 1 rsync rsync 1463971840 7月   9 14:03 1499580087_gitlab_backup.tar
  
-rw------- 1 rsync rsync 1464616960 7月10 14:03 1499666550_gitlab_backup.tar
  
-rw-r--r-- 1 rsync rsync       9837 7月   1 22:11 gitlab_configure_file.tar.gz
  
-rw-r--r-- 1 rsync rsync      142 7月   1 22:11 nginx_configure_file.tar.gz
  
-rw-r--r-- 1 rsync rsync       9436 7月   1 22:11 postfix_configure_file.tar.gz
  

  

  9、查看邮件
https://images2015.cnblogs.com/blog/1053682/201707/1053682-20170710195403853-149449582.png
  10、加入定时任务
  

# crontab -l  

  
#gitlab data backup script
  
0 3 * * * /bin/sh /server/script/send_gitlab_backup.sh >/dev/null 2>&1
  

  

  11、只保留15天gitlab备份数据
  

#vi del_gitlab_backup.sh  
#
!/bin/bash  
#del
15 day gitlab backup  

find /data/gitlab_backup -type f -name "*.tar" -mtime +15|xargs rm -f  

  #加入定时任务中
  

# crontab -l  
#times
sync by lee at 2017-06-28  
*/5 * * * * /usr/sbin/ntpdate 10.0.100.208 >/dev/null 2>&1
  

  
#gitlab_backup_nulige_2017-6-27
  
0 2 * * * /bin/gitlab-rake gitlab:backup:create
  

  
#gitlab data backup script
  
0 3 * * * /bin/sh /server/script/send_gitlab_backup.sh >/dev/null 2>&1
  

  
#del 15 day gitlab backup
  
0 4 * * * /bin/sh /server/script/del_gitlab_backup.sh >/dev/null 2>&1
  

  
#gitstaticstics script
  
#0 4 * * 1 /usr/local/bin/gitstatistics.sh &
  
页: [1]
查看完整版本: rsync数据备份解决方案