Rsync
企业案例:rsync上机实战考试题: 某公司里有一台Web服务器,里面的数据很重要,但是如果硬盘坏了,数据就会丢失,现在领导要求你把数据在其他机器上做一个周期性定时备份。要求如下: 每天晚上00点整在Web服务器A上打包备份网站程序目录并通过rsync命令推送到服务器B上备份保留(备份思路可以是先在本地按日期打包,然后再推到备份服务器上)。 具体要求如下: 1)Web服务器A和备份服务器B的备份目录必须都为/backup。 2)Web服务器站点目录假定为(var/www/HTml)。 3)Web服务器本地仅保留7天内的备份。 4)备份服务器上检查备份结果是否正常,并将每天的备份结果发给管理员信箱(选做)。 5)备份服务器上每周六的数据都保留,其他备份仅保留180天备份(选做)。 |
Rsync-backup :rsync服务端
[[email protected] ~]# rpm -qa rsync 查看是否有rsync包 rsync-3.0.6-12.el6.x86_64 [[email protected] ~]# touch /etc/rsyncd.conf 创建配置文件 [[email protected] ~]# vim /etc/rsyncd.conf 编辑配置文件
#created by yvonne 16:30 2015-6-24 ##rsyncd.conf start## uid = rsync 指定用户uid gid = rsync 指定用户组id use chroot = no 用户不能切入 max connections = 2000 指定最大连接时2000 timeout = 600 指定超时时间 pid file = /var/run/rsyncd.pid 指定pid文件路径 lock file = /var/run/rsync.lock 指定lock文件路径 log file = /var/log/rsyncd.log 指定日志文件路径 ignore errors 指定报错提示: read only = false 不允许只读 list = false 不允许列表查看 hosts allow = 192.168.10.0/24 允许的网段 hosts deny = 0.0.0.0/32 拒绝的网段/ip(此处不拒绝) auth users = rsync_backup 指定验证用户 secrets file = /etc/rsync.password 指定密码文件名 ##################################### [backup] comment = backup server by yvonne 16:30 2015-6-24 path = /backup 指定备份路径 "/etc/rsyncd.conf" 23L, 516C written [[email protected] ~]# mkdir /backup 创建备份目录/backup [[email protected] ~]# useradd rsync 创建验证用户 [[email protected] ~]# chown rsync /backup/ 更改备份目录/backup的属主为验证用户rsync [[email protected] ~]# ll /backup/ total 0 [[email protected] ~]# ls -ld /backup/ drwxr-xr-x. 2 rsync root 4096 Jun 22 19:20 /backup/ [[email protected] ~]# echo "rsync_backup:oldboy">/etc/rsync.password 创建密码文件 [[email protected] ~]# cat /etc/rsync.password rsync_backup:oldboy [[email protected] ~]# chmod 600 /etc/rsync.password 将密码文件权限改为600 [[email protected] ~]# ls -ld /etc/rsync.password -rw-------. 1 root root 20 Jun 22 19:23 /etc/rsync.password [[email protected] ~]# rsync –daemon 启动rsync服务 [[email protected] ~]# vim /etc/rc.local #!/bin/sh # # This script will be executed *after* all the other init scripts. # You can put your own initialization stuff in here if you don‘t # want to do the full Sys V style init stuff. touch /var/lock/subsys/local /etc/init.d/iptables stop rsync –daemon 加入开机自启动 ~ "/etc/rc.local" 9L, 261C written [[email protected] ~]# |
Rsync-client :rsync客户端
[[email protected] ~]# rpm -qa rsync 查看是否有rsync包 rsync-3.0.6-12.el6.x86_64 [[email protected] ~]# mkdir /backup 创建备份目录/backup [[email protected] ~]# cd /backup/ [[email protected] backup]# touch test 在/backup下创建文件用户测试 [[email protected] backup]# cd [[email protected] ~]# echo "oldboy">/etc/rsync.password 创建密码文件 [[email protected] ~]# chmod 600 /etc/rsync.password 将密码文件的权限改为600 [[email protected] ~]# ll /etc/rsync.password -rw-------. 1 root root 7 Jun 22 19:07 /etc/rsync.password [[email protected] ~]# rsync /backup/* [email protected]::backup --password-file=/etc/rsync.password [[email protected] ~]# rsync /backup/* [email protected]::backup --password-file=/etc/rsync.password 推送备份文件 [[email protected] ~]# mkdir -p /var/www/HTml 创建站点目录 [[email protected] ~]# cd /var/www/ [[email protected] www]# ll total 4 drwxr-xr-x. 2 root root 4096 Jun 22 19:13 HTml [[email protected] www]# tar -zcvf /backup/`/sbin/ifconfig eth4|awk -F ‘[: ]+‘ ‘NR==2 {print $4 }‘`_var_www_HTml_`date +%F`.tar.gz HTml/ 打包( ip_name_date) HTml/ [[email protected] www]# ls /backup/ 查看打包成功 192.168.10.104_var_www_HTml_2015-06-22.tar.gz test [[email protected] www]# mkdir -p /server/scripts [[email protected] www]# vim /server/scripts/tar.sh 编写打包脚本 #print for tar /var/www/HTml backup by yvonne 16:46 2015-6-24. cd /var/www/ &&\ tar -zcf /backup/`/sbin/ifconfig eth4|awk -F ‘[: ]+‘ ‘NR==2 {print $4 }‘`_var_www_HTml_`date +%F`.tar.gz HTml/ ~ ~ "/server/scripts/tar.sh" [New] 3L, 192C written [[email protected] www]# /bin/sh /server/scripts/tar.sh 全路径执行 HTml/ [[email protected] www]# ls /backup/ 192.168.10.104_var_www_HTml_2015-06-22.tar.gz test [[email protected] www]# rsync /backup/* [email protected]::backup --password-file=/etc/rsync.password 推送备份文件 [[email protected] www]# vim /server/scripts/backup.sh 写入脚本 #print for push backup files to rsync-server by yvonne 16:50 2015-6-24. /bin/sh /server/scripts/tar.sh &&\ 先执行tar脚本打包 rsync /backup/* [email protected]::backup --password-file=/etc/rsync.password ~ 再执行备份脚本 ~ "/server/scripts/backup.sh" [New] 3L, 196C written [[email protected] www]# /bin/sh /server/scripts/backup.sh 执行脚本 HTml/ [[email protected] www]# crontab –e 写入定时任务 no crontab for root - using an empty one #print for backup HTml files to backup-server everyday 00:00 by yvonne 16:55 2015-6_24. 00 00 * * * /bin/sh /server/scripts/backup.sh >/dev/null 2>&1 ~ "/tmp/crontab.4JQ20E" 3L, 153C written crontab: installing new crontab [[email protected] www]# [[email protected] www]# [[email protected] www]# crontab –l 查看定时任务 #print for backup HTml files to backup-server everyday 00:00 by yvonne 16:55 2015-6_24. 00 00 * * * /bin/sh /server/scripts/backup.sh >/dev/null 2>&1
[[email protected] www]# find /backup/ -type f -mtime +7 -name "*.tar.gz" 保留7天内的 [[email protected] www]# find /backup/ -type f -mtime -7 -name "*.tar.gz" 删除7天内的ok /backup/192.168.10.104_var_www_HTml_2015-06-22.tar.gz [[email protected] www]# find /backup/ -type f -mtime +7 -name "*.tar.gz" |xargs rm -rf [[email protected] www]# vim /server/scripts/reserve.sh 写入脚本 #print for reserve backup files in 7 days by yvonne 16:55 2015-6-24. find /backup/ -type f -mtime +7 -name "*.tar.gz" |xargs rm -rf ~ "/server/scripts/reserve.sh" [New] 2L, 133C written [[email protected] www]# /bin/sh /server/scripts/reserve.sh 执行脚本 [[email protected] www]# crontab –e 写入定时任务 #print for backup HTml files to backup-server everyday 00:00 by yvonne 16:55 2015-6_24. 00 00 * * * /bin/sh /server/scripts/backup.sh >/dev/null 2>&1 #print for reserve backup files in 7 days =it‘s monday to do it by yvonne 17:00 2015-6-24. 00 01 * * 1 /bin/sh /server/scripts/reserve.sh >/dev/null 2>&1 ~ "/tmp/crontab.rK7RPc" 6L, 308C written crontab: installing new crontab [[email protected] www]# crontab –l 查看定时任务 #print for backup HTml files to backup-server everyday 00:00 by yvonne 16:55 2015-6_24. 00 00 * * * /bin/sh /server/scripts/backup.sh >/dev/null 2>&1 #print for reserve backup files in 7 days =it‘s monday to do it by yvonne 17:00 2015-6-24. 00 01 * * 1 /bin/sh /server/scripts/reserve.sh >/dev/null 2>&1 [[email protected] www]# cd [[email protected] ~]# vim /etc/mail.rc 将备份信息发送给管理员邮箱 # This is the configuration file for Heirloom mailx (formerly # known under the name "nail". +++原来的配置内容省略++++ 在后面追加一下内容 ############################################################### set [email protected] smtp=smtp.163.com set [email protected] smtp-auth-password=huang123 smtp-auth=nologin "/etc/mail.rc" 73L, 2106C written [[email protected] ~]# [[email protected] ~]# mail -s "test the smtp mail" [email protected] 备注:关于发送邮件详细信息请参考: http://www.4wei.cn/archives/1001468 Linux mail/mailx命令使用外部smtp(网易163的SMTP)来发送邮件 http://blog.csdn.net/gdpencil/article/details/6048628 linux下实现每天自动发送服务器日志到邮箱(使用sendmail) |
关于“5)备份服务器上每周六的数据都保留,其他备份仅保留180天备份(选做)。”欢迎大家给出可行方案。