rsync数据备份解决方案

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了rsync数据备份解决方案相关的知识,希望对你有一定的参考价值。

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

一、项目规则:

1、系统环境

[[email protected] ~]# cat /etc/redhat-release 
CentOS Linux release 7.2.1511 (Core) 
[[email protected] ~]# uname -r
3.10.0-327.el7.x86_64

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

[[email protected] etc]# tail -2 /etc/hosts
192.168.30.137 zabbix-server
192.168.30.136 zabbix-client

 

二、安装rsync软件

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

[[email protected] ~]# yum install rsync -y
[[email protected] ~]#  rpm -qa rsync
rsync-3.0.9-17.el7.x86_64

2、编辑rsyncd.conf配置文件

[[email protected] ~]# vi /etc/rsyncd.conf
#Rsync server
#created by nulige 2017-7-8
##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 = 192.168.30.1/24
hosts deny = 0.0.0.0/32
auth users = rsync_backup
secrets file = /etc/rsync.password
##################################################
[backup]
comment = backup server by nulige at 207-7-8
path = /backup

3、添加用户并授权、更改所属组】

[[email protected] etc]# useradd rsync -s /sbin/nologin -M
[[email protected]-server etc]# id rsync
uid=1000(rsync) gid=1000(rsync) groups=1000(rsync)
[[email protected]-server etc]# mkdir /backup
[[email protected]-server etc]# chown -R rsync /backup
[[email protected]-server etc]#  ls -ld /backup
drwxr-xr-x 2 rsync root 6 Jun 24 10:18 /backup

4、创建密码文件

[[email protected] etc]#  echo "rsync_backup:nulige" >/etc/rsync.password
[[email protected] etc]# chmod 600 /etc/rsync.password
[[email protected] etc]#  cat /etc/rsync.password
rsync_backup:nulige

5、添加rsync启动脚本

[[email protected] ~]# 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

6、给脚本添加执行权限

[[email protected] ~]# chmod a+x /etc/init.d/rsync

7、启动rsync服务

#启动
[[email protected] ~]# service rsync start

#重启
[[email protected] ~]# service rsync restart
rsync service is not running...

#查看状态
[[email protected] ~]# service rsync status
rsync service is not running ! 

#启动rsync服务(没有用启动脚本的情况,系统默认的启动方法)

[[email protected] etc]# rsync --daemon

8、查看启动端口

[[email protected] etc]# netstat -lntup|grep rsync
tcp        0      0 0.0.0.0:873             0.0.0.0:*               LISTEN      49936/rsync         
tcp6       0      0 :::873                  :::*                    LISTEN      49936/rsync  

 9、加入开机自启动

 [[email protected] ~]#echo "#rsync boot start by nulige at 2017-7-8" >>/etc/rc.local

[[email protected] ~]#echo "rsync --daemon" >>/etc/rc.local

[[email protected] ~]# tail /etc/rc.local 
#rsync boot start by nulige at 2017-7-8
rsync --daemon

三、安装客户端

 

以上是关于rsync数据备份解决方案的主要内容,如果未能解决你的问题,请参考以下文章

rsync远程同步(都在用的备份方案)

Rsync + Intofy 数据实时同步方案

linux系统中Rsync文件同步方案

探索rsync在备份方案中的作用

rsync如何同步单个文件到服务器上,使用rsync命令将备份同步到远程服务器的四个简单步骤...

Linux rsync 远程同步部署篇