Centos rsync 文件同步备份,crontab定时同步

主要特性

rsync是linux系统下的数据镜像备份工具。使用快速增量备份工具Remote Sync可以远程同步,支持本地复制,或者与其他SSH、rsync主机同步。

  • 可以镜像保存整个目录树和文件系统。
  • 可以很容易做到保持原来文件的权限、时间、软硬链接等等。
  • 无须特殊权限即可安装。
  • 快速:第一次同步时 rsync 会复制全部内容,但在下一次只传输修改过的文件。rsync 在传输数据的过程中可以实行压缩及解压缩操作,因此可以使用更少的带宽。
  • 安全:可以使用scp、ssh等方式来传输文件,当然也可以通过直接的socket连接。
  • 支持匿名传输,以方便进行网站镜像。
yum install rsync #安装rsync
mkdir /etc/rsyncd
#创建 rsyncd.conf,这是 rsync 服务器的配置文件
touch /etc/rsyncd/rsyncd.conf
ln -s /etc/rsyncd/rsyncd.conf /etc/rsyncd.conf #创建软链接

 #创建 rsyncd.secrets ,这是用户密码文件文件 权限必须设置为600 chmod 600 
touch /etc/rsyncd/rsyncd.secrets 

##说明文件 可以自定添加说明信息,同步文件是会在开头提示
touch /etc/rsyncd/rsyncd.motd

  • 设置账号密码
#格式为user:password 
vim /etc/rsyncd/rsyncd.secrets  -> root:root
chown root.root /etc/rsyncd/rsyncd.secrets  
chmod 600 /etc/rsyncd/rsyncd.secrets 
  • 修改服务说明文件
vim /etc/rsyncd/rsyncd.motd 
#新增如下内容:
*********************************
      Welcome to server 167!
*********************************
  • 修改服务端配置文件
    -vim /etc/rsyncd/rsyncd.conf
pid file = /var/run/rsyncd.pid #进程对应的进程id
lock file = /var/run/rsync.lock  #锁文件
log file = /var/log/rsync.log #日志文件
secrets file = /etc/rsyncd/rsyncd.secrets #账户密码文件路径
motd file = /etc/rsyncd/rsyncd.motd #服务端说明信息

port = 873 #指定运行端口,默认是873
address = 192.168.148.167 #指定服务端IP地址
uid = root #用户远端的命令使用rsync访问共享目录
gid = root #用户组
use chroot = no #安全相关
max connections = 10 #最大连接数
read only = yes #可写

hosts deny=* #禁止IP访问

transfer logging = yes #使rsync服务器使用ftp格式的文件来记录下载和上载操作在自己单独的日志中
log format = %t %a %m %f %b #详见下面说明
syslog facility = local3 #详见下面说明
timeout = 300 #可以覆盖客户指定的IP超时时间 默认值不限制

[www] #模块名称
path = /wwwroot/data #服务器提供访问的目录
list=yes #是否能列表
hosts allow=192.168.148.134 192.168.148.168 ##允许IP访问 以空格分割 客户端IP
hosts deny=* #禁止IP访问
ignore errors = true #忽略错误
auth users = root #虚拟用户
#auth users 是必须在服务器上存在的真实的系统用户,如果你想用多个用户以 “,” 号隔开,比如auth users = moTzxx,root
comment = This is moTzxx-comment #提交信息,便于识别
#exclude = file3/  #此处表明,同步文件 除去所有的 file3 目录,如果以斜杠开头,则可指定“path = /wwwroot/data”下的任何目录或文件
  • log format
    通过该选项用户在使用transfer logging可以自己定制日志文件的字段
    其格式是一个包含格式定义符的字符串,格式定义符如下所示:
    %h 远程主机名
    %a 远程IP地址
    %l 文件长度字符数
    %p 该次rsync会话的进程id
    %o 操作类型:“send"或"recv”
    %f 文件名
    %P 模块路径
    %m 模块名
    %t 当前时间
    %u 认证的用户名(匿名时是null)
    %b 实际传输的字节数
    %c 当发送文件时,该字段记录该文件的校验码
    默认log格式为:"%o %h [%a] %m (%u) %f %l"
    一般来说,在每行的头上会添加"%t [%p] "
    在源代码中同时发布有一个叫rsyncstats的perl脚本程序来统计这种格式的日志文件
  • syslog facility
    指定rsync发送日志消息给syslog时的消息级别
    常见的消息级别是:uth, authpriv, cron, daemon, ftp, kern, lpr, mail, news, security, sys-log, user, uucp, local0, local1, local2, local3, local4, local5, local6和local7
    默认值是daemon。
  • timeout
    通过该选项可以覆盖客户指定的IP超时时间
    通过该选项可以确保rsync服务器不会永远等待一个崩溃的客户端
    超时单位为秒钟,0表示没有超时定义,这也是默认值
    对于匿名rsync服务器来说,一个理想的数字是600
  • 启动rsync服务
#--config用于指定rsyncd.conf的位置,如果在/etc下可以不写
/usr/bin/rsync --daemon  --config=/etc/rsyncd/rsyncd.conf  
  • 检测服务器防火墙 rsync默认端口 873
iptables -A INPUT -p tcp -m state --state NEW  -m tcp --dport 873 -j ACCEPT
iptables -L  #查看一下防火墙是不是打开了 873端口

客户端

  • 配置密码
#密码格式为password 
vim /etc/rsyncd.secrets  -> root
chmod 600 /etc/rsyncd.secrets 
  • 列出 rsync 服务器上的内容
#sync--list-only  --password-file=/etc/rsyncd.secrets 192.168.148.167::www
[root@localhost vhosts]# rsync  --list-only  --password-file=/etc/rsyncd.secrets 192.168.148.167::www
*********************************
      Welcome to server 167!
*********************************
drwxr-xr-x             57 2021/03/01 14:04:03 .
-rw-r--r--             62 2021/03/01 06:44:24 index.html
-rw-r--r--             47 2021/03/01 06:43:37 index.php
drwxr-xr-x             32 2021/03/01 14:08:57 test_new
您在 /var/spool/mail/root 中有新邮件
[root@localhost vhosts]# 
  • 同步文件
# rsync  -avzP --delete  --password-file=/etc/rsyncd.secrets  192.168.148.167::www /wwwroot/data
[root@localhost vhosts]# rsync  -avzP --delete  --password-file=/etc/rsyncd.secrets  192.168.148.167::www /wwwroot/data
*********************************
      Welcome to server 167!
*********************************

receiving incremental file list

sent 25 bytes  received 175 bytes  400.00 bytes/sec
total size is 143  speedup is 0.71
您在 /var/spool/mail/root 中有新邮件
[root@localhost vhosts]# 
  • crontab实现实时同步
  1. 创建同步脚本
[root@localhost vhosts]# vim /wwwroot/rsync.sh 
#!/bin/bash
rsync  -avzP --delete  --password-file=/etc/rsyncd.secrets  192.168.148.167::www /wwwroot/data

#添加执行权限
chmod +x /wwwroot/rsync.sh 
  1. 加入定时任务
 crontab -e
* * * * * /wwwroot/rsync.sh
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值