rsync

2020-02-08  本文已影响0人  mrwangjian

[root@backup ~]# scp /etc/hostname 172.16.1.31:/tmp
root@172.16.1.31's password:
hostname 100% 7 15.4KB/s 00:00
[root@backup ~]# #本地 /etc 目录 推送到 nfs01 的/tmp 下面
[root@backup ~]#

rsync -av /etc nfs01:/mnt

前面参数是source , 后面是target

推过去

[root@backup ~]# rsync -av /etc nfs01:/mnt
root@nfs01's password:
sending incremental file list
etc/hosts

sent 46,602 bytes received 658 bytes 13,502.86 bytes/sec
total size is 26,670,532 speedup is 564.34

拉过来

[root@backup ~]# rsync -av nfs01:/etc/hostname /tmp/
root@nfs01's password:
receiving incremental file list
hostname

sent 43 bytes received 100 bytes 40.86 bytes/sec
total size is 6 speedup is 0.04
[root@backup ~]# cat /tmp/hostname
nfs01

rsync服务端配置文件

[root@backup ~]# cat /etc/rsyncd.conf

created by oldboy 15:01 2009-6-5

##rsyncd.conf start##
fake super =yes 
uid = rsync
gid = rsync
use chroot = no
max connections = 2000
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 = 10.0.0.0/24
#hosts deny = 0.0.0.0/32
auth users = rsync_backup
secrets file = /etc/rsync.password
#####################################
[data]
comment = www by old0boy 14:18 2012-1-13
path = /data 

read error: Connection reset by peer

[root@backup ~]# rsync -av /etc/hosts rsync_backup@10.0.0.41::data
sending incremental file list
rsync: read error: Connection reset by peer (104)
rsync error: error in socket IO (code 10) at io.c(785) [sender=3.1.2]

配置文件

auth failed on module data

[root@backup /data]# rsync /etc/hosts rsync_backup@10.0.0.41::data
Password:
@ERROR: auth failed on module data
data模块的密码错误了
1.密码错误
2./etc/rsync.password 内容是否正确
rsync error: error starting client-server protocol (code 5) at main.c(1648) [sender=3.1.2]

看日志 发现是密码文件权限的问题

2019/05/18 14:52:37 [9000] secrets file must not be other-accessible (see strict modes option)
2019/05/18 14:52:37 [9000] auth failed on module data from UNKNOWN (10.0.0.41) for rsync_backup: ignoring secrets file

2019/05/18 15:52:20 [3084] auth failed on module data from nfs01 (172.16.1.31) for rsybackup: no matching rule

Operation not permitted

root@backup01 ~]# rsync -av /etc/hosts rsync_backup@10.0.0.41::data
Password:
sending incremental file list
hosts
rsync: chgrp ".hosts.YudpXN" (in data) failed: Operation not permitted (1)

sent 95 bytes received 128 bytes 14.39 bytes/sec
total size is 349 speedup is 1.57
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1178) [sender=3.1.2]

[root@backup01 ~]# ll -d /data /tmp
drwxr-xr-x 2 rsync rsync 19 May 18 14:56 /data
drwxr-xr-x. 2 root root 6 May 18 14:34 /tmp
[root@backup01 ~]# chmod 1777 /tmp
[root@backup01 ~]# rsync -av /etc/hosts rsync_backup@10.0.0.41::data
Password:
sending incremental file list
hosts
rsync: chgrp ".hosts.XGfttQ" (in data) failed: Operation not permitted (1)

sent 95 bytes received 128 bytes 40.55 bytes/sec
total size is 349 speedup is 1.57
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1178) [sender=3.1.2]

[root@backup01 ~]# ll -d /data
drwxr-xr-x 2 rsync rsync 19 May 18 14:56 /data
[root@backup01 ~]# ll -d /data /tmp
drwxr-xr-x 2 rsync rsync 19 May 18 14:56 /data
drwxr-xr-x. 2 root root 6 May 18 14:34 /tmp
[root@backup01 ~]# chmod 1777 /tmp
[root@backup01 ~]# rsync -av /etc/hosts rsync_backup@10.0.0.41::data
Password:
sending incremental file list
hosts
rsync: chgrp ".hosts.XGfttQ" (in data) failed: Operation not permitted (1)

