lixiaolong 发表于 2018-8-21 09:39:00

用shell实现web网站全备份

  应用场景:备份公司web服务器数据,日志以及系统配置信息。
  脚本说明:本地使用tar备份,备份完成时使用md5sum 生成标志以便备份服务器上检查备份是否成功,备份结果用rsync推送到备份服务器(也可使用ftp方式上传至ftp服务器),备份服务器检查备份是否成功并发送邮件通知管理员。
  备份本地保留一周,服务器保留一月数据。
  分析:需要备份内容(1-4为配置文件,5-6为web服务器数据及日志)
  1、定时任务服务的配置文件/var/spool/cron/root
  2、日常脚本的目录
  3、web服务器数据,假定为/var/www/html
  4、日志,假定为/var/log/httpd
  首先,配置好rsync服务器和客户端,并测试可以使用;
  其次,本地tar打包备份;
  再次,使用rsync推送到服务器;
  最后,服务器端检查并邮件告警;
  测试各个阶段都没问题,设置定时任务。
  **************************************************************************************
  一、配置rsync 服务端
  1.配置rsync.conf
  uid= rsync
  gid= rsync
  usechroot = no
  maxconnections = 200
  timeout= 300
  pidfile = /var/run/rsyncd.pid
  lockfile = /var/run/rsync.lock
  logfile = /var/log/rsyncd.log
  
  comment= It's my test data!
  path= /data/
  ignoreerrors
  readonly = false
  list= false
  hostsallow = 192.168.147.0/24
  hostsdeny = 0.0.0.0/32
  authusers = rsync_backup
  secretsfile = /etc/rsyncd.passwd
  2.准备用户并授权
  # mkdir /data
  # useradd rsync -s/sbin/nologin -M
  # chown -Rrsync.rsync /data
  3.配置用于rsync同步的密码文件
  # echo"rsync_backup:123456" >> /etc/rsyncd.passwd
  # cat/etc/rsyncd.passwd
  rsync_backup:123456
  # chmod 600/etc/rsyncd.passwd
  # ll/etc/rsyncd.passwd
  -rw------- 1 root root 20 Sep7 17:29 /etc/rsyncd.passwd
  4.启动rsync服务
  二、配置rsync客户端
  # echo " 123456">> /etc/rsyncd.passwd
  # cat/etc/rsyncd.passwd
  123456
  # chmod 600/etc/rsyncd.passwd
  # ll/etc/rsyncd.passwd
  -rw------- 1 root root 20 Sep7 17:29 /etc/rsyncd.passwd
  三、编写shell脚本
  #!/bin/sh
  Date=$(date +%Y%m%d)
  Back_Dir="/backup/$Date"
  Back_file="var/spool/cron/rootetc/httpd/conf/httpd.conf "
  Back_web_dir=var/www/html
  Back_log_dir=var/log/httpd
  SIP="172.16.1.51::backup"
  Back_list="Back_fileBack_web_dirBack_log_dir "
  [ -d $Back_Dir] || mkdir -p $Back_Dir
  for i in `echo $Back_list `
  do
  cd / && tar czf $Back_Dir/$i.tar.gz${!i}
  if [ $? -eq 0 ]
  then
  cd $Back_Dir
  md5sum $i.tar.gz > $i.md5.txt
  rsync -avzp {$i.tar.gz,$i.md5.txt}rsync_backup@$SIP --password-file=/etc/rsyncd.passwd
  else
  echo "bak err!"
  fi
  doen
  #del local backup file,Keep a week data
  find $Back_Dir -type f -name "*.tar.gz" -mtime +7 | xargs rm -f
  服务器端检查邮件告警脚本在服务端完成:
  #!/bin/bash
  #define var
  Today=$(date +%F)
  Flag_file=/backup/192.168.147.128/flag.$Today.txt
  Mail_file=/opt/mail_bak_${Today}.txt
  #check flag
  if [ ! -f ${Flag_file} ]
  then
  echo "backup is error,pls view back server" > ${Mail_file}
  else
  find/backup -type f -name "flag.$(date +%F)*" | xargs md5sum -c | grep FAILED > ${Mail_file}
  fi
  #check Mail_file
  if [ -s ${Mail_file} ]
  then
  mail -s "$(date +%F-%T) back" esen_monitor@163.com
页: [1]
查看完整版本: 用shell实现web网站全备份