检查系统是否安装rsync
客户端NFS:
[root@nfs ~]# rpm -qa rsync
rsync-3.0.6-12.el6.x86_64
服务端rsync:
[root@rsync ~]# rpm -qa rsync
rsync-3.0.6-12.el6.x86_64
如果没有安装rsync服务
[root@rsync ~]# yum install rsync -y
[root@rsync ~]# vi /etc/rsyncd.conf
rsync server
#created by apu
#rsyncd.conf start#
uid = rsync//用户
gid = rsync
use chroot = no
max connections = 2000
timeout = 600
pid file = /var/run/rstbcd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
ignore errors
read only = false
list = false
hosts allow = 192.168.74.0/24
hosts deny = 0.0.0.0/32
auth users = rsync_backup
secrets file = /etc/rsync.password
###################################
[backup]
comment=rsync+inotify:rsync_backup@192.168.74.135::backup/ by apu
path = /backup/
注意:rsyncd.conf配置文件可以用man查看具体设置
[root@rsync /]# useradd rsync -M -s /sbin/nologin
[root@rsync /]# tail -1 /etc/passwd
rsync:x:504:504::/home/rsync:/sbin/nologin
[root@rsync ~]# cat /etc/rsync.password
rsync_backup:oldboy//用户:密码
[root@rsync ~]# chmod 600 /etc/rsync.password
[root@rsync ~]# ls -l /etc/rsync.password
-rw------- 1 root root 20 Sep 14 00:05 /etc/rsync.password
注意:修改600是为了让服务调用,另外一点密码文件最好设置权限600。
[root@nfs ~]# cat /etc/rsync.password
oldboy
注意:由于命令中带有用户名,所以客户端密码文件只需要密码。
[root@rsync ~]# chmod 600 /etc/rsync.password
提示:以下的NFS都是rsync的客户端,rsync为服务端。
2.1、问题一、实现把Web服务器A数据备份到服务器B。
具体要求如下:
nfs:
[root@nfs ~]# mkdir /backup
rsync:
[root@rsync /]# mkdir /backup
[root@rsync /]# chown -R rsync /bakcup
[root@rsync /]# ls -ld /backup/
drwxr-xr-x 2 rsync root 4096 Sep 14 00:13 /backup/
nfs:
[root@nfs ~]# mkdir -p /app/logs
[root@nfs ~]# mkdir -p /etc/scripts
假设备份内容:
a.定时任务服务的配置文件(/var/spool/cron/root)
b.开机自启动的配置文件(/etc/rc.local)
c.日常脚本的目录(/server/scripts)
d.防火墙iptables的配置文件(/etc/sysconfig/iptables)。
[root@nfs ~]# cp /var/spool/cron/root /etc/rc.local /server/scripts/* /etc/sysconfig/iptables /app/logs
[root@nfs app]# cd /app
[root@nfs app]# tar -zcvf /app/logs/setup_$(date +%F).tar.gz ./logs
./logs/
./logs/test.sh
./logs/iptables
./logs/root
./logs/rc.local
[root@nfs app]# tar tf /app/logs/setup_2015XXX/tar.gz
./logs/
./logs/test.sh
./logs/iptables
./logs/root
./logs/rc.local
[root@nfs /]# mkdir -p /var/html/www
[root@nfs /]# cd /var/html/www/
[root@nfs www]# touch {a..z}.html
[root@nfs www]# touch readme.txt
[root@nfs /]# cd /var/html
[root@nfs html]# tar -zcvf /app/logs/www_$(date +%F).tar.gz ./www
nfs:
rsync -avz /app/logs/ rsync_backup@server_ip::backup/ --password-file=/etc/rsync.password
5)Web服务器保留打包后的7天的备份数据即可(本地留存不能多于7天,因为太多硬盘会满),备份服务器B上要保留6个月的数据副本。
nfs:
[root@nfs /]# find /app/logs -type f -name "www*.tar.gz" -mtime +7|xargs rm -f
[root@nfs /]# find /app/logs -type f -name "setup*.tar.gz" -mtime +7|xargs rm -f
rsync:
[root@nfs /]# find /backup -type f -name "setup*.tar.gz" -mtime +180|xargs rm -f
6)备份服务器B上要按照备份数据服务器的IP为目录保存,打包的文件按照时间名字保存。特别提示:本题在工作中是网站生产环境全网备份项目方案的一个小型模拟,很有意义。
[root@nfs scripts]# cat rsync_backup.sh
#!/bin/bash
#声明变量#
ip=`/sbin/ifconfig eth0|/bin/awk -F ‘[: ]+‘ ‘NR==2 {print $4}‘`
date=`/bin/date +%F`
path=/backup/$ip/$date
server_ip=192.168.74.135
#创建打包目录#
/bin/mkdir $path -p && \
#打包/var/html/www的文件#
cd /var/html && \
/bin/tar -zcf $path/html_$date.tar.gz ./www && \
#把需要备份的配置文件拷贝到统一目录/app/logs中#
/bin/cp /var/spool/cron/root /etc/rc.local /server/scripts/* /etc/sysconfig/iptables /app/logs && \
cd /app && \
/bin/tar -zcf $path/setup_$date.tar.gz ./logs
/usr/bin/rsync -avz /backup/ rsync_backup@$server_ip::backup/ --password-file=/etc/rsync.password
[root@nfs /]# crontab -e
##backup setup files,/var/www/html by apu at 2015-09-15##
55 23 * * * /bin/sh /server/scripts/rsync_backup.sh>/dev/null 2>&1
[root@nfs /]# tail -f /var/log/cron
Sep 15 13:39:36 nfs crontab[1673]: (root) REPLACE (root)
Sep 15 13:39:36 nfs crontab[1673]: (root) END EDIT (root)
Sep 15 13:40:01 nfs crond[961]: (root) RELOAD (/var/spool/cron/root)
Sep 15 13:40:01 nfs CROND[1679]: (root) CMD (/usr/sbin/ntpdate time.nist.gov>/dev/null 2>&1)
Sep 15 13:40:01 nfs CROND[1680]: (root) CMD (/usr/lib64/sa/sa1 1 1)
Sep 15 13:40:15 nfs crontab[1684]: (root) BEGIN EDIT (root)
Sep 15 13:40:19 nfs crontab[1684]: (root) REPLACE (root)
Sep 15 13:40:19 nfs crontab[1684]: (root) END EDIT (root)
Sep 15 13:41:01 nfs crond[961]: (root) RELOAD (/var/spool/cron/root)
Sep 15 13:41:01 nfs CROND[1689]: (root) CMD (/bin/sh /server/scripts/rsync_backup.sh>/dev/null 2>&1)
Sep 15 13:42:01 nfs CROND[1704]: (root) CMD (/bin/sh /server/scripts/rsync_backup.sh>/dev/null 2>&1)
Sep 15 13:43:01 nfs CROND[1718]: (root) CMD (/bin/sh /server/scripts/rsync_backup.sh>/dev/null 2>&1)
[root@rsync backup]# watch -n 1 -d ‘ls -l `pwd`‘
[root@nfs /]# cat /server/scripts/rsync_backup.sh
#!/bin/bash
#声明变量#
ip=`/sbin/ifconfig eth0|/bin/awk -F ‘[: ]+‘ ‘NR==2 {print $4}‘`
date=`/bin/date +%F`
path=/backup/$ip/$date
server_ip=192.168.74.135
#创建打包目录#
/bin/mkdir $path -p && \
#打包/var/html/www的文件#
cd /var/html && \
/bin/tar -zcf $path/html_$date.tar.gz ./www && \
#把需要备份的配置文件拷贝到统一目录/app/logs中#
/bin/cp /var/spool/cron/root /etc/rc.local /server/scripts/* /etc/sysconfig/iptables /app/logs && \
cd /app && \
/bin/tar -zcf $path/setup_$date.tar.gz ./logs
/usr/bin/rsync -avz /backup/ rsync_backup@$server_ip::backup/ --password-file=/etc/rsync.password
本文出自 “运维那些事儿” 博客,请务必保留此出处http://apu1986.blog.51cto.com/10581571/1695057
原文地址:http://apu1986.blog.51cto.com/10581571/1695057