linux系统中Rsync文件同步方案

linux服务器下Rsync文件同步配置


  1. Rsync(remote synchronize) 远程数据同步工具,可以使用“Rsync算法”
    同步本地和远程主机之间的文件、Rsync的好处是只同步两个文件不同的部分,
    相同的部分不在传递、类似于增量备份,这使的在服务器传递备份文件或者同步文件。
  2. crontab配合rsync 使用可达到定时备份同步任务。

Rsync部署环境
1、服务器准备
| Host | IP | ----------|
| Server | 192.168.60.110 | ----------|
| client | 192.168.60.80 | ----------|

2、Server

        2.1、ubuntu  16.04默认已安装rsync,直接修改它的配置文件即可。
        sudo vim /etc/default/rsync
        RSYNC_ENABLE=true   #false改true

3、修改配置文件
3.1、 rsync佩在文件默认在/usr/share/doc/rsync/examples/下,需要手动将配置文件拷贝到/etc目录下,
sudo cp /usr/share/doc/rsync/examples/rsyncd.conf /etc
3.2、修改conf配置文件。
sudo vim /etc/rsyncd.conf

            #motd file=/etc/motd
            log file=/var/log/rsyncd
            #for pid file, do not use /var/run/rsync.pid if
            #you are going to run rsync out of the init.d script.
            #The init.d script does its own pid file handling,
            #so omit the "pid file" line completely in that case.
            pid file=/var/run/rsyncd.pid
            syslog facility=daemon
            #socket options=
            #MODULE OPTIONS
            [ftp]
    comment = public archive
    path = /home/zy/ftp
    use chroot = no
            #max connections=10
    lock file = /var/lock/rsyncd
            #the default for read only is yes...
    read only = yes
    list = yes
    uid = nobody
    gid = nogroup
            #exclude = 
            #exclude from = 
            #include =
            #include from =
    auth users = liu_rsync
    secrets file = /etc/rsyncd.secrets
    strict modes = yes
    hosts allow = 192.168.60.110
            #hosts deny =
    ignore errors = no
    ignore nonreadable = yes
    transfer logging = no
            #log format = %t: host %h (%a) %o %f (%l bytes). Total %b bytes.
    timeout = 600
    refuse options = checksum dry-run
    dont compress = *.gz *.tgz *.zip *.z *.rpm *.deb *.iso *.bz2 *.tbz

4、创建一个密码文件
vim /etc/rsyncd.pwd
内容为:rsync:123
sudo chmod 0600 /etc/rsyncd.pwd
5、启动rsync
sudo /etc/init.d/rsync start
如果启动成功,那么server端酒配置成功了。

Client

1、客户端不需要进行特殊的配置,直接同步即可
rsync -vzrtopg --progress [email protected]::my_rsync_bk .
1.1、此时rsync只能将Server端现有的数据同步下来,如果Server新添加数据却不能同步下来,这有点难受。
1.2、所以需要创建一个密码文件(和server端做免密钥也可以的)
sudo vim /etc/rsync.pwd输入123 #密码要一致
sudo chmod 0600 /etc/rsync.pwd
sudo chown 普通用户:普通用户组 /etc/rsync.pwd
1.3、自动执行:命令行终端输入:crontab -e 添加以下内容,
/1 rsync -a --password-file=/etc/rsync.pwd [email protected]::my_rsync_bk /data/
/1 为一分钟同步一次 只要Server端添加了新文件、1分钟内就会自动同步到客户端。
1.5、删除文件需要同步 需要添加 --delete 参数
/1 * rsync -a --password-file=/etc/rsync.pwd --delete [email protected]::my_rsync_bk /data/
rsync同步配置完成


案例:

一、从本地同步文件到远程服务器
rsync -avz /home/ftp [email protected].8:home/ftp
二、将远程服务器的文件同步到本地crontab -e
rsync -avz [email protected].8:home/ftp /home/ftp


我的同步参数:

远程PC同步到本地PC.每十分钟执行一次

猜你喜欢

转载自blog.51cto.com/11353391/2483106