标签(空格分隔): Linux 服务搭建 - 陈思齐
--- 本教学笔记是本人学习和工作生涯中的摘记整理而成,此为初稿(尚有诸多不完善之处),为原创作品,允许转载,转载时请务必以超链接形式标明文章原始出处,作者信息和本声明。否则将追究法律责任。
[TOC]
某公司里有一台 NFS 服务器,里面的数据很重要,但是如果硬盘坏了,数据就会丢失,现在领导要求你把数据在其他 机器上做一个周期性定时备份。要求如下:
具体要求如下:
操作系统
- [root@backup ~]# cat /etc/redhat-release
- CentOS release 6.8 (Final)
内核版本
- [root@backup ~]# uname -r
- 2.6.32-642.el6.x86_64
主机网络参数设置:
主机名 | 网卡 eth0 | 网卡 eth1 | 用途 |
---|---|---|---|
backup | 10.0.0.41 | 172.16.1.41 | rsync 服务端 |
nfs01 | 10.0.0.31 | 172.16.1.31 | NFS 存储服务器客户端 |
web01 | 10.0.0.8/24 | 172.16.1.8/24 | nginx web 服务器 |
1)确认软件是否安装:
- [root@backup ~]# rpm -qa rsync
- rsync-3.0.6-12.el6.x86_64
2)配置 rsyncd.conf 配置文件
- #rsync_config____start
- #created by chensiqi 22:00 2017-3-6
- #blog:http://www.cnblogs.com/chensiqiqi/
- ##rsyncd.conf start##
- # 用户
- uid = rsync
- # 组
- gid = rsync
- # 程序安全设置
- use chroot = no
- # 客户端连接数
- max connections = 200
- # 超时时间
- timeout = 300
- # 进程号文件位置
- pid file = /var/run/rsyncd.pid
- # 进程锁
- lock file = /var/run/rsync.lock
- # 日志文件位置
- log file = /var/log/rsyncd.log
- ###################################
- [backup]
- # 使用目录
- path = /backup/
- # 有错误时忽略
- ignore errors
- # 可读可写(true或false)
- read only = false
- # 阻止远程列表(不让通过远程方式看服务端有啥)
- list = false
- # 允许IP
- hosts allow = 172.16.1.0/24
- # 禁止IP
- hosts deny = 0.0.0.0/32
- # 虚拟用户
- auth users = rsync_backup
- # 存放用户和密码的文件
- secrets file = /etc/rsync.password
- ##rsync_config____end##
3) 创建 rsync 账户及共享目录并修改目录属主为 rsync
- [root@backup ~]# useradd -M -s /sbin/nologin rsync
- [root@backup ~]# mkdir /backup
- [root@backup ~]# chown -R rsync /backup
4) 启动服务:rsync --daemon
- [root@backup ~]# rsync --daemon
- [root@backup ~]# ss -antup | grep rsync
- tcp LISTEN 0 5 :::873 :::* users:(("rsync",1851,5))
- tcp LISTEN 0 5 *:873 *:* users:(("rsync",1851,4))
5) 制作服务启动脚本 / etc/init.d/rsyncd
- #!/bin/bash
- #author:Mr.chen
- # chkconfig:35 13 91
- # description:This is Rsync service management shell script
- # Source function library
- . /etc/rc.d/init.d/functions
- start(){
- rsync --daemon
- if [ $? -eq 0 -a `ps -ef|grep -v grep|grep rsync|wc -l` -gt 0 ];then
- action "Starting Rsync:" /bin/true
- sleep 1
- else
- action "Starting Rsync:" /bin/false
- sleep 1
- fi
- }
- stop(){
- pkill rsync;sleep 1;pkill rsync
- if [ `ps -ef|grep -v grep|grep "rsync --daemon"|wc -l` -lt 1 ];then
- action "Stopping Rsync: " /bin/true
- sleep 1
- else
- action "Stopping Rsync:" /bin/true
- sleep 1
- fi
- }
- case "$1" in
- start)
- start;
- ;;
- stop)
- stop;
- ;;
- restart|reload)
- stop;
- start;
- ;;
- *)
- echo $"Usage: $0 {start|stop|restart|reload}"
- ;;
- esac
6) 启动脚本放到 / etc/init.d 目录下,增加 x 执行权限并添加开机启动
- [root@backup ~]# chmod +x /etc/init.d/rsyncd
- [root@backup ~]# /etc/init.d/rsyncd stop
- 已终止
- [root@backup ~]# /etc/init.d/rsyncd start
- Starting Rsync: [确定]
- [root@backup ~]# chkconfig rsyncd on
- [root@backup ~]# chkconfig --list | grep rsync
- rsyncd 0:关闭 1:关闭 2:启用 3:启用 4:启用 5:启用 6:关闭
7) 创建 rsync 虚拟账户名和密码, 并赋予密码文件 600 权限
- [root@backup ~]# echo "rsync_backup:123456" > /etc/rsync.password
- [root@backup ~]# cat /etc/rsync.password
- rsync_backup:123456
- [root@backup ~]# chmod 600 /etc/rsync.password
- [root@backup ~]# ll /etc/rsync.password
- -rw-------. 1 root root 20 3月 7 20:54 /etc/rsync.password
8) 配置 Rsync 服务器端检查脚本
- #!/bin/bash
- # 全网服务器备份解决方案_rsync服务器端检查脚本
- # author:Mr.chen
- # 2017-3-8
- ./etc/init.d/functions
- /etc/init.d/postfix status &>/dev/null || /etc/init.d/postfix start
- Path=/backup
- fileName="md5sum.txt"
- /usr/bin/md5sum -c $Path/$fileName
- if [ $? -eq 0 ];then
- action "备份正常!" /bin/true
- rm -rf $Path/$fileName
- else
- action "备份异常!" /bin/false
- echo "Rsync服务器备份失败"|mail -s "$(date +%F)服务器告警" xxxxxxxx@qq.com
- fi
- # 找出超过6个月的不是周1的备份文件并删除(周1的数据其实是在周2打包)
- find $Path/ ! -name "*_2.tar.gz" -mtime +180 -type f | xargs rm -rf
9) 将脚本挂定时任务
- 00 6 * * * /bin/sh /server/scripts/rsync_Server.sh >/dev/null 2>&1
backup 服务器 Rsync 服务端至此配置完毕
1)只需要创建密码文件(只包含密码即可),并赋予密码文件 600 权限
- [root@nfs01 ~]# echo "123456" > /etc/rsync.password
- [root@nfs01 ~]# chmod 600 /etc/rsync.password
2) 创建共享目录 backup
- [root@nfs01 ~]# mkdir /backup
3) 在客户端进行推送测试
- [root@nfs01 ~]# cd /backup
- [root@nfs01 backup]# touch {1..5}
- [root@nfs01 backup]# ls
- 1 2 3 4 5
- [root@nfs01 backup]# rsync -avzP /backup/ rsync_backup@192.168.197.132::backup --password-file=/etc/rsync.password
- sending incremental file list
- ./
- 1
- 0 100% 0.00kB/s 0:00:00 (xfer#1, to-check=4/6)
- 2
- 0 100% 0.00kB/s 0:00:00 (xfer#2, to-check=3/6)
- 3
- 0 100% 0.00kB/s 0:00:00 (xfer#3, to-check=2/6)
- 4
- 0 100% 0.00kB/s 0:00:00 (xfer#4, to-check=1/6)
- 5
- 0 100% 0.00kB/s 0:00:00 (xfer#5, to-check=0/6)
- sent 239 bytes received 106 bytes 230.00 bytes/sec
- total size is 0 speedup is 0.00
4)nfs01 服务器打包脚本实现
- #!/bin/bash
- # 全网服务器备份解决方案_rsync客户端打包脚本
- # author:Mr.chen
- # 2017-3-7
- Path=/backup
- backup_Server=172.16.1.41
- local_IP=`/sbin/ifconfig eth1|awk -F"[ :]+" 'NR==2{print $4}'`
- Dir=${local_IP}_$(date +%F_%w)
- mkdir -p $Path/$Dir
- [ -f /var/spool/cron/root ] && cp -rp /var/spool/cron/root $Path/$Dir/
- [ -f /etc/rc.local ] && cp -rp /etc/rc.d/rc.local $Path/$Dir/
- [ -d /server/scripts ] && cp -rp /server/scripts $Path/$Dir/
- [ -d /var/html/www ] && cp -rp /var/html/www $Path/$Dir/
- [ -d /app/logs ] && cp -rp /app/logs $Path/$Dir/
- [ -f /etc/sysconfig/iptables ] && cp -rp /etc/sysconfig/iptables $Path/$Dir/
- cd $Path
- tar -zcf $Path/${Dir}.tar.gz $Dir
- rm -rf $Path/$Dir
- # 创建md5sum验证信息
- /usr/bin/md5sum $Path/${Dir}.tar.gz > $Path/md5sum.txt
- # 推送打包的文件到备份服务器
- rsync -az $Path/ rsync_backup@${backupServer}::backup --password-file=/etc/rsync.password
- # 找出超过7天的备份并删除
- find $Path/ -name "${local_IP}*" -type f -mtime +7 | xargs rm -rf
5) 将脚本挂定时任务
- 00 0 * * * /bin/sh /server/scripts/backup.sh >/dev/null 2>&1
NFS 服务器 Rsync 客户端至此配置完毕
来源: http://www.cnblogs.com/chensiqiqi/p/6531003.html