(1)以下操作都是在backup服务器
1安装
[root@backup ~]# rsync --version
rsync version 3.1.2 protocol version 31
[root@backup ~]# yum install rsync(不需要) 安装rsync包组
2配置配置文件/etc/rsyncd.conf
备份
cp /etc/rsyncd.conf{,.ori} 给文件先备份
cat>/etc/rsyncd.conf<<EOF
#rsync_config_______________start
#created by oldboy
#site: http://www.oldboyedu.com 广告
uid = rsync
gid = rsync
use chroot = no
fake super = yes
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 rsync默认用户
secrets file = /etc/rsync.password
[backup]
comment = welcome to oldboyedu backup!
path = /backup/
EOF
完成 cat查
3 创建用户和备份目录
[root@backup ~]# useradd rsync 创建个rsync用户
[root@backup ~]# id rsync 查看
uid=1001(rsync) gid=1001(rsync) 组=1001(rsync) 确认
[root@backup ~]# mkdir -p /backup 建个backup目录
[root@backup ~]# chown -R rsync.rsync /backup/ UID GID改成 rsync
[root@backup ~]# ls -ld /backup/ 查看 确认
drwxr-xr-x 2 rsync rsync 6 4月 15 12:12 /backup/
4 启动和检查
systemctl start rsyncd 开启rsync服务
systemctl enable rsyncd
systemctl status rsyncd 查看确认
[root@backup ~]# ps -ef|grep sync|grep -v grep #检查进程
[root@backup ~]# netstat -lntup|grep 873 #检查端口
[root@backup ~]# lsof -i :873 #检查端口
5 配置密码文件
[root@backup ~]# echo "rsync_backup:oldboy" > /etc/rsync.password 在etc下建个密码文件 默认用户密码为oldboy
[root@backup ~]# chmod 600 /etc/rsync.password 给这文件600权限
[root@backup ~]# cat /etc/rsync.password
rsync_backup:oldboy
[root@backup ~]# ls -l /etc/rsync.password
-rw------- 1 root root 20 4月 15 11:51 /etc/rsync.password
rsync服务端配置完成。
(2)以下操作都是在客户端服务器
以下方法2选1
方法1:认证密码文件
[root@nfs01 ~]# echo "oldboy" > /etc/rsync.password
[root@nfs01 ~]# chmod 600 /etc/rsync.password
[root@nfs01 ~]# cat /etc/rsync.password
oldboy
[root@nfs01 ~]# ls -l /etc/rsync.password
-rw------- 1 root root 7 4月 15 11:55 /etc/rsync.password
方法2:
[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
注意:之前如果错了,改完配置,要重启服务:
[root@backup ~]# systemctl restart rsyncd
测试下客户端NFS可以推到服务端BACKUP:
[root@nfs01 ~]# rsync -avz /etc rsync_backup@172.16.1.41::backup 服务端查有就完成了
测试增量
[root@nfs01 ~]# touch /etc/oldboy.txt
[root@nfs01 ~]# rsync -avz /etc rsync_backup@172.16.1.41::backup
增加新模块
1)最简单的实现,可以在配置文件结尾加如下内容(特殊底纹部分):
[root@backup ~]# cat /etc/rsyncd.conf
#rsync_config_______________start
#created by oldboy
#site: http://www.oldboyedu.com
uid = rsync
gid = rsync
use chroot = no
fake super = yes
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/
[data] #<==新模块。
path = /data/ #<==新备份目录。
2)建立目录并授权。
[root@backup ~]# mkdir -p /data
[root@backup ~]# chown -R rsync.rsync /data
[root@backup ~]# ls -ld /data
drwxr-xr-x 2 rsync rsync 6 4月 16 10:04 /data
3)重启rsync服务(只要修改配置,就考虑重载服务)
[root@backup ~]# systemctl restart rsyncd
4)从客户端访问测试
[root@nfs01 /etc]# rsync -avz /etc rsync_backup@172.16.1.41::data
[root@backup ~]# ls /data
etc
老师题目
第一个里程碑:
41搭建好rsync服务,并在31、7上测试成功。
第二个里程碑 开发脚本打包备份
/backup 备份目录
/var/spool/cron/root /etc/rc.local /server/scripts /var/html/www /app/logs 需要备份的内容
模拟创建:
[root@nfs01 ~]# mkdir -p /server/scripts
[root@web01 ~]# mkdir -p /server/scripts /var/html/www /app/logs
web01:
[root@web01 ~]# mkdir -p /backup
[root@web01 ~]# ls -ld /backup/
drwxr-xr-x 2 root root 6 4月 16 11:36 /backup/
[root@web01 /]# tar zcvhf /backup/bak_$(date +%F_%w).tar.gz /var/spool/cron/root /etc/rc.local /server/scripts /var/html/www /app/logs 所需要的备份的命令 外面测试完复制到下面脚本
tar: 从成员名中删除开头的“/”
/var/spool/cron/root
/etc/rc.local
/server/scripts/
/var/html/www/
/app/logs/
[root@web01 /]# ls /backup/
bak_2019-04-16_2.tar.gz
写脚本
[root@web01 /]# mkdir /server/scripts/ -p
[root@web01 /]# cd /server/scripts/
[root@web01 /server/scripts]# vim bak.sh
/bin/tar zchf /backup/$IP/bak_$(date +%F_%w).tar.gz /var/spool/cron/root /etc/rc.local /server/scripts /var/html/www /app/logs &>/dev/null 编辑它
[root@web01 /server/scripts]# /bin/sh /server/scripts/bak.sh
[root@web01 /server/scripts]#ls /backup/ 查看是不曾加了定时任务的文件
[root@web01 /server/scripts]# cat bak.sh 查脚本的内容
定时任务 未完下面是完整 的
#!/bin/sh which PATH查所有的全路径
export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin" 加这个不用加全路径
export RSYNC_PASSWORD=oldboy
IP=$(hostname -i)
mkdir -p /backup/$IP
#1.打包
tar zchf /backup/$IP/bak_$(date +%F_%w).tar.gz /var/spool/cron/root /etc/rc.local /server/scripts /var/html/www /app/logs &>/dev/null &&\
#2.删除
find /backup/ -type f -name "*.tar.gz" -mtime +7|xargs rm -f &&\
#3.推送
rsync -az /backup/ rsync_backup@172.16.1.41::backup &>/dev/null
[root@web01 /server/scripts]# /bin/sh /server/scripts/bak.sh
[root@web01 /server/scripts]# ls /backup/
172.16.1.7
[root@web01 /server/scripts]# ls /backup/172.16.1.7/
bak_2019-04-17_3.tar.gz
[root@bakup ~]# ls /backup/
172.16.1.7
[root@bakup ~]# ls /backup/172.16.1.7/
bak_2019-04-17_3.tar.gz
[root@nfs01 /server/scripts]# crontab -e 最后放定时任务里
##bak confing by oldboy at 2020.10.10
00 00 * * * /bin/sh /server/scripts/bak.sh >/dev/null 2>&1 0点每天执行任务
确保备份完整
做flag和采集指纹 以下是示例 无用
[root@nfs01 /server/scripts]# #采集人的指纹
[root@nfs01 /server/scripts]# md5sum oldboy.txt >zhiwen.log
[root@nfs01 /server/scripts]# cat zhiwen.log
348bd3ce10ec00ecc29d31ec97cd5839 oldboy.txt
[root@nfs01 /server/scripts]# #校验
[root@nfs01 /server/scripts]# md5sum -c zhiwen.log
oldboy.txt: 确定
[root@web01 /server/scripts]# cat bak.sh 完整版 bak.sh脚本,放定时任务里00点执行的
[root@web01 /server/scripts]# vim bak.sh
#!/bin/sh
export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin"
export RSYNC_PASSWORD=oldboy
IP=$(hostname -i)
mkdir -p /backup/$IP
#1.打包
tar zchf /backup/$IP/bak_${IP}_$(date +%F_%w).tar.gz /var/spool/cron/root /etc/rc.local /server/scripts /var/html/www /app/logs &>/dev/null &&\
touch /backup/$IP/bak_${IP}_$(date +%F_%w).flag &&\
#采集指纹
find /backup/ -type f -name "*.tar.gz"|xargs md5sum >/backup/$IP/bak_${IP}_$(date +%F_%w).flag &&\
#2.删除
find /backup/ -type f -name "*.tar.gz" -mtime +7|xargs rm -f &&\
#3.推送
rsync -az /backup/ rsync_backup@172.16.1.41::backup &>/dev/null
客户端完
backup服务器,
[root@bakup /backup/172.16.1.31]# mkdir /server/scripts/
mkdir: 无法创建目录"/server/scripts/": 文件已存在
[root@bakup /backup/172.16.1.31]# cd /server/scripts/
[root@bakup /server/scripts]# ll
总用量 0
[root@bakup /server/scripts]# vim del.sh
[root@bakup /server/scripts]# vim del.sh
#!/bin/sh
export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin"
#1.删除
find /backup/ -type f -name "*.tar.gz" -mtime 180|xargs rm -f &&\
find /backup/ -type f ! -name "*_1.tar.gz" -mtime +7|xargs rm -f
find /backup/ -type f -name "*.flag"|xargs md5sum -c > /tmp/fail.log
mail -s "$(date +%F_%T) backup data error" 83016753@qq.com </tmp/fail.log
[root@bakup /server/scripts]# vim mail.rc
set from=18947117523@163.com 自己邮箱号
set smtp=smtp.163.com 不动
set smtp-auth-user=18947117523 邮箱号@前
set smtp-auth-password=abc123 授权码
set smtp-auth=login 不动
我的
set from=bj_yueguolin@163.com
set smtp=smtp.163.com
set smtp-auth-user=bj_yueguolin
set smtp-auth-password=q19880722
set smtp-auth=login