家具网站模板下载,怎么用ftp备份网站,网站建设完提交百度,百度网站的优缺点rsync搭建全网备份 1. 总体概述1.1 目标1.2 简易指导图1.3 涉及工具或命令1.4 环境 2. 实施2.1 配置备份服务器2.2 备份文件准备2.3 整合命令2.4 扩展功能 1. 总体概述
1.1 目标
本次搭建目标#xff1a;
每天定时把服务器数据备份到备份服务器备份完成后进行校验把过期数据… rsync搭建全网备份 1. 总体概述1.1 目标1.2 简易指导图1.3 涉及工具或命令1.4 环境 2. 实施2.1 配置备份服务器2.2 备份文件准备2.3 整合命令2.4 扩展功能 1. 总体概述
1.1 目标
本次搭建目标
每天定时把服务器数据备份到备份服务器备份完成后进行校验把过期数据删除普通服务器数据保留7天备份服务器数据保留1年完成后邮件通知
1.2 简易指导图 1.3 涉及工具或命令
rsysnc备份工具守护进程模式cron定时任务md5sumMD5校验工具mailx邮件tar、find、date等等
1.4 环境
服务器ip系统版本说明backup-svr-01192.168.202.128CentOS Linux release 7.9.2009 (Core)备份服务器data-svr-01192.168.202.129CentOS Linux release 7.9.2009 (Core)数据服务器
2. 实施
在每个步骤中会把具体的需求细化分解
2.1 配置备份服务器
创建备份目录
[rootbackup-svr-01 ~]# chown rsync.rsync /backup/
[rootbackup-svr-01 ~]# ll /backup/ -d
drwxr-xr-x 2 rsync rsync 6 Aug 29 16:39 /backup/创建rsync进程使用的用户
[rootbackup-svr-01 ~]# useradd -s /sbin/nologin -M rsync
[rootbackup-svr-01 ~]# id rsync
uid1003(rsync) gid1004(rsync) groups1004(rsync)客户端连到备份服务器用rsync用户访问权限访问数据这个用户只提供访问权限所以不需要登录shell
创建客户端访问备份服务器的账户
[rootbackup-svr-01 ~]# echo rsync_bak_01:rsync /etc/rsync.passwd
[rootbackup-svr-01 ~]# cat /etc/rsync.passwd
rsync_bak_01:rsync
[rootbackup-svr-01 ~]# chmod 400 /etc/rsync.passwd
[rootbackup-svr-01 ~]# ll /etc/rsync.passwd
-r-------- 1 root root 19 Aug 29 16:51 /etc/rsync.passwd编译rsync的配置文件 没有安装rsync可以通过yum install -y rsync安装 [rootbackup-svr-01 ~]# cat /etc/rsyncd.conf
# /etc/rsyncd: configuration file for rsync daemon modeuid rsync
gid rsync
use chroot no
max connections 4
pid file /var/run/rsyncd.pid
lock file /var/run/rsync.lock
log file /var/log/rsyncd.log
# exclude lostfound/
# transfer logging yes
timeout 900
# ignore nonreadable yes
# dont compress *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2
fake super yes
[backup]
path /backup/ #备份到服务器的目录
ignore errors
read only false
list false
hosts allow 192.168.202.0/24 #允许的IP范围
hosts deny 0.0.0.0/32 #其他IP都禁止
auth users rsync_bak_01
secrets file /etc/rsync.passwd配置完成后重启rsyncd服务
[rootbackup-svr-01 ~]# systemctl restart rsyncd测试rsync
首先在客户端配置密码文件
[rootdata-svr-01 ~]# cat /etc/rsync.passwd
rsync传输测试
[rootdata-svr-01 ~]# rsync -avz /etc/hosts rsync_bak_01192.168.202.128::backup --password-file/etc/rsync.passwd
sending incremental file list
hostssent 187 bytes received 43 bytes 153.33 bytes/sec
total size is 254 speedup is 1.10[rootbackup-svr-01 ~]# ll /backup/
total 4
-rw-r--r-- 1 rsync rsync 254 May 9 2023 hosts2.2 备份文件准备
这里我们在普通服务器客户端把一些常用文件重要数据等内容进行备份视具体情况而定
本次示例中以/etc目录为例
创建备份数据存放目录
[rootdata-svr-01 ~]# mkdir /data这里我们把备份数据存放在/data/ip目录中
mkdir -p /data/ip a s ens33|sed -rn 3 s#^[^[:digit:]](.*)/.*#\1#gp取IP可以有多种方式例如
[rootdata-svr-01 ~]# hostname -I|awk {print $1}
192.168.202.129
[rootdata-svr-01 ~]# ip a s ens33|awk BEGIN{FSinet |/24} NR3{print $2}
192.168.202.129打包数据并以时间进行区分
打包数据
[rootdata-svr-01 ~]# tar zcf /data/ip a s ens33|sed -rn 3 s#^[^[:digit:]](.*)/.*#\1#gp/etc.date %F.tar.gz /etc/
tar: Removing leading / from member names查看
[rootdata-svr-01 ~]# tree /data/
/data/
└── 192.168.202.129└── etc.2024-08-29.tar.gz1 directory, 1 file后面我们会把这些命令整理到脚本中目前只是测试
2.3 整合命令
我们把前面用到的命令整合到脚本中并进行测试
编写脚本
[rootdata-svr-01 scripts]# cat etc_bak2svr.sh
#!/bin/bash
#author yurq#set -e. /etc/profileipip a s ens33|sed -rn 3 s#^[^[:digit:]](.*)/.*#\1#gp
timedate %F
svr192.168.202.128if [ ! -d /data/$ip ];thenmkdir -p /data/$ip
fitar zcf /data/$ip/etc.$time.tar.gz /etc/if [ $? -ne 0 ];thenecho tar etc failed
fils /data/$ip/etc.$time.tar.gzif [ $? -ne 0 ];thenecho etc.$time.tar.gz lost found.
firsync -avz /data/$ip/etc.$time.tar.gz rsync_bak_01$svr::backup --password-file/etc/rsync.passwdif [ $? -ne 0 ];thenecho rsync failed
fi测试脚本 客户端
[rootdata-svr-01 scripts]# rm -rf /data/*
[rootdata-svr-01 scripts]# sh etc_bak2svr.sh
tar: Removing leading / from member names
/data/192.168.202.129/etc.2024-08-29.tar.gz
sending incremental file list
etc.2024-08-29.tar.gzsent 10,207,860 bytes received 43 bytes 4,083,161.20 bytes/sec
total size is 10,411,198 speedup is 1.02服务器
[rootbackup-svr-01 ~]# ll /backup/
total 10168
-rw-r--r-- 1 rsync rsync 10411198 Aug 29 19:42 etc.2024-08-29.tar.gz这并不是我们想要的rsync我们在使用的时候应该把ip构建的目录也带上一起传过去
修改脚本
rsync -avz /data/$ip rsync_bak_01$svr::backup --password-file/etc/rsync.passwd把这行给了就可以了注意ip后不要加/
清理环境重新上传
[rootdata-svr-01 scripts]# sh etc_bak2svr.sh
tar: Removing leading / from member names
/data/192.168.202.129/etc.2024-08-29.tar.gz
sending incremental file list
192.168.202.129/
192.168.202.129/etc.2024-08-29.tar.gzsent 10,207,905 bytes received 47 bytes 6,805,301.33 bytes/sec
total size is 10,411,198 speedup is 1.02[rootbackup-svr-01 ~]# tree /backup/
/backup/
└── 192.168.202.129└── etc.2024-08-29.tar.gz1 directory, 1 file校验文件 写到这笔者想起来还应该带上MD5校验文件修改脚本
[rootdata-svr-01 data]# cat /scripts/etc_bak2svr.sh
#!/bin/bash
#author yurq#set -e. /etc/profileipip a s ens33|sed -rn 3 s#^[^[:digit:]](.*)/.*#\1#gp
timedate %F
svr192.168.202.128if [ ! -d /data/$ip ];thenmkdir -p /data/$ip
fitar zcf /data/$ip/etc.$time.tar.gz /etc/if [ $? -ne 0 ];thenecho tar etc failed
fils /data/$ip/etc.$time.tar.gzif [ $? -ne 0 ];thenecho etc.$time.tar.gz lost found.
firsync -avz /data/$ip rsync_bak_01$svr::backup --password-file/etc/rsync.passwdif [ $? -ne 0 ];thenecho rsync failed
ficd /data/
md5sum $ip/etc.$time.tar.gz $ip/etc.md5if [ $? -ne 0 ];thenecho make md5 failed
fi验证
[rootdata-svr-01 data]# sh /scripts/etc_bak2svr.sh
tar: Removing leading / from member names
/data/192.168.202.129/etc.2024-08-29.tar.gz
sending incremental file list
192.168.202.129/
192.168.202.129/etc.2024-08-29.tar.gz
192.168.202.129/etc.md5sent 3,513 bytes received 19,446 bytes 4,174.36 bytes/sec
total size is 10,411,270 speedup is 453.47
[rootdata-svr-01 data]# cat 192.168.202.129/etc.md5
f9a218d7b059fa412fdecef06d27f469 192.168.202.129/etc.2024-08-29.tar.gz[rootbackup-svr-01 ~]# tree /backup/
/backup/
└── 192.168.202.129├── etc.2024-08-29.tar.gz└── etc.md51 directory, 2 files下面编辑定时任务每天晚上2点备份 首先编辑一个任务每分钟的任务进行测试毕竟不能等到晚上2点再验证
[rootdata-svr-01 data]# cat /var/spool/cron/root
* * * * * sh /scripts/etc_bak2svr.sh /dev/null[rootdata-svr-01 data]# tail -f /var/log/cron
...
Aug 30 03:26:01 data-svr-01 CROND[65599]: (root) CMD (sh /scripts/etc_bak2svr.sh /dev/null)
...
[rootbackup-svr-01 ~]# tree /backup/
/backup/
└── 192.168.202.129├── etc.2024-08-29.tar.gz├── etc.2024-08-30.tar.gz└── etc.md51 directory, 3 files任务正常完成了不过发现一个问题md5文件把之前的冲掉了所以需要修改脚本
md5sum $ip/etc.$time.tar.gz $ip/etc.$time.md5制作md5的时候加上日期
清除环境再次验证。另外发现md5在上传之后增加的实际是不对的调整执行顺序。
[rootdata-svr-01 scripts]# cat etc_bak2svr.sh
#!/bin/bash
#author yurq#set -e. /etc/profileipip a s ens33|sed -rn 3 s#^[^[:digit:]](.*)/.*#\1#gp
timedate %F
svr192.168.202.128if [ ! -d /data/$ip ];thenmkdir -p /data/$ip
fitar zcf /data/$ip/etc.$time.tar.gz /etc/if [ $? -ne 0 ];thenecho tar etc failed
fils /data/$ip/etc.$time.tar.gzif [ $? -ne 0 ];thenecho etc.$time.tar.gz lost found.
ficd /data/
md5sum $ip/etc.$time.tar.gz $ip/etc.$time.md5if [ $? -ne 0 ];thenecho make md5 failed
firsync -avz /data/$ip rsync_bak_01$svr::backup --password-file/etc/rsync.passwdif [ $? -ne 0 ];thenecho rsync failed
fi
[rootbackup-svr-01 ~]# tree /backup/
/backup/
└── 192.168.202.129├── etc.2024-08-30.md5└── etc.2024-08-30.tar.gz1 directory, 2 files2.4 扩展功能
添加清除过期备份客户端备份保留7天
[rootdata-svr-01 scripts]# mkdir /tmp/etc_backup
[rootdata-svr-01 scripts]# ll /home/
total 0
[rootdata-svr-01 scripts]# mkdir /home/yurq
[rootdata-svr-01 scripts]# cp -r /etc/* /home/yurq/
[rootdata-svr-01 scripts]# find /etc/ -type f -mtime 7|xargs -I file mv file /tmp/etc_backup/
[rootdata-svr-01 scripts]# ll /tmp/etc_backup/
total 22300
-rwxr-xr-x. 1 0 0 8702 Jul 28 2020 00_header
-rw-r--r--. 1 0 0 232 May 9 2023 00-keyboard.conf
-rwxr-xr-x. 1 0 0 175 May 22 2020 00-netreport
-rwxr-xr-x. 1 0 0 1043 Mar 21 2019 00_tuned
-rwxr-xr-x. 1 0 0 232 Jul 28 2020 01_users
-rwxr-xr-x. 1 0 0 392 Aug 8 2019 0anacron
...创建目录并拷贝/etc/内容进行测试完成后把查找目录改为备份数据目录
find /data/ -type f -mtime 7|xargs -I file mv file /tmp/etc_backup/把命令添加到脚本中
添加邮件通知
echo test |mailx -s test xxx163.com这里最好申请企业邮箱发送太多条邮件到个人邮箱可能被邮箱系统拦截