sent 95 bytes received 128 bytes 40.55 bytes/sec
total size is 349 speedup is 1.57
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1178) [sender=3.1.2]
[root@backup01 ~]#
[root@backup01 ~]#
[root@backup01 ~]#
[root@backup01 ~]# vim /etc/rsyncd.conf
[root@backup01 ~]# systemctl restart rsyncd
[root@backup01 ~]# rsync -av /etc/hosts rsync_backup@10.0.0.41::data
Password:
sending incremental file list
hosts

sent 95 bytes received 49 bytes 12.52 bytes/sec
total size is 349 speedup is 2.42

Permission denied

[root@backup ~]# rsync -av /etc/hosts rsync_backup@10.0.0.41::data
Password:
sending incremental file list
rsync: ERROR: cannot stat destination "." (in data): Permission denied (13)
rsync error: errors selecting input/output files, dirs (code 3) at main.c(635) [Receiver=3.1.2]
[root@backup ~]# ll -d /data/
drw-------. 2 rsync rsync 6 May 18 14:32 /data/

echo '123456'>/etc/rsync.password
chmod 600 /etc/rsync.password
rsync -avz -P /etc/sysconfig rsync_backup@172.16.1.41::data --password-file /etc/rsync.password

企业案例:rsync上机实战考试题:
web01

backup

某公司里有一台Web服务器,里面的数据很重要,但是如果硬盘坏了,数据就会丢失,现在领导要求你把数据在其他机器上做一个周期性定时备份。要求如下:
每天晚上12点整在Web服务器A(web01 )上打包备份网站程序目录并通过rsync命令推送到服务器B(backup)上备份保留
(备份思路可以是先在本地按日期打包,然后再利用rsync推到备份服务器上)。

具体要求如下:
1)Web服务器A和备份服务器B的备份目录必须都为/backup。
2)Web服务器站点目录假定为(/var/www/html)。
3)Web服务器本地仅保留7天内的备份。
4)每天晚上12点把备份/backup 推送到 备份服务器B上
5)备份服务器上每周六的数据都保留,其他备份仅保留180天备份。

6)备份服务器上检查备份结果是否正常,并将每天的备份结果发给管理员信箱。

web服务器 上面的脚本

[root@web01 ~]# cat /server/scripts/bak-web01.sh

#!/bin/bash

ip=`hostname -I |awk '{print $NF}'`
mkdir -p /backup/$ip
#backup configure 
tar zchf /backup/$ip/conf-`date +%F-%w`.tar.gz /etc/  /var/spool/cron/  /var/www/html/
#push rsync 
rsync -avz /backup/   rsync_backup@backup::backup   --password-file=/etc/rsync.password
#del 
#find /backup/ -type f  -name "*.tar.gz" -mtime +7|xargs rm 

[root@web01 ~]# sh /server/scripts/bak-web01.sh
[root@web01 ~]# sh -x /server/scripts/bak-web01.sh
++ hostname -I
++ awk '{print $NF}'

[root@web01 ~]# crontab -l

backup configure

00 00 * * * sh /server/scripts/bak-web01.sh >/dev/null 2>&1

[root@web01 ~]# mkdir -p /var/www/html
[root@web01 ~]# tar zchf /backup/172.16.1.7/conf-date +%F-%w.tar.gz /etc/ /var/spool/cron/ /var/www/html/
tar: Removing leading `/' from member names
[root@web01 ~]# tar tf /backup/172.16.1.7/conf-2019-05-18-6.tar.gz

echo 123456 > /etc/rsync.password
chmod 600 /etc/rsync.password

backup.oldboyedu.com

[root@web01 ~]# cat /server/scripts/bak-web01.sh

!/bin/bash

ip=hostname -I |awk '{print $NF}'
mkdir -p /backup/$ip

backup configure

cd / &&
tar zchf /backup/$ip/conf-date +%F-%w.tar.gz etc/ var/spool/cron/ var/www/html/

md5sum

find /backup/ip/ -type f -name "*.tar.gz" |xargs md5sum >/backup/ip/check.md5

push rsync

rsync -az /backup/ rsync_backup@backup::backup --password-file=/etc/rsync.password

del

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

Linux下面发邮件
申请 163 126邮箱 不用QQ邮箱

Linux下面配置
启动邮件服务postfix #backup服务器

总结:
1.综合架构结构 画出来
2.第1个服务-备份服务rsync(守护进程模式)
3.全网备份项目
4.架构和排错流程

上一篇下一篇

猜你喜欢

热点阅读