Rsync同步服务器配置详解

关于Rsync的作用其他应用命令这里不做具体阐述,这里只对rsync同步服务器的配置过程给出详细过程



1    Rsync部署环境准备

1.1   服务器准备


服务器系统


角色


Ip


Centos6.6x86_64


Backup服务器

1.2   检查环境

[[email protected] ~]# cat /etc/redhat-release

CentOS release 6.6 (Final)

[[email protected] nfs]# uname -r

2.6.32-504.el6.x86_64

[[email protected] nfs]# uname -i

x86_64

1.3   修改服务器名称

[email protected] ~]# hostname backup

[[email protected] ~]# cat  /etc/sysconfig/network

NETWORKING=yes

HOSTNAME=backup

[[email protected] ~]# cat /etc/hosts

192.168.196.136 backup

2    Backup 服务端端配置

2.1   Rsync软件列表

Centos6默认装3版本

2.2       检查软件是否安装

[[email protected] ~]# rpm -qa rsync

rsync-3.0.6-12.el6.x86_64

2.3   安装软件

[[email protected] ~]# yum install -y rsync

2.4   配置rsync(尽可能的复制目录等信息)

2.4.1   配置文件

Rsync默认配置文件是不存在的所以需要自己建立配置。

2.4.1.1      创建文件

[[email protected] ~]# touch /etc/rsyncd.conf

2.4.1.2      编辑配置文件

[[email protected] ~]# cat /etc/rsyncd.conf

#Rsync server

#created by xiao_k

##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/rync.lock

log file = /var/log/rsync.log

ignore errors

read only = false

list = false

hosts allow = 192.168.196.0/24

hosts deny = 0.0.0.0/32

auth users = rsync_backup

secrets file = /etc/rsync.password

###################################

[backup]

comment = www by xiao_k

path = /backup

2.5   启动服务

Rsync默认没有启动脚本(自己写)

[[email protected] ~]# rsync –daemon

2.6   检查是否启动

[[email protected] ~]# ps -ef |grep rsync|grep -v grep

root       3474      1  0 06:27 ?        00:00:00 rsync --daemon

2.7   检查端口

[[email protected] ~]# netstat -lntup|grep rsync

tcp        0      0 0.0.0.0:873       0.0.0.0:*    LISTEN      3474/rsync

tcp        0      0 :::873    :::*                LISTEN      3474/rsync

2.8   添加用户配置用户

2.8.1   添加

[[email protected] ~]# useradd rsync -s /sbin/nologin -M

2.8.2   检查

[[email protected] ~]# id rsync

uid=505(rsync) gid=505(rsync) groups=505(rsync)

2.9   创建以及配置备份服务器存放目录

2.9.1   创建目录

[[email protected] ~]# mkdir /backup

2.9.2   修改权限

[[email protected] ~]# chown -R rsync /backup/

2.9.3   检查

[[email protected] ~]# ls -ld /backup

drwxr-xr-x. 2 rsync root 4096 Aug  7 06:43 /backup

2.10  配置密码文件

配置文件默认不存在,自己需要创建

2.10.1  创建文件

[[email protected] ~]# echo "rsync_backup:xiao_k">/etc/rsync.password

2.10.2  检查配置结果

[[email protected] ~]# cat /etc/rsync.password

rsync_backup:xiao_k

用户:密码 用户就是配置文件中指定的用户。

2.10.3  检查并修改密码文件权限

2.10.3.1    检查文件权限

[[email protected] ~]# ll /etc/rsync.password

-rw-r--r--. 1 root root 20 Aug  7 06:50 /etc/rsync.password

2.10.3.2    修改权限

[[email protected] ~]# chmod 600 /etc/rsync.password

2.10.3.3    检查

[[email protected] ~]# ll /etc/rsync.password

-rw-------. 1 root root 20 Aug  7 06:50 /etc/rsync.password

2.11  启动服务

2.11.1  启动服务

[[email protected] ~]# rsync –daemon

2.11.2  检查启动结果

[[email protected] ~]# ps -ef|grep rsync|grep -v grep

root       3474      1  0 06:27 ?        00:00:00 rsync --daemon

2.11.3  添加到开机自启动

[[email protected] ~]# cat /etc/rc.local

#!/bin/sh

#

# This script will be executed *after* all the other init scripts.

# You can put your own initialization stuff in here if you don't

# want to do the full Sys V style init stuff.

touch /var/lock/subsys/local

rsync --daemon

3    客户端配置

3.1   创建客户端密码文件

[[email protected] ~]# echo "xiao_k" /etc/rsync.password

3.2   检查

[[email protected] ~]# cat /etc/rsync.password

xiao_k

3.3   修改文件权限

[[email protected] ~]# chmod 600 /etc/rsync.password

3.4   检查

配置完成

4    定时备份推送。

