centos7.5全网备份项目
客户端web01脚本:
[root@web01 scripts]# cat backup.sh
#!/bin/bash
Backup_dir="/backup" --------Backup_dir为变量
IP_info=$(ifconfig|grep “172” |awk ‘{print $2}’) --------IP_info是变量,为本机的内网的地址
#create backup dir ----------创建一个备份目录及md5sum算法认证文件,确保数据完整性
mkdir -p $ Backup_dir/$IP_info ------ $ Backup_dir后面没有空格(包括后面)
touch $ Backup_dir/$IP_info/finger.txt
#tar backup date --------创建备份数据(定时任务文件,脚本文件,站点网站目录,站点网站日志)
cd /
tar -zchf $ Backup_dir/$ IP_info/web01_backup_$(date +%F_week%w).tar.gz ./var/spool/cron/root ./etc/sysconfig/iptables ./etc/rc.local ./server/scripts/
tar -zchf $ Backup_dir/$ IP_info/www_backup_$(date +%F_week%w).tar.gz ./var/html/www/
tar -zchf $ Backup_dir/$ IP_info/www_log_backup_$(date +%F_week%w).tar.gz ./app/logs/
#create md5sum finger.txt file --------重定向md5sum内容到finger.txt文件
find $ Backup_dir -type f -mtime -1 ! -name “fing*”|xargs md5sum >$ Backup_dir/$IP_info/finger.txt
#backup push data info -------------客服端把备份目录推向备份服务器
rsync -az $Backup_dir/ rsync_backup@172.16.1.41::backup --password-file=/etc/rsync.password
客服端nfs01脚本:
#!/bin/bash
Backup_dir="/backup"
IP_info=$(ifconfig|grep “172” |awk ‘{print $2}’)
#create backup dir
mkdir -p $ Backup_dir/$IP_info
touch $ Backup_dir/$IP_info
#tar backup date
cd /
tar -zchf $ Backup_dir/$ IP_info/nfs01_backup_$(date +%F_week%w).tar.gz ./var/spool/cron/root ./etc/sysconfig/iptables ./etc/rc.local ./server/scripts
#create md5sum finger.txt file
find $ Backup_dir -type f -mtime -1 ! -name “fing*”|xargs md5sum >$ Backup_dir/$IP_info/finger.txt
#backup push data info
rsync -az $Backup_dir/ rsync_backup@172.16.1.41::backup --password-file=/etc/rsync.password
服务端backup脚本
#!/bin/bash
#check backup_finger.txt data ---------md5sum -c 验证客户端发送过来的文件的完整性
find /backup/ -type -f ! -name “finger.txt” |xargs md5sum -c >/tmp/check2.txt
#send check mail -------发送认证文件的结果到运维人员邮箱
/usr/bin/cat /tmp/check.txt |mail -s “check_md5sum” 1066073302@qq.com
脚本执行结果:
定时备份:
web01
[root@web01 scripts]# crontab -e
#time sync by yanhe at 2020-04-25
*/5 * * * * /usr/sbin/ntpdate ntp1.aliyun.com &>/dev/null
0 0 * * * /usr/bin/sh /server/scripts/backup.sh &>/dev/null
nfs01
[root@nfs01 scripts]# crontab -e
#time sync by yanhe at 2020-04-25
*/5 * * * * /usr/sbin/ntpdate ntp1.aliyun.com &>/dev/null
0 0 * * * /usr/bin/sh /server/scripts/backup.sh &>/dev/null
backup
[root@backup scripts]# crontab -e
#time sync by yanhe at 2020-04-25
*/5 * * * * /usr/sbin/ntpdate ntp1.aliyun.com &>/dev/null
0 5 * * * /usr/bin/sh /server/scripts/backup.sh &>/dev/null
注:服务器的定时任务要晚于客户端的定时任务