zoukankan      html  css  js  c++  java
  • Linux下利用rsync实现多服务器文件同步

    Linux下利用rsync实现多服务器文件同步
    第一部分:简单的数据备份
     
    第一步:服务器端的配置
    #rpm –qa |grep rsync
    如果没有的话,则自行下载安装
    #cd  /usr/local/src
    # wget http://www.samba.org/ftp/rsync/src-previews/rsync-3.0.6pre1.tar.gz
    #tar –zxvf rsync-3.0.6pre1.tar.gz
    #cd rsync-3.0.6pre1
    #./configure && make && make install
    假设web服务器有三个目录需要备份
    /www
    /home/web_user1/
    /home/web_user2/
    创建rsync的配置文件
    #vim /etc/rsyncd.conf
    uid = nobody
    gid = nobody
    use chroot = no
    max connections = 4
    pid file = /var/run/rsyncd.pid
    lock file = /var/run/rsync.lock
    log file = /var/log/rsyncd.log
     
    [www]
    path = /www/
    ignore errors
    read only = true
    list = false
    hosts allow = 10.80.11.244
    hosts deny = 0.0.0.0/32
    auth users = backup
    secrets file = /etc/backserver.pas
     
    [web_user1]
    path = /home/web_user1/
    ignore errors
    read only = true
    list = false
    hosts allow = 10.80.11.244
    hosts deny = 0.0.0.0/32
    uid = web_user1
    gid = web_user1
    auth users = backup
    secrets file = /etc/backserver.pas
     
    [web_user2]
    path = /home/web_user2/
    ignore errors
    read only = true
    list = false
    hosts allow = 10.80.11.244
    hosts deny = 0.0.0.0/32
    uid = web_user2
    gid = web_user2
    auth users = backup
    secrets file = /etc/backserver.pas
    备注:
    uid = nobody        //指定文件传输过程中的用户身份
    gid = nobody        //指定文件传输过程中的组身份
    log file = /var/log/rsyncd.log        //指定使用独立的日志文件的位置
    pid file = /var/run/rsyncd.pid        //保存pid到指定文件,以便于使用脚本终止进程
    read only = yes        //该目录设置为只读,即只允许客户端下行同步,不允许上传。若需要进行从备份机还原数据,改为no
    rsyncd默认在873端口监听服务,可在客户端使用telnet连接测
    密码文件为/etc/backserver.pas
    #vim /etc/backserver.pas
    backup:123456
    格式为:用户名:密码
    设置密码文件仅仅root用户能访问
    #chmod 600 /etc/backserver.pas
    然后建立备份用户
    #useradd backup
    #passwd backup
    在服务器端启动rsync服务
    # nohup rsync --daemon &
     
    第二步:备份端的配置
    安装rsync软件,同上
    设置密码文件
    #vim /etc/rsync.pas
    123456
    在客户端不需要加用户名,只需要加密码,同时设置为只有root有权限
    #chmod 600 /etc/rsync.pass
    创建备份目录
    #mkdir-p /backup/www
    #mkdir -p /backup/web_user1
    #mkdir -p /backup/web_user2
    把服务器端文件www模块备份到本机
    #/usr/bin/rsync -vzrtopg --delete --exclude "logs/" --exclude "conf/ssl.*/" --progress backup@10.80.11.243::www /backup/www/ --password-file=/etc/rsync.pass
     
     
    --delete是指如果服务器端删除了这一文件,那么客户端也相应把文件删除,保持真正的一致, 删除本地目录中多余的文件
    -- exclude "logs/" 表示不对/www/logs目录下的文件进行备份。
    --exclude "conf/ssl.*/"表示不对/www/conf/ssl.*/目录下的文件进行备份。
    对其他的模块的备份如下:
    #/usr/bin/rsync -vzrtopg --delete --exclude "logs/" --exclude "conf/ssl.*/" --progress backup@10.80.11.243::web_user1 /backup/web_user1/ --password-file=/etc/rsync.pass
     
    #/usr/bin/rsync -vzrtopg --delete --exclude "logs/" --exclude "conf/ssl.*/" --progress backup@10.80.11.243:: web_user2 /backup/web_user2/ --password-file=/etc/rsync.pass
    如果提示以下错误
    @ERROR: invalid uid web_user2
    rsync error: error starting client-server protocol (code 5) at main.c(1503) [receiver=3.0.6pre1]
    则需要修改web服务器上的web_user2uidgid注释掉
    定时备份的话,在crontab –e里面设置
    :备份脚本,可以加入crontab,按时自动备份(backup服务器上运行)
    #!/bin/bash
    DATE=`date  +%w`
    rsync -tvzrp -e ssh --progress --delete backup@10.80.11.244::www /backup/www/$DATE   --password-file=/etc/rsync.pass > /var/log/test.$DATE
    mail -s "Backup is done" yhl5555@126.com < /var/log/test.$DATE
  • 相关阅读:
    CF1202F You Are Given Some Letters...
    CF1178E Archaeology
    PTA (Advanced Level) 1005 Spell It Right
    PTA (Advanced Level) 1004 Counting Leaves
    Qt5——从零开始的Hello World教程(Qt Creator)
    PTA (Advanced Level) 1003 Emergency
    PTA (Advanced Level) 1002 A+B for Polynomials
    HDU 1272 小希的迷宫
    FZU 2150 Fire Game
    HihoCoder
  • 原文地址:https://www.cnblogs.com/helloyb/p/2870162.html
Copyright © 2011-2022 走看看