4.1   命令行测试送

4.1.1   本地打包:

cd / && tar -acvf /backup/config_$(date +%F-%H:%M).tar.gz  /var/spool/cron/root  /etc/rc.local /etc/sysconfig/iptables /server/scripts

cd / && tar zcvf /backup/www_$(date +%F-%H:%M).tar.gz /var/html/www

cd / tar zcvf /backup/logs_$(date +%F-%H:%M).tar.gz app/logs/

4.1.2   向远端服务器推送

[[email protected] ~]# rsync -avz /backup/ [email protected]::backup --password-file=/etc/rsync.password

4.1.3   删除本地过期备份

[[email protected] /]# find /backup -type f -name "*.tar.gz" -mtime +180|xargs rm -f

4.1.4   编写脚本

4.1.4.1      备份脚本

#!/bin/sh

IP=$(ifconfig eth0|awk -F '[ :]+' 'NR==2 {print $4}')

Path="/backup/$IP"

[ ! -d $Path ] && mkdir $Path -p

#backup

tar -acf $Path/config_$(date +%F-%H:%M).tar.gz  /var/spool/cron/root  /etc/rc.local /etc/sysconfig/iptables /serve

r/scripts

tar -zcf $Path/www_$(date +%F-%H:%M).tar.gz /var/html/www

tar -zcf $Path/logs_$(date +%F-%H:%M).tar.gz app/logs/

#to back server

rsync -az /backup/ [email protected]::backup --password-file=/etc/rsync.password

#delete

find /backup -type f -name "*.tar.gz" -mtime +7|xargs rm -f

4.1.5   删除

[[email protected] /]# cat /server/scripts/del_back.sh

#########################################################################

# File Name: del_back.sh

# Author: xiao_k

# mail: [email protected]

# Created Time:Sun 05 Aug 2018 09:48:42 AM CST

#########################################################################

#!/bin/bash

/bin/find /backup -type f -name "*.tar.gz" -mtime +180|xargs rm -f

4.1.6   编写定时任务

[[email protected] /]# crontab -l

##########################################################00 00 * * * /bin/sh  /server/scripts/backup.sh  &>/dev/null

##########################################################

00 01 * * * /bin/sh /server/scripts/del_back.sh &>/dev>null

5    检查数据完整性脚本及定时备份检查

5.1   客户端

[[email protected] backup]# cat /server/scripts/backup.sh

#########################################################################

# File Name: backup.sh

# Author: xiao_k

# mail: [email protected]

# Created Time:Sun 05 Aug 2018 01:43:54 PM CST

#########################################################################

#!/bin/bash

IP=$(ifconfig eth0|awk -F '[ :]+' 'NR==2 {print $4}')

Path="/backup/$IP"

[ ! -d $Path ] && mkdir $Path -p

#backup

tar -acf $Path/config_$(date +%F).tar.gz  /var/spool/cron/root  /etc/rc.local /etc/sysconfig/iptables /server/scripts &&\

tar -zcf $Path/www_$(date +%F).tar.gz /var/html/www  &&\

tar -zcf $Path/logs_$(date +%F).tar.gz /app/logs/ && \

find /backup/ -type f -name "*.tar.gz"|xargs md5sum>>$Path/flag_$(date +%F)

#to back server

rsync -az /backup/ [email protected]::backup --password-file=/etc/rsync.password

#delete

find /backup -type f -name "*.tar.gz" -mtime +7|xargs rm -f

5.2   服务器端

5.2.1   编写脚本检查

[[email protected] opt]# cat /server/scripts/check_md5.sh

#########################################################################

# File Name: check_md5.sh

# Author: xiao_k

# mail: [email protected]

# Created Time:Tue 07 Aug 2018 09:40:26 PM CST

#########################################################################

#!/bin/bash

find /backup -type f -name "flag_$(date +%F)"|xargs md5sum -c |grep FAILED >/opt/mail_body_flag_$(date +%F).txt

if [  -f /opt/mail_body_flag_$(date +%F).txt -o -s /opt/mail_body_flag_$(date +%F).txt ]

then

echo "ok">>/opt/ok_mail_body_flag_$(date +%F).txt

fi

if [ ! -f /opt/mail_body_flag_$(date +%F).txt -o -s /opt/mail_body_flag_$(date +%F).txt ]

then

mail -s "$(date +%U%T) back" [email protected] </opt/mail_body_flag_$(date +%F).txt

fi

5.2.2   定时任务

####

00 01 * * * /bin/sh   /server/scripts/check_md5.sh



到这里基本的rsync同步服务器就就配置成功了。当然可能存在一定安全问题,先跑通,再变通。欢迎大佬指正。

原文地址:http://blog.51cto.com/967243153/2157419

时间: 2024-08-29 22:28:41

Rsync同步服务器配置详解的相关文章

