hq8501 发表于 2018-8-25 06:05:01

测试NFS+rsync+sersync+SMTP+shell脚本+cron

考核1:
  实现拓扑图:

  实验需要的机器:
  名称
  ip地址
  主机名
  web服务器
  192.168.1.10
  web01
  rsync备份服务器
  192.168.1.20
  rsync
  nfs存储服务器
  192.168.1.30
  nfs
  1,nfs创建一个目录名为/data/web的目录作为web服务器的后端存储,然后共享使用虚拟用户webuser,因为公司访问量不是很大,所以可以直接用sync来同步写入到磁盘
  在NFS服务器上:
  yum -y install nfs-utils.x86_64 rpcbind.x86_64
  /etc/init.d/rpcbind start && /etc/init.d/nfs start
  mkdir -p /data/web
  # cat /etc/exports#修改nfs配置文件
  /data/web 192.168.1.0/24(rw,sync,root_squash,all_squash,anonuid=666,anongid=666)
  useradd -u 666 webuser -s /sbin/nologin –M
  chown -R webuser:webuser /data/web/
  # exportfs -arv
  exporting 192.168.1.0/24:/data/web
  #先进行本地挂载测试,如果成功在到远端挂载
  # showmount -e 192.168.1.30
  Export list for 192.168.1.30:
  /data/web 192.168.1.0/24
  # mount -t nfs 192.168.1.30:/data/web /mnt/
  # df -Th

  Filesystem         Type>  /dev/mapper/vg_moban-lv_root
  ext4    18G 1000M   16G   6% /
  tmpfs                tmpfs490M   0490M   0% /dev/shm
  /dev/sda1            ext4   477M   52M400M12% /boot
  192.168.1.30:/data/web   nfs   18G 1000M   16G   6% /mnt
  # umount /mnt
  在web服务器挂载nfs用作网站的后端存储:
  # mkdir /webdata
  # mount -t nfs 192.168.1.30:/data/web /webdata/
  # df -Th

  Filesystem         Type>  /dev/mapper/vg_moban-lv_root
  ext4    18G998M   16G   6% /
  tmpfs                tmpfs490M   0490M   0% /dev/shm
  /dev/sda1            ext4   477M   52M400M12% /boot
  192.168.1.30:/data/webnfs   18G 1000M   16G   6% /webdata
  # echo "mount -t nfs 192.168.1.30:/data/web /webdata/" >>/etc/rc.local#设置成开机自动挂载
  NFS共享挂载已经完成,然后下一步实施搭建rsync服务器
  yum -y install rsync.x86_64
  # vim /etc/rsyncd.conf
  uid = rsync
  gid = rsync
  use chroot = no
  max connections = 20
  list = false
  timeout = 300
  read only = false
  pid file = /var/run/rsyncd.pid
  lock file= /var/run/rsyncd.lock
  log file = /var/log/rsyncd.log
  ignore errors
  host allow = 192.168.1.0/24
  
  comment = This is an nfs real-time backup module
  path = /backup/nfs
  auth users = nfs_rsync
  secrets file = /etc/rsync.password
  # mkdir -p /backup/nfs
  # useradd rsync -s /sbin/nologin -M
  # chown rsync:rsync /backup/nfs/
  # echo "nfs_rsync:nfspassword" >> /etc/rsync.password
  # chmod 600 /etc/rsync.password
  # /usr/bin/rsync --daemon
  # echo "/usr/bin/rsync --daemon" >>/etc/rc.local
  NFS充当客户端:
  # echo "nfspassword" >> /etc/rsync.password #生成密码文件
  # chmod 600 /etc/rsync.password
  # yum -y install rsync.x86_64
  # rsync -avz /data/web/ nfs_rsync@192.168.1.20::nfsbackup/ --password-file=/etc/rsync.password
  测试推送成功,
  然后在NFS上的数据要实时的备份到rsync服务器上(sersync)
  # mkdir /applocal/
  # mv sersync2.5.4_64bit_binary_stable_final.tar.gz/applocal/
  # cd /applocal/
  # tar -xvzf sersync2.5.4_64bit_binary_stable_final.tar.gz
  sersync /
  sersync /bin/sersync2
  sersync/conf/confxml.xml
  # tree /applocal/sersync
  /applocal/sersync
  ├── bin
  │   └── sersync2
  └── conf
  └── confxml.xml
  2 directories, 2 files
  # cp confxml.xml{,.bak}
  # vim confxml.xml
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  [@nfs /]# /applocal/sersync/bin/sersync2 -dro /applocal/sersync/conf/confxml.xml
  # cd /data/web/
  # touch 1
  # echo " /applocal/sersync/bin/sersync2 -dro /applocal/sersync/conf/confxml.xml" >>/etc/rc.local
  #然后实行全网备份脚本:
  备份每台服务器里面的:/etc/rc.local    /etc/hosts    /scripts
  备份数据在本地保存7天,在rsync上保存180天,星期六的数据不删除
  如果备份成功发送邮件到18163201@qq.com
  1)先搭建邮件服务,用SMTP
  # yum -y install mailx.x86_64
  # vim /etc/mail.rc
  ##邮件服务,利用qq.com来发送邮件
  set from=18163201@qq.com
  set smtp=smtps://smtp.qq.com:465
  set smtp-auth-user=18163201@qq.com
  set smtp-auth-password=dcnhslrkexdbbgbh
  set smtp-auth=login
  set ssl-verify=ignore
  set nss-config-dir=/etc/pki/nssdb
  # echo 123 | mail -v -s "test Email" 18163201@qq.com
  然后开始写备份脚本:
  在rsync配置文件里面增加:
  
  comment = This is a full network backup module
  path = /backup
  auth users = rsync_backup
  secrets file = /etc/rsyncn.password
  # chown rsync:rsync /backup/
  # echo "rsync_backup:rsync_backup" >>/etc/rsyncn.password
  # chmod 600 /etc/rsyncn.password
  # vim /scripts/nfs_backup.sh
  #!/bin/bash
  #创建一个备份使用的脚本,要求如下:
  #1,使用日期作为文件名的一部分
  #2,打包备份/etc/rc.local and /etc/exports and /scripts/ and /etc/crontab
  #3,备份数据在本地只保留7天
  #4,备份数据都放在/backup/目录下
  #5,推送到备份服务器上要是用自己的ip地址作为目录
  #每天00点打包文件,然后推送,这时候文件名日期应该是前一天的日期,截取前一天的日期
  Time=$(/bin/date +"%Y-%m-%d" -d "-1 days")
  Ip=$(/sbin/ifconfig eth3|awk -F "[ :]+" 'NR==2 {print $4}' )
  [ ! -d /backup/$Ip ] && mkdir -p /backup/$Ip
  Path=/backup/$Ip
  tar -czf $Path/$Time\.tar.gz /etc/rc.local /etc/exports /scripts/ /etc/crontab \
  && md5sum $Path/$Time\.tar.gz >$Path/file.md5
  rsync -az /backup/ rsync_backup@192.168.1.20::networkbackup --password-file=/etc/rsyncn.password
  #然后删除七天以前的文件
  find $Path -mtime +7 -type f |xargs -i rm -rf {}
  # crontab -l -u root
  00 * * * * /bin/sh /scripts/nfs_backup.sh
  #!/bin/bash
  #先找出180天以前的文件,进行删除,因为/backup目录下有一个nfs目录是nfs的备份文件所以它里面的内容不>
  能动,要给他排除掉
  for i in`find /backup/ -type d ! -name nfs`
  do
  #进行文件完整性校验
  [ -f $i/file.md5 ] && \
  if [ $(md5sum $i/file.md5 | awk -F "[: ]+" '{print $2}') = 'OK' ]
  then
  echo "文件备份成功" |mail -s "文件备份" 18163201@qq.com
  fi
  #删除80天以前的内容
  find $i -type f -mtime +180 | xargs -i rm -rf {}
  done
  # crontab -l -u root
  00 * * * * /bin/sh /scripts/backup_del_md5.sh

页: [1]
查看完整版本: 测试NFS+rsync+sersync+SMTP+shell脚本+cron