服务器数据备份解决方案(2)

2.1.4 创建rsync的密码配置文件,默认也是空的
[root@backup ~]# cat /etc/rsync.password 
rsync_backup:linuxidc
[root@backup ~]# chmod 600  /etc/rsync.password 
[root@backup ~]# ls -l /etc/rsync.password 
-rw-------. 1 root root 20 11月 29 01:14 /etc/rsync.password

2.1.5 开启服务并开机自启动
[root@backup ~]# tail -1 /etc/rc.local 
/usr/bin/rsync --daemon

2.1.6 服务器端测试
[root@backup ~]# ps -ef|grep rsync|grep -v grep
root      23049      1  0 Jan15 ?        00:00:00 rsync --daemon
root@backup ~]# lsof -i:rsync
COMMAND  PID USER  FD  TYPE DEVICE SIZE/OFF NODE NAME
rsync  23049 root    3u  IPv4  56971      0t0  TCP *:rsync (LISTEN)
rsync  23049 root    5u  IPv6  56972      0t0  TCP *:rsync (LISTEN)
[root@backup ~]# netstat -lntup|grep rsync
tcp        0      0 0.0.0.0:873                0.0.0.0:*                  LISTEN      23049/rsync       
tcp        0      0 :::873                      :::*                        LISTEN      23049/rsync       
[root@backup ~]# telnet 172.16.1.41 873
Trying 172.16.1.41...
Connected to 172.16.1.41.
Escape character is '^]'.
@RSYNCD: 30.0
说明服务器端配置没有问题

2.2 验证rsync服务能否推送成功。
2.2.1 客户端配置
[root@web01 ~]# cat /etc/rsync.password 
linuxidc
[root@web01 ~]# ls -ld /etc/rsync.password 
-rw------- 1 root root 7 1月  18 16:01 /etc/rsync.password

2.2.2 客户端推送
[root@web01 backup]# rsync -avz /backup/  rsync_backup@172.16.1.41::backup/ --password-file=/etc/rsync.password   
sending incremental file list
./
1
10
2
3
4
5
6
7
8
9
说明客户端推送成功

2.2.3 Web服务器实现打包,备份,推送,删除。
2.2.3.1 命令行备份
[root@web01 backup]# mkdir /var/html/www -p
[root@web01 scripts]# mkdir /app/logs -p
[root@web01 /]# tar zcvfh /backup/sys_config_$(date +%F).tar.gz  ./var/spool/cron/root  ./server/scripts/ ./etc/rc.local 
./var/spool/cron/root
./server/scripts/
./server/scripts/bak.sh
./etc/rc.local
[root@web01 /]# tar zcvfh /backup/webdata_config_$(date +%F).tar.gz ./var/html/www/
./var/html/www/
./var/html/www/4
./var/html/www/7
./var/html/www/5
./var/html/www/6
./var/html/www/2
./var/html/www/8
./var/html/www/3
./var/html/www/10
./var/html/www/1
./var/html/www/9
[root@web01 /]# tar zcvfh /backup/access_log_config_$(date +%F).tar.gz ./app/logs/ 
./app/logs/
./app/logs/f
./app/logs/c
./app/logs/a
./app/logs/d
./app/logs/e
./app/logs/b
./app/logs/g

2.2.3.2 推送:
[root@web01 backup]# rsync -avz /backup/ rsync_backup@172.16.1.41::backup/ --password-file=/etc/rsync.password

2.2.3.3 删除:
[root@web01 backup]# find /backup/ -type f -mtime +7|xargs rm -rf

2.2.3.4 全部放到脚本里:
cat  /server/scripts/bak.sh
#!/bin/sh
export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
IP=$(ifconfig eth1|awk -F "[ :]+" 'NR==2{print $4}')
Bak_Path=/backup
mkdir $Bak_Path/$IP -p
if [ $(date +%w) -eq 2  ];then
date="$(date +%F -d "-1day")_week1"
else
date="$(date +%F -d "-1day")"
fi
cd / &&\
tar zcfh $Bak_Path/$IP/sys_config_${date}.tar.gz ./var/spool/cron ./etc/rc.local ./server/sc
ripts  &&\
tar zcf $Bak_Path/$IP/webdata_${date}.tar.gz var/html/www/  &&\
tar zcf $Bak_Path/$IP/access_log_${date}.tar.gz app/logs/  &&\
find $Bak_Path -type f -name "*.tar.gz"|xargs md5sum >$Bak_Path/$IP/flag_${date} &&\
rsync -az $Bak_Path/ rsync_backup@172.16.1.41::backup/ --password-file=/etc/rsync.password
&&\
###delete data before 7days ago by chen at 20170118
find $Bak_Path -type f  -mtime +7|xargs rm -f
~

2.2.3.5 每天00点定时任务推送
[root@web01 backup]# crontab -l
00 00 * * * /bin/sh /server/scripts/bak.sh >/dev/null 2>&1  完成客户端的定时任务推送工作

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/84ee371ac8a243f8f1c1d70e31926048.html