Rsync服务配置详解,实现服务器间数据同步!

1.1 什么是rsync? rsync是Unix下的一款应用软件,它能同步更新两处计算机的文件与目录,并适当利用差分编码以减少数据传输.rsync中一项与其他大部分类似程序或协议中所未见的重要特性是镜像对每个目标只需要一次传送.rsync可拷贝/显示目录属性,以及拷贝文件,并可选择性的压缩以及递归拷贝. 在常驻模式(daemon mode)下,rsync默认监听TCP端口873,以原生rsync传输协议或者通过远程shell如RSH或者SSH伺服文件.SSH情况下,rsync客户端运行程序必须同

rsync 远程数据同步工具详解

rysnc 命令用法:(OPTION-参数,USER-用户,HOST-IP地址,SRC-复制源位置,DEST-复制目标位置)Shell拉:rsync [OPTION] [[email protected]]HOST:SRC [DEST]rsync拉:rsync [OPTION] [[email protected]]HOST::[MODULE] [DEST]Shell推:rsync [OPTION] SRC [[email protected]]HOST:DESTrsync推:rsync [OP

rsync+inotify实现文件实时同步-步骤详解

实验拓扑(centos7下):192.168.80.181 服务器端(主机名www.aa.com)192.168.80.182 客户端(主机名www.ab.com)1.使用SSH源:安装rsync,服务端和客户端同时安装,只使用客户端命令就OK了.systemctl stop firewalldsetenforce 0yum install -y rsync---以上三句在服务器端和客户端都要执行---------rsync -avz [email protected]:/tmp/ /opt/

linux下rsync文件同步配置详解

介绍 rsync(remote sync)是unix及类unix平台下的数据镜像备份软件,它不像FTP那样需要全备份,rsync可以根据数据的变化进行差异备份,从而减少数据流量,提高工作效率 rsync主要分为三个配置文件,分别是rsyncd.conf(主配置文件),rsyncd.secrets(密码文件),rsyncd.motd(服务器信息文件) ? 环境 centos7 IP:192.168.10.130?? 作为rsync服务器 centos7 IP:192.168.10.132? 作为r

Rsync命令参数详解

在对rsync服务器配置结束以后,下一步就需要在客户端发出rsync命令来实现将服务器端的文件备份到客户端来.rsync是一个功能非常强大的工具,其命令也有很多功能特色选项,我们下面就对它的选项一一进行分析说明. Rsync的命令格式可以为以下六种: 1rsync [OPTION]... SRC DEST 2rsync [OPTION]... SRC [[email protected]]HOST:DEST 3rsync [OPTION]... [[email protected]]HOST:S

linux日常管理-rsync常用选项详解

-av 同步目录 写法 123/   /tmp/333/ 意思是把123下的文件同步到/tmp/333/下  结尾不加/ 只同步目录 两个目录一样的. ///////////////////////////////////////////////////////////////////////////////////////////////// 同步软连接 1.创建软连接 2.查看软连接文件详细信息 3.同步到/tmp/333/ 4.查看/tmp/333/的软连接文件.如果是远程同步,本地没有软

烂泥:【转】rsync命令参数详解

本文由秀依林枫提供友情赞助,首发于烂泥行天下. rsync安装完毕后,我们可以通过rsync –help查看rysnc命令的使用.如下: 有关rsync的命令格式,在此我们就不多介绍了.如果有想了解的童鞋,请参考这篇文章<烂泥:linux文件同步之rsync学习(一)>. 本篇文章,我们只介绍rsync的命令参数. rsync参数的具体解释如下: -v, --verbose 详细模式输出 -q, --quiet 精简输出模式 -c, --checksum 打开校验开关,强制对文件传输进行校验

rsync 安装使用详解

rsync是类unix系统下的数据镜像备份工具,从软件的命名上就可以看出来了——remote sync.它的特性如下:可以镜像保存整个目录树和文件系统.可以很容易做到保持原来文件的权限.时间.软硬链接等等.无须特殊权限即可安装.优化的流程,文件传输效率高.可以使用rcp.ssh等方式来传输文件,当然也可以通过直接的socket连接.支持匿名传输,以方便进行网站镜象.软件下载rysnc的主页地址为:http://rsync.samba.org/目前最新版本为2.4.6.可以选择从原始网站下载:ht

Linux rsync 命令参数详解

1,在对rsync服务端配置结束以后,下一步就需要在客户端发出rsync命令来实现将服务器端的文件备份到客户端来.rsync是一个功能非常强大的工具,其命令也有很多功能特色选项,我们下面就对它的选项一一进行分析说明. Rsync的命令格式可以为以下六种rsync [OPTION]... SRC DESTrsync [OPTION]... SRC [USER@]HOST:DESTrsync [OPTION]... [USER@]HOST:SRC DESTrsync [OPTION]... [USE