Linux系统crondrsync打包备份数据操作实战
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux系统crondrsync打包备份数据操作实战相关的知识,希望对你有一定的参考价值。
Linux系统crond、rsync、打包备份数据操作实战
前面介绍了系统中定时任务、rsync、打包等各种操作,同时也介绍了实际生产环境中的各类案例,今天我们结合这几种功能做一次实战(备份数据)
一:项目背景
某公司WEB服务器数据非常重要,但是如果硬件出现故障,数据就会丢失,人工备份比较费时费力,因些需要进行相关自动备份,并且要备份至指定的备份服务器上
具体备份内容有:
1、网站站点所有目录及相关文件
2、系统相关的配置文件
3、网站的访问日志文件
具体要求如下:(每天0点备份)
1、WEB服务器上的数据和备份服务器上的数据都存放在/backup/下(规范)
2、系统文件各类配置文件(定时任务、脚本等)
3、web服务器站点目录(/var/web/www)
4、web服务器日志目录(/app/logs)
5、web服务器数据只保留7天内的备份内容,备份服务器上保留180天内的数据
6、备份服务器上的备份要求按照服务器IP为目录保存,备份的数据需按时间做为文件名
二:需求分析
鉴于目前此种需求进行分析,可以得出操作思路为
1、备份服务器B配置rsync服务,做为服务端
2、web服务器配置为rsync客户端
3、web服务器上打包备份数据至指定的目录/backup/下
4、然后利用rsync服务推送备份数据至备份服务器上
5、以上配置完成后,可手工测试,如果测试成功,可将上述配置打包写成脚本
6、将脚本写入定时任务,周期性进行备份数据
三:项目结构图
四:备份服务器操作过程
1、服务器环境
[[email protected] ~]# cat /etc/redhat-release
CentOS release 6.5 (Final)
[[email protected] ~]# uname -r
2.6.32-431.el6.x86_64
2、配置rsync操作过程
[[email protected]Backup ~]# cat /etc/rsyncd.conf
cat: /etc/rsyncd.conf: No such file or directory
如果有此文件,配置前要进行备份,再进行相关配置
配置rsync服务
[[email protected] ~]# cat /etc/rsyncd.conf
##rsync config start
##created by root 2016-08-08 15:00
##rsync.conf config start
uid = rsync
gid = rsync
use chroot = no
max connetctions = 200
timeout = 100
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
[rsync]
path = /backup/
ignore errors
read only = false
list = false
hosts allow = 192.168.1.0/24
hosts deny = 0.0.0.0/32
auth users = rsync_backup
secrets file = /etc/rsync.password
##rsync config end
添加用户
[[email protected]Backup ~]# useradd rsync -s /sbin/nologin -M
改变目录权限
[[email protected] ~]# chown -R rsync.rsync /backup
配置密码文件
[[email protected]Backup ~]# echo "rsync_backup:rsync.conf">>/etc/rsync.password
[[email protected]Backup ~]# cat /etc/rsync.password
rsync_backup:rsync.conf
改变密码文件权限
[[email protected]Backup ~]# chmod 600 /etc/rsync.password
[[email protected]Backup ~]# ls -ld /etc/rsync.password
-rw-------. 1 root root 24 Sep 9 13:06 /etc/rsync.password
格式化文件
[[email protected]Backup ~]# dos2unix /etc/rsyncd.conf
dos2unix: converting file /etc/rsyncd.conf to UNIX format ...
开启服务后台运行
[[email protected]Backup ~]# rsync --daemon
[[email protected]Backup ~]# netstat -lntup|grep rsync
tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 2002/rsync
tcp 0 0 :::873 :::* LISTEN 2002/rsync
将服务开启命令写入开机自启当中
[[email protected] ~]# echo "##web backup by root 20160808">>/etc/rc.local;echo "/usr/bin/rsync --daemon">>/etc/rc.local
五:WEB服务器操作过程
1、服务器环境
[[email protected] ~]# cat /etc/redhat-release
CentOS release 6.5 (Final)
[[email protected] ~]# uname -r
2.6.32-431.el6.x86_64
2、配置rsyncp客户端
[[email protected]WEB ~]# echo "rsync.conf">>/etc/rsync.password
[[email protected]WEB ~]# cat /etc/rsync.password
rsync.conf
[[email protected]WEB ~]# chmod 600 /etc/rsync.password
[[email protected]WEB ~]# ls -ld /etc/rsync.password
-rw-------. 1 root root 11 Sep 8 06:25 /etc/rsync.password
3、创建相应的目录
[[email protected] /]# mkdir /backup/
[[email protected] /]# mkdir /var/web/www -p
[[email protected] /]# mkdir /app/logs -p
[[email protected] /]# tree /var/web
/var/web
+-- www
[[email protected] /]# tree /app
/app
+-- logs
4、手工测试下推送数据是否成功
[[email protected] backup]# rsync -avzP /backup/ [email protected]::rsync --password-file=/etc/rsync.password
sending incremental file list
./
a
0 100% 0.00kB/s 0:00:00 (xfer#1, to-check=3/5)
b
0 100% 0.00kB/s 0:00:00 (xfer#2, to-check=2/5)
c
0 100% 0.00kB/s 0:00:00 (xfer#3, to-check=1/5)
d
0 100% 0.00kB/s 0:00:00 (xfer#4, to-check=0/5)
sent 197 bytes received 87 bytes 568.00 bytes/sec
total size is 0 speedup is 0.00
[[email protected] ~]# cd /backup/
[[email protected] backup]# tree
+-- a
+-- b
+-- c
+-- d
表明数据能成功推送至备份服务器相应的目录下,同时说明上述配置正确
六、按要求打包需求中的各目录
打包站目录文件
[[email protected] web]# tar zcvf /backup/www_$(date +%F).tar.gz ./www
./www/
[[email protected] web]# ls /backup/
www_2016-09-09.tar.gz
打包日志文件
[[email protected] app]# tar zcvf /backup/log_$(date +%F).tar.gz ./logs
[[email protected] app]# ls /backup/
log_2016-09-09.tar.gz www_2016-09-09.tar.gz
打包系统配置文件
[[email protected] /]# tar zcvf /backup/etc_$(date +%F).tar.gz ./etc
[[email protected] /]# ls /backup/
etc_2016-09-09.tar.gz log_2016-09-09.tar.gz www_2016-09-09.tar.gz
打包定时任务文件
[[email protected] ~]# cp /var/spool/cron/root /backup/root_$(date +%F)
[[email protected] ~]# ls /backup/
etc_2016-09-09.tar.gz root_2016-09-09
log_2016-09-09.tar.gz www_2016-09-09.tar.gz
打包系统脚本文件
[[email protected] ~]# cd /server/
[[email protected] server]# tar zcvf /backup/scripts_$(date +%F).tar.gz ./scripts
./scripts/
[[email protected] server]# ls /backup/
etc_2016-09-09.tar.gz root_2016-09-09 www_2016-09-09.tar.gz
log_2016-09-09.tar.gz scripts_2016-09-09.tar.gz
手工推送测试数据备份是否成功
[[email protected] server]# rsync -avzP /backup/ [email protected]::rsync --password-file=/etc/rsync.password
sending incremental file list
./
etc_2016-09-09.tar.gz
9949927 100% 34.90MB/s 0:00:00 (xfer#1, to-check=4/6)
log_2016-09-09.tar.gz
112 100% 0.40kB/s 0:00:00 (xfer#2, to-check=3/6)
root_2016-09-09
204 100% 0.74kB/s 0:00:00 (xfer#3, to-check=2/6)
scripts_2016-09-09.tar.gz
114 100% 0.41kB/s 0:00:00 (xfer#4, to-check=1/6)
www_2016-09-09.tar.gz
109 100% 0.39kB/s 0:00:00 (xfer#5, to-check=0/6)
sent 9954208 bytes received 106 bytes 19908628.00 bytes/sec
total size is 9950466 speedup is 1.00
[[email protected] backup]# ls
etc_2016-09-09.tar.gz log_2016-09-09.tar.gz root_2016-09-09
scripts_2016-09-09.tar.gz www_2016-09-09.tar.gz
数据备份成功,同样符合当前要求
七:书写备份脚本
备份网站站点目录与日志文件
###backup web and logs to backup directory
cd /var/web/ && tar zcf /backup/www_$(date +%F).tar.gz ./www
cd /app/ && tar zcf /backup/log_$(date +%F).tar.gz ./logs
备份系统配置文件
###bakcup configrue of system to backup directory
cd / && tar zcf /backup/etc_$(date +%F).tar.gz ./etc
cd /server/ tar zcf /backup/scripts_$(date +%F).tar.gz ./scripts
cp /var/spool/cron/root /backup/root_$(date +%F)
推送数据到备份服务器
###rsync data of backup to backup servers directory
cd /backup/ && rsync -avzP /backup/ [email protected]::rsync --password-file=/etc/rsync.password >/dev/null 2>&1
删除7天前的备份数据
###delete data of 7days ago
find /backup -type f -name "*.tar.gz" -mtime +7|xrags rm -f
八:按要求重组脚本内容
1、web服务器数据只保留7天内的备份内容,备份服务器上保留180天内的数据
2、备份服务器上的备份要求按照服务器IP为目录保存,备份的数据需按时间做
为了规范特定将脚本文件放到/server/scripts/下面
cd /server/scripts/
vi backup.sh
#!/bin/bash
ip=`grep IPADD /etc/sysconfig/network-scripts/ifcfg-eth0|cut -d = -f2`
mkdir /backup/$ip -p
配置变量
##backup web and logs data to backup directory
cd /var/web/ && tar zcf /backup/$ip/www_$(date +%F).tar.gz ./www
cd /app/ && tar zcf /backup/$ip/log_$(date +%F).tar.gz ./logs
###backup configrue of system to backup directory
cd / && tar zcf /backup/$ip/etc_$(date +%F).tar.gz ./etc
cd /server/ && tar zcf /backup/$ip/scripts_$(date +%F).tar.gz ./scripts
cp /var/spool/cron/root /backup/$ip/root_$(date +%F)
##rsync data of backup to backup servers directory
cd /backup/ && rsync -avzP ./ [email protected]::rsync --password-file=/etc/rsync.password>/dev/null 2>&1
##delete data of 7 days ago
find /backup -type f -name "*.tar.gz" -mtime +7|xargs rm -f
[[email protected] scripts]# cd /backup/
[[email protected] backup]# ls
[[email protected] backup]# /bin/sh /server/scripts/backup.sh
[[email protected] backup]# ls -ll
total 4
drwxr-xr-x. 2 root root 4096 Sep 9 23:02 192.168.1.2
[[email protected] backup]# cd 192.168.1.2/
[[email protected] 192.168.1.2]# ls
etc_2016-09-09.tar.gz log_2016-09-09.tar.gz root_2016-09-09
scripts_2016-09-09.tar.gz www_2016-09-09.tar.gz
脚本执行成功,表明脚本配置正确
九:配置定时任务
1、WEB服务器上配置定时任务
[[email protected] ~]# crontab -e
##backup web server data to backup-server by 20160808
00 00 * * * /bin/sh /server/scripts/backup.sh>/dev/null 2>&1
[[email protected] ~]# crontab -l
##backup web server data to backup-server by 20160808
00 00 * * * /bin/sh /server/scripts/backup.sh>/dev/null 2>&1
2、备份服务器上配置定时任务
[[email protected] ~]# mkdir /server/scripts/ -p
[[email protected] ~]# cd /server/scripts/
[[email protected] scripts]# vi delete.sh
###delete data of 180 days ago
find /backup -type f -name "*.tar.gz" -mtime +180|xargs rm -f
[[email protected] ~]# crontab -e
##delete data by 20160808
00 00 * * 6 /bin/sh /server/scripts/delete.sh>dev/null 2>&1
[[email protected] ~]# crontab -l
##delete data by 20160808
00 00 * * 6 /bin/sh /server/scripts/delete.sh>dev/null 2>&1
十:测试数据备份结果
手工测试
[[email protected] backup]# rm -rf *
[[email protected] backup]# /bin/sh /server/scripts/backup.sh
[[email protected] backup]# ls -ll
total 4
drwxr-xr-x. 2 root root 4096 Sep 9 23:22 192.168.1.2
[[email protected] backup]# ls 192.168.1.2/
etc_2016-09-09.tar.gz root_2016-09-09 www_2016-09-09.tar.gz
log_2016-09-09.tar.gz scripts_2016-09-09.tar.gz
[[email protected] backup]# rm -rf *
[[email protected] backup]# ls
192.168.1.2
[[email protected] backup]# ls 192.168.1.2/
etc_2016-09-09.tar.gz root_2016-09-09 www_2016-09-09.tar.gz
log_2016-09-09.tar.gz scripts_2016-09-09.tar.gz
手工测试正常,数据备份、推送成功
测试定时任务
[[email protected] backup]# rm -rf * 删除之前手工测试数据
[[email protected] backup]# date
Fri Sep 9 23:26:08 CST 2016
[[email protected] backup]# date 090923592016.55 修改当前系统时间
Fri Sep 9 23:59:55 CST 2016
[[email protected] backup]# date 查看当前系统时间
Sat Sep 10 00:00:36 CST 2016
[[email protected] backup]# ls
192.168.1.2
[[email protected] backup]# ls 192.168.1.2/ 查看定时任务执行情况
etc_2016-09-10.tar.gz root_2016-09-10 www_2016-09-10.tar.gz
log_2016-09-10.tar.gz scripts_2016-09-10.tar.gz
[[email protected] backup]# rm -rf * 删除之前手工测试数据
[[email protected] backup]# ls
[[email protected] backup]# ls -ll
total 4
drwxr-xr-x. 2 rsync rsync 4096 Sep 9 19:00 192.168.1.2
[[email protected] backup]# ls 192.168.1.2/ 查看数据备份情况
etc_2016-09-10.tar.gz root_2016-09-10 www_2016-09-10.tar.gz
log_2016-09-10.tar.gz scripts_2016-09-10.tar.gz
经过测试数据备份成功,并且正常推送到备份服务器指定目录下,表明定时任务执行成功
十一:下载服务器的数据至本地
为了方便日后维护与管理,可以将服务器的数据下载到本地,后续有故障时可直接上传到指定服即可恢复数据
[[email protected] backup]# tar zcvf 192.168.1.2_data.tar.gz ./192.168.1.2/
[[email protected] backup]# sz -y 192.168.1.2_data.tar.gz
[[email protected] backup]# sz -y /etc/rsyncd.conf
如果需要定期执行此种操作,可以将上述配置写成脚本 ,然后配置定时任务
长按二维码关注微信公众号(友侃有笑)
本文出自 “民工哥博客” 博客,请务必保留此出处http://mingongge.blog.51cto.com/2429897/1880465
以上是关于Linux系统crondrsync打包备份数据操作实战的主要内容,如果未能解决你的问题,请参考以下文章
如何将Linux系统的多个目录及文件备份并压缩到一个文件,以方面保持和传递?
linux中打包备份脚本,手动执行可以正常打包,使用crontab -e 添加到系统定时任务中执行就无法打包