Rsync备份服务
一、Rsync备份软件应用与实践
1.1.1 什么是Rsync?
Rsync英文全称为Remote synchronization 缩写rsync,是开源、高速的、可实现本地以及远程,全量以及增量的数据复制(拷贝)远程工具。
官方链接资料:http://www.samba.org/ftp/rsync/rsync.html
1.1.2 Rsync的作用?
工作中需要定时/实时的数据备份。本地服务器目录,不同主机、不同机房之间的数据备份。都可以用Rsync来完成。
1.1.3 Rsync的功能特性。
1.支持拷贝普通文件与特殊文件,如链接文件,设备文件等。
2.支持排除指定文件或目录同步的功能,相当于打包命令tar的排除功能。
3.可以做到保持原文件或目录的权限、时间、软硬链接、属主、组等所有属性均不改变。
4.可实现增量复制,既只复制发生变化的数据,因此数据传输效率很高。
5.可以使用rcp,rsh,ssh等方式来配合进行隧道加密传输文件(rsync本身不对数据加密)。
5.可以使用rcp,rsh,ssh等方式来配合进行隧道加密传输文件(rsync本身不对数据加密)。
7.支持匿名或认证(无需系统用户)进程模式传输,安全的进行数据备份及镜像。
全量备份:每一次备份都全部备份。
增量备份:只备份有变化或增加的数据。
二、Rsync的三种工作模式。
2.1.1 Rsync本地模式(拷贝与删除)。
rsync就是一个命令
rsync [OPTION...] SRC... [DEST]
rsync命令 参数选项 [源目录或文件] 目的目录或文件
命令操作:
rsync命令
a.把数据从一个地方复制到另一个地方(仅在一台机器增量),相当于cp。
b.通过加参数实现删除文件和清空文件内容的功能,相当于rm命令。
c.查看属性信息功能,相当于ls。
2.1.2 本地拷贝删除功能。
- 拷贝
[root@backup ~]# rsync /etc/hosts /opt
[root@backup ~]# ls -l /opt
total 4
-rw-r--r-- 1 root root 332 Apr 28 21:07 hosts
#相当于cp命令
2.删除文件内容
[root@backup ~]# touch /null.txt
[root@backup ~]# cat /opt/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
172.16.1.5 lb01
172.16.1.6 lb02
172.16.1.7 web01
172.16.1.8 web02
172.16.1.9 web03
172.16.1.31 nfs01
172.16.1.41 backup
172.16.1.51 db01 db01.etiantian.org
172.16.1.61 m01
[root@backup ~]# rsync -r --delete /null.txt /opt/hosts
[root@backup ~]# cat /opt/hosts
#内容被清空了
3.删除目录下所有文件:
[root@backup ~]# mkdir /null
[root@backup ~]# rsync -r --delete /null/ /opt/ #让后面opt和前面null目录内容保持一致
[root@backup ~]# ls /opt/
2.1.2 远程shell模式
就是把一台主机的数据拷贝到另外一台主机上。有两种方式。
pull,拉:从远端拉取到本地。
rsync [OPTION...] [USER@]HOST:SRC... [DEST]
rsync命令 参数选项 [认证用户]@[主机地址]:[源路径] 本地路径
push,推:从本地推到远端。
rsync [OPTION...] SRC... [USER@]HOST:[DEST]
rsync命令 参数选项 本地路径 [认证用户]@[主机地址]:[目标路径]
1.pull 实战
[root@nfs01 ~]# rsync -avz /etc/hosts root@172.16.1.41:/opt/
The authenticity of host '172.16.1.41 (172.16.1.41)' can't be established.
ECDSA key fingerprint is SHA256:bbt9sjPOENs3zK9cw7YmIo0ABuFkZnTxXbOaIdpSOo0.
ECDSA key fingerprint is MD5:e5:3b:15:2e:6c:82:4b:b1:f8:45:dc:80:72:de:11:47.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '172.16.1.41' (ECDSA) to the list of known hosts.
root@172.16.1.41's password:
sending incremental file list
hosts
sent 218 bytes received 35 bytes 46.00 bytes/sec
total size is 332 speedup is 1.31
[root@backup ~]# ll /opt/hosts
-rw-r--r-- 1 root root 332 Apr 28 04:57 /opt/hosts
[root@backup ~]# cat /opt/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
172.16.1.5 lb01
172.16.1.6 lb02
172.16.1.7 web01
172.16.1.8 web02
172.16.1.9 web03
172.16.1.31 nfs01
172.16.1.41 backup
172.16.1.51 db01 db01.etiantian.org
172.16.1.61 m01
#rsync -avz /etc/hosts -e "ssh -p 22" root@172.16.1.41:/opt/
上述命令是等价的。-e 指定通道 ssh ssh服务连接客户端 -p 22指定22端口。
2.pash 实战
[root@nfs01 ~]# rsync -avz root@172.16.1.41:/opt/hosts /opt
root@172.16.1.41's password:
receiving incremental file list
hosts
sent 43 bytes received 218 bytes 58.00 bytes/sec
total size is 332 speedup is 1.27
[root@nfs01 ~]# ll /opt/
total 4
-rw-r--r-- 1 root root 332 Apr 28 04:57 hosts
[root@nfs01 ~]# cat /opt/
cat: /opt/: Is a directory
[root@nfs01 ~]# cat /opt/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
172.16.1.5 lb01
172.16.1.6 lb02
172.16.1.7 web01
172.16.1.8 web02
172.16.1.9 web03
172.16.1.31 nfs01
172.16.1.41 backup
172.16.1.51 db01 db01.etiantian.org
172.16.1.61 m01
##rsync -avz -e "ssh -p 22" root@172.16.1.41:/opt/hosts /opt/
上述命令是等价的。-e 指定通道 ssh ssh服务连接客户端 -p 22指定22端口。
2.1.3 Rsync守护进程模式
首先要搭建Rsync服务端(要有守护进程),然后才能在客户端实现拉取数据。这是工作中的重要模式。
1.Rsync常见参数
-v, --verbose 显示输出过程
-z, --compress 压缩
-a, --archive多参数集合(-rtopgDl)
-r, --recursive 递归
-t, --times 保持修改时间属性
-o, --owner 保持属主不变
-p, --perms 保持权限不变
-g, --group 保持用户组不变
-l, --links 保持拷贝软连接
-q, --quiet 安静的拷贝
--bwlimit=KBPS限制I/O带宽,KBytes per second。
--delete 删除
--exclude 排除
--exclude-from 从文件中排除
企业常用参数组合:-avz或者-vzrtopg
三、Rsync守护进程模式的部署
3.1.1 服务器端部署
1.检查是否安装Rsync服务
[root@backup ~]# rsync --version
rsync version 3.1.2 protocol version 31
Copyright (C) 1996-2015 by Andrew Tridgell, Wayne Davison, and others.
Web site: http://rsync.samba.org/
Capabilities:
64-bit files, 64-bit inums, 64-bit timestamps, 64-bit long ints,
socketpairs, hardlinks, symlinks, IPv6, batchfiles, inplace,
append, ACLs, xattrs, iconv, symtimes, prealloc
rsync comes with ABSOLUTELY NO WARRANTY. This is free software, and you
are welcome to redistribute it under certain conditions. See the GNU
General Public Licence for details.
2.如果没有安装,可以使用yum安装。
[root@backup ~]# yum install rsync -y
3.配置配置文件/etc/rsyncd.conf
[root@backup ~]# ll /etc/rsyncd.conf
-rw-r--r-- 1 root root 458 Apr 1 12:22 /etc/rsyncd.conf
备份配置文件:
[root@backup ~]# cp /etc/rsyncd.conf{,.back}
[root@backup ~]# ll /etc/rsyncd.conf{,.back}
-rw-r--r-- 1 root root 458 Apr 1 12:22 /etc/rsyncd.conf
-rw-r--r-- 1 root root 458 Apr 28 22:14 /etc/rsyncd.conf.back
文件配置如下:
[root@backup ~]# cat /etc/rsyncd.conf
#rsync_config_______________start
uid = rsync #管理备份目录的用户
gid = rsync #管理备份目录用户组
fake super = yes #将rsync用户伪装成超级用户
use chroot = no #安全功能,数据是否锁定到备份目录
max connections = 200 #最大连接数
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 = 172.16.1.0/24 #允许连接的主机
hosts deny = 0.0.0.0/32 不允许连接主机
auth users = rsync_backup #指定备份虚拟用户名
secrets file = /etc/rsync.password #密码存放文件
[backup] #模块
comment = welcome to oldboyedu backup! #说明注释
path = /backup/ #指定备份目录
更多配置可以这样查询:
[root@backup ~]# man rsyncd.conf
4.创建备份管理用户,目录和密码文件并授予权限。
[root@backup ~]# useradd -M -s /sbin/lonogin rsync
[root@backup ~]# id rsync
uid=1001(rsync) gid=1001(rsync) groups=1001(rsync)
[root@backup ~]# mkdir -pv /backup
mkdir: created directory ‘/backup
[root@backup ~]# chown -R rsync.rsync /backup/
[root@backup ~]# ll -d /backup/
drwxr-xr-x 2 rsync rsync 6 Apr 28 22:42 /backup/
密码文件创建:
[root@backup ~]# echo "rsync_backup:oldboy" > /etc/rsync.password
[root@backup ~]# chmod 600 /etc/rsync.password
[root@backup ~]# cat /etc/rsync.password
rsync_backup:oldboy
[root@backup ~]# ll /etc/rsync.password
-rw------- 1 root root 20 Apr 28 22:45 /etc/rsync.password
5.启动Rsync守护进程并开机自启动
[root@backup ~]# systemctl start rsyncd
[root@backup ~]# systemctl enable rsyncd
Created symlink from /etc/systemd/system/multi-user.target.wants/rsyncd.service to /usr/lib/systemd/system/rsyncd.service.
[root@backup ~]# systemctl status rsyncd
● rsyncd.service - fast remote file copy program daemon
Loaded: loaded (/usr/lib/systemd/system/rsyncd.service; enabled; vendor preset: disabled)
Active: active (running) since Tue 2020-04-28 22:49:42 CST; 16s ago
Main PID: 2115 (rsync)
CGroup: /system.slice/rsyncd.service
└─2115 /usr/bin/rsync --daemon --no-detach
Apr 28 22:49:42 backup systemd[1]: Started fast remote file copy program daemon.
Apr 28 22:49:42 backup rsyncd[2115]: params.c:Parameter() - Ignoring badly formed line in config file: rsync_config________...__start
Apr 28 22:49:42 backup rsyncd[2115]: params.c:Parameter() - Ignoring badly formed line in config file: ignore errors
Apr 28 22:49:42 backup rsyncd[2115]: rsyncd version 3.1.2 starting, listening on port 873
Hint: Some lines were ellipsized, use -l to show in full.
6.查看进程和端口
[root@backup ~]# ps -ef | grep rsync
root 2154 1493 0 22:51 pts/0 00:00:00 grep --color=auto rsync
[root@backup ~]# ss -luntp |grep 873
tcp LISTEN 0 5 *:873 *:* users:(("rsync",pid=2115,fd=3))
tcp LISTEN 0 5 [::]:873 [::]:* users:(("rsync",pid=2115,fd=5))
[root@backup ~]# netstat -luntp|grep 873
tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 2115/rsync
tcp6 0 0 :::873 :::* LISTEN 2115/rsync
3.1.2 配置客户端
1.配置密码文件并修改权限
[root@nfs01 ~]# echo "oldboy" > /etc/rsync.password
[root@nfs01 ~]# chmod 600 /etc/rsync.password
[root@nfs01 ~]# cat /etc/rsync.password
oldboy
2.Rsync语法
pull,拉:从远端拉取到本地。
语法1(常用):
rsync [OPTION...] [USER@]HOST::SRC... [DEST]
rsync命令 参数选项 [虚拟用户]@[主机地址]::[模块名] 本地路径
语法2:
rsync [OPTION...] rsync://[USER@]HOST::SRC... [DEST]
rsync命令 参数选项 rsync://[虚拟用户]@[主机地址]/[模块名] 本地路径
push,推:从本地推到远端。
语法1(常用):
rsync [OPTION...] [DEST] [USER@]HOST::SRC...
rsync命令 参数选项 本地路径 [虚拟用户]@[主机地址]::[模块名]
语法2:
rsync [OPTION...] [DEST] rsync://[USER@]HOST::SRC...
rsync命令 参数选项 本地路径 rsync://[虚拟用户]@[主机地址]/[模块名]
四、测试成果
4.1.1 在客户端测试
1.测试pull
[root@nfs01 ~]# rsync -avz /etc/hosts rsync_backup@172.16.1.41::backup --password-file=/etc/rsync.password
sending incremental file list
hosts
rsync: chgrp ".hosts.1wDLEO" (in backup) failed: Operation not permitted (1)
sent 222 bytes received 124 bytes 692.00 bytes/sec
total size is 332 speedup is 0.96
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1178) [sender=3.1.2]
2.在服务端查看:
[root@backup ~]# ll /backup/
total 4
-rw------- 1 rsync rsync 332 Apr 28 23:21 hosts
3.报错解决
[root@nfs01 ~]# rsync -avz /etc/hosts rsync_backup@172.16.1.41::backup --password-file=/etc/rsync.password
sending incremental file list
hosts
rsync: chgrp ".hosts.YDuTjO" (in backup) failed: Operation not permitted (1)
sent 223 bytes received 124 bytes 694.00 bytes/sec
total size is 332 speedup is 0.96
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1178) [sender=3.1.2]
配置服务器端的配置文件里添加fake super = yes
[root@backup ~]# vim /etc/rsyncd.conf
fake super = yes
[root@backup ~]# grep fake /etc/rsyncd.conf
fake super = yes
5.重启Rsync服务
[root@backup ~]# systemctl restart rsyncd
6.客服端在备份一次
[root@nfs01 ~]# rsync -avz /etc/hosts rsync_backup@172.16.1.41::backup --password-file=/etc/rsync.password
sending incremental file list
hosts
sent 222 bytes received 43 bytes 530.00 bytes/sec
total size is 332 speedup is 1.25
#没有在报错
7.备份密码扩展小知识
我们在客服端创建密码文件其实有两种方法,
第一种就创建密码文件并授权
[root@nfs01 ~]# echo "oldboy" > /etc/rsync.password
[root@nfs01 ~]# chmod 600 /etc/rsync.password
[root@nfs01 ~]# cat /etc/rsync.password
oldboy
第二种就是配置环境变量
[root@nfs01 ~]# echo 'export RSYNC_PASSWORD=oldboy' >>/etc/bashrc
[root@nfs01 ~]# tail -1 /etc/bashrc
export RSYNC_PASSWORD=oldboy
[root@nfs01 ~]# . /etc/bashrc
[root@nfs01 ~]# echo $RSYNC_PASSWORD
oldboy
8.使用第二年种方法配置密码备份的时候就可以不用密码文件了。
[root@nfs01 ~]# rsync -avz /etc rsync_backup@172.16.1.41::backup
sending incremental file list
9.增量备份测试
[root@nfs01 ~]# touch /etc/cs.txt
[root@nfs01 ~]# rsync -avz /etc rsync_backup@172.16.1.41::backup
sending incremental file list
etc/
etc/cs.txt
sent 45,358 bytes received 665 bytes 30,682.00 bytes/sec
total size is 28,325,825 speedup is 615.47
10.在web01客户端测试
密码文件方法
[root@web01 ~]# mkdir /data
[root@web01 ~]# touch /data/web01.txt
[root@web01 ~]# rsync -zav /data/ rsync_backup@172.16.1.41::backup --password-file=/etc/rsync.password
sending incremental file list
./
web01.txt
sent 109 bytes received 46 bytes 310.00 bytes/sec
total size is 0 speedup is 0.00
环境变量方法
[root@web01 ~]# touch /data/web02.txt
[root@web01 ~]# rsync -zav /data/ rsync_backup@172.16.1.41::backup
sending incremental file list
./
web02.txt
sent 135 bytes received 46 bytes 362.00 bytes/sec
total size is 0 speedup is 0.00
4.1.2 rsync 应用扩展
1.排除备份
[root@nfs01 /data]# touch {a..d}
[root@nfs01 /data]# ls -l
total 0
-rw-r--r-- 1 root root 0 Apr 29 04:03 a
-rw-r--r-- 1 root root 0 Apr 29 04:03 b
-rw-r--r-- 1 root root 0 Apr 29 04:03 c
-rw-r--r-- 1 root root 0 Apr 29 04:03 d
--ecclude 参数 排除
[root@nfs01 /data]# rsync -avz /data/ --exclude=a --exclude=b rsync_backup@172.16.1.41::backup --password-file=/etc/rsync.password
sending incremental file list
./
c
d
sent 151 bytes received 65 bytes 432.00 bytes/sec
total size is 0 speedup is 0.00
[root@backup /backup]# ls -l
total 4
-rw-r--r-- 1 rsync rsync 0 Apr 29 04:03 c
-rw-r--r-- 1 rsync rsync 0 Apr 29 04:03 d
--exclude-from 从文件排除
[root@nfs01 /data]# touch {10..20}
[root@nfs01 /data]# ls
10 11 12 13 14 15 16 17 18 19 20
[root@nfs01 /data]# seq 10 15 > paichu.txt
[root@nfs01 /data]# cat paichu.txt
10
11
12
13
14
15
[root@nfs01 /data]# rsync -avz /data/ --exclude-from=paichu.txt rsync_backup@172.16.1.41::backup --password-file=/etc/rsync.password
sending incremental file list
./
16
17
18
19
20
paichu.txt
sent 385 bytes received 141 bytes 1,052.00 bytes/sec
total size is 18 speedup is 0.03
[root@backup /backup]# ls
16 17 18 19 20
2.删除
--delete 参数删除 (很危险慎用)。
[root@nfs01 /data]# ls -l
total 4
-rw-r--r-- 1 root root 0 Apr 29 04:17 10
-rw-r--r-- 1 root root 0 Apr 29 04:17 11
-rw-r--r-- 1 root root 0 Apr 29 04:17 12
-rw-r--r-- 1 root root 0 Apr 29 04:17 13
-rw-r--r-- 1 root root 0 Apr 29 04:17 14
-rw-r--r-- 1 root root 0 Apr 29 04:17 15
-rw-r--r-- 1 root root 0 Apr 29 04:17 16
-rw-r--r-- 1 root root 0 Apr 29 04:17 17
-rw-r--r-- 1 root root 0 Apr 29 04:17 18
-rw-r--r-- 1 root root 0 Apr 29 04:17 19
-rw-r--r-- 1 root root 0 Apr 29 04:17 20
-rw-r--r-- 1 root root 18 Apr 29 04:17 paichu.txt
[root@backup /backup]# ls -l
total 0
-rw-r--r-- 1 rsync rsync 0 Apr 29 04:17 16
-rw-r--r-- 1 rsync rsync 0 Apr 29 04:17 17
-rw-r--r-- 1 rsync rsync 0 Apr 29 04:17 18
-rw-r--r-- 1 rsync rsync 0 Apr 29 04:17 19
-rw-r--r-- 1 rsync rsync 0 Apr 29 04:17 20
[root@nfs01 /data]# rm -f *
再这样执行备份命令后服务端的数据就会丢失.
[root@nfs01 /data]# rsync -avz --delete /data/ rsync_backup@172.16.1.41::backup --password-file=/etc/rsync.password
sending incremental file list
deleting 20
deleting 19
deleting 18
deleting 17
deleting 16
./
sent 51 bytes received 53 bytes 208.00 bytes/sec
total size is 0 speedup is 0.00
这样操作之后Rsync服务器的就会删除清空
[root@backup /backup]# ls
推送:
rsync -avz --delete /data/ rsync_backup@172.16.1.41::backup
本地目录有啥,远端就有啥。
注意:远端目录是不是东西更多,多了东西会被删除。提前备份backup对应的目录。
rsync -avz --delete rsync_backup@172.16.1.41::backup /data/
远端有啥。本地目录就有啥,注意本地/data目录。把/data改成根。提前注意备份本地/data
五、Rsync多模块环境备份
5.1.1 多模块环境
1.更改服务器端配置文件增加模块。
[root@backup ~]# vim /etc/rsyncd.conf
[root@backup ~]# tail -2 /etc/rsyncd.conf
[data]
path = /data/
#注意,除了增加以上两行外,也可以设定更多的独立配置参数,例如:独立虚拟用户,独立密码文件等。
2.创建模块目录
[root@backup ~]# mkdir /data/
[root@backup ~]# ls -ld /data
drwxr-xr-x 2 root root 6 Apr 29 04:45 /data
3.授权rsync用户
[root@backup ~]# ls -ld /data/
drwxr-xr-x 2 rsync rsync 6 Apr 29 04:45 /data/
4.重启服务器
[root@backup ~]# systemctl restart rsyncd
[root@backup ~]# systemctl status rsyncd
● rsyncd.service - fast remote file copy program daemon
Loaded: loaded (/usr/lib/systemd/system/rsyncd.service; enabled; vendor preset: disabled)
Active: active (running) since Wed 2020-04-29 04:47:14 CST; 9s ago
Main PID: 3281 (rsync)
CGroup: /system.slice/rsyncd.service
└─3281 /usr/bin/rsync --daemon --no-detach
Apr 29 04:47:14 backup systemd[1]: Started fast remote file copy program daemon.
5.客户端推送
[root@nfs01 ~]# rsync -avz /etc/hosts rsync_backup@172.16.1.41::data --password-file=/etc/rsync.password
sending incremental file list
hosts
sent 222 bytes received 43 bytes 530.00 bytes/sec
total size is 332 speedup is 1.25
6.服务端查看
[root@backup ~]# ls -l /data/
total 4
-rw-r--r-- 1 rsync rsync 332 Apr 28 04:57 hosts
六、Rsync 全网备份部署企业案列
6.1.1 项目介绍
公司有一台web服务器,里面的数据很重要,但是如果硬盘坏了,数据就会丢失,现在领导要求把数据做备份,这样web服务器上的数据即使丢失,也可以恢复。每天晚上10整在web服务器A上打包备份系统配置文件,网站程序目录以及访问日志并通过Rsync命令推送到服务器B上备份保留。
6.1.2 创建需要打包的目录文件
1.创建压缩打包文件目录
[root@web01 /]# mkdir -p /backup/
[root@web01 /]# ls -ld /backup/
drwxr-xr-x 2 root root 6 Apr 29 06:20 /backup/
2.创建web程序目录文件
[root@web01 /]# mkdir -pv /var/html/www
mkdir: created directory ‘/var/html’
mkdir: created directory ‘/var/html/www’
3.web日志文件
[root@web01 /]# mkdir -pv /app/log/
mkdir: created directory ‘/app’
mkdir: created directory ‘/app/log/’
4.创建脚本存放目录
[root@web01 /]# mkdir -pv /server/scripts
[root@web01 /]# ls -ld /server/scripts
drwxr-xr-x 2 root root 25 Apr 29 02:12 /server/scripts
5.命令行测试
[root@web01 /]# tar zcvf /backup/back_$(hostname -I|awk '{print $2}')_$(date +%F_%w).tar.gz /var/spool/cron/root /var/html/www/ /app/log/ /etc/rc.local /server/scripts/
tar: Removing leading `/' from member names
/var/spool/cron/root
/var/html/www/
/app/log/
/etc/rc.local
/server/scripts/
/server/scripts/tar_back.sh
[root@web01 /]# ls -l /backup/
total 4
-rw-r--r-- 1 root root 627 Apr 29 06:34 back_172.16.1.7_2020-04-29_3.tar.gz
6.写备份的脚本
[root@web01 ~]# cat /server/scripts/sync_back.sh
#!/bin/bash
#auto by chenhj 2019-1-11
#date file
export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin"
LD=/app/log
WD=/var/html/www
SCD=/server/scripts
LOCD=/etc/rc.local
CRND=/var/spool/cron/root
IP=`hostname -I|awk '{print $2}'`
TIME=`date +%F_%w`
if [ -d $LD -a $WD -a $SCD -a $LOCD -a $CRND ]
then
mkdir -p /backup/$IP
tar zcvf /backup/$IP/sync_$IP_$TIME.tar.gz $LD $WD $SCD $LOCD $CRND &>/dev/null
else
"tar not is fales "
fi
if [ -d /backup/$IP ]
then
rsync -azv /backup/ rsync_backup@172.16.1.41::backup --password-file=/etc/rsync.password >/dev/null 2>&1
else
echo "not is rsyns "
fi
find /backup/$IP/ -type f -name "*.tar.gz" -mtime +7 | xargs rm -f
7.在Rsync服务端查看结果
[root@backup ~]# ls -l /backup/172.16.1.7/sync_2020-04-29_3.tar.gz
-rw-r--r-- 1 rsync rsync 913 Apr 29 07:12 /backup/172.16.1.7/sync_2020-04-29_3.tar.gz
- 添加定时任务就可以了
[root@web01 /]# echo '#rsync bacup file' >>/var/spool/cron/root
[root@web01 /]# echo '00 22 * * * /bin/bash /server/scripts/sync_back.sh &>/dev/null'>>/var/spool/cron/root
[root@web01 /]# crontab -l
#time ntpdate
*/5 * * * * ntpdate ntp3.aliyun.com >/dev/null 2>&1
#rsync bacup file
00 22 * * * /bin/bash /server/scripts/sync_back.sh &>/dev/null
10.另一台服务器测试
[root@nfs01 ~]# mkdir -pv /server/scripts
[root@nfs01 ~]# mkdir -pv /var/html/www
mkdir: created directory ‘/var/html’
mkdir: created directory ‘/var/html/www’
[root@nfs01 ~]# mkdir -pv /app/logs
mkdir: created directory ‘/app’
mkdir: created directory ‘/app/logs’
[root@nfs01 ~]# mkdir /backup/
11.编写脚本
[root@nfs01 ~]# cat /server/scripts/sync_back.sh
#!/bin/bash
#auto by chenhj 2019-1-11
#date file
export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin"
LD=/app/log
WD=/var/html/www
SCD=/server/scripts
LOCD=/etc/rc.local
CRND=/var/spool/cron/root
IP=`hostname -I|awk '{print $2}'`
TIME=`date +%F_%w`
if [ -d $LD -a $WD -a $SCD -a $LOCD -a $CRND ]
then
mkdir -p /backup/$IP
tar zcvhf /backup/$IP/sync_$IP_$TIME.tar.gz $LD $WD $SCD $LOCD $CRND &>/dev/null
else
"tar not is fales "
fi
if [ -d /backup/$IP ]
then
find /backup/$IP/ -type f -name "*.tar.gz" | xargs md5sum >/backup/$IP/sync_$IP_$TIME.flag
rsync -azv /backup/ rsync_backup@172.16.1.41::backup --password-file=/etc/rsync.password >/dev/null 2>&1
else
echo "not is rsyns "
fi
find /backup/$IP/ -type f -name "*.tar.gz" -mtime +7 | xargs rm -f
12.检查
[root@nfs01 ~]# ls -l /backup/172.16.1.31/
total 8
-rw-r--r-- 1 root root 79 Apr 29 08:31 sync_2020-04-29_3.flag
-rw-r--r-- 1 root root 1354 Apr 29 08:31 sync_2020-04-29_3.tar.gz
13.服务端检查
[root@backup ~]# ll /backup/172.16.1.7/
total 4
-rw-r--r-- 1 rsync rsync 913 Apr 29 07:12 sync_2020-04-29_3.tar.gz
14.服务端配置邮件发送
[root@backup ]# vim /etc/mail.rc
在最后添加如下
set from=1010235677@qq.com
set smtp=smtp.qq.com
set smtp-auth-user=1010235677@qq.com
set smtp-auth-password=yyyksszmzndxbbbc
set smtp-auth=login
15 服务端脚本(待完善)
[root@backup /backup/172.16.1.31]# cat /server/scripts/back.sh
#!/bin/bash
#auto by chenhj 2019.1.11
#rsync back file
BKDIR=/backup/
if [ -d $BKDIR ]
then
find /backup/ -type f -name "*.tar.gz" -mtime +180 | xargs rm -f
find /backup/ -type f -name "*.flag" -mtime +1 | xargs rm -f
find /backup/ -type f -name "*_$(date +%F_%w).flag"|xargs md5sum -c >>/tmp/check.log
echo "rsync ok" | mailx -s "Rsync 备份" 1010235677@qq.com </tmp/check.log
else
echo "rsync not is ok" | mailx -s "Rsync 备份" 1010235677@qq.com </tmp/check.log
fi