自学内容网 自学内容网

对运行的服务所在目录就行备份

管理一个Web服务器的备份任务

  1. 检查并确认服务器上的特定服务(如Nginx或Apache)是否正在运行。

  2. 停止该服务以进行备份(如果必要)。

  3. 创建一个备份目录,如果它不存在的话。

  4. 使用rsync命令将Web服务器的根目录同步到备份目录。

  5. (可选)压缩备份目录。

  6. 重新启动之前停止的服务。

  7. 通过backup.log文件(或失败的通知,如果脚本中的任何步骤失败)。

    oot@hcss-ecs-c2b8:/var/test# vim backup.sh
    root@hcss-ecs-c2b8:/var/test# ./backup.sh
    -bash: ./backup.sh: Permission denied
    root@hcss-ecs-c2b8:/var/test# chmod +x backup.sh 
    root@hcss-ecs-c2b8:/var/test# ./backup.sh
    停止服务 nginx 以进行备份...
    正在将 /var/www/html 同步到 /mnt/backups/webserver/20241011_002357...
    sending incremental file list
    rsync: [sender] change_dir "/var/www/html" failed: No such file or directory (2)
    
    sent 19 bytes  received 12 bytes  62.00 bytes/sec
    total size is 0  speedup is 0.00
    rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1333) [sender=3.2.3]
    Unknown command verb is-inactive. # 这里我们脚本里面 systemctl 参数写错了进行修改
    root@hcss-ecs-c2b8:/var/test# chmod +x backup.sh 
    root@hcss-ecs-c2b8:/var/test# chmod +x backup.sh 
    root@hcss-ecs-c2b8:/var/test# vim backup.sh
    #脚本正文开始
    
    
    #!/bin/bash  
    
    # 配置  
    SERVICE_NAME="nginx"  # 要检查的服务名称(nginx或apache2等)  
    WEB_ROOT="/var/www/html"  # Web服务器的根目录  
    BACKUP_DIR="/mnt/backups/webserver/$(date +%Y%m%d_%H%M%S)"  # 备份目录  
    
    # 检查服务是否正在运行  
    if ! systemctl is-active --quiet "$SERVICE_NAME"; then
      echo "服务 $SERVICE_NAME 未运行,无需停止。"  
    else
      echo "停止服务 $SERVICE_NAME 以进行备份..."  
      systemctl stop "$SERVICE_NAME"
      if [ $? -ne 0 ]; then
        echo "无法停止服务 $SERVICE_NAME。备份中止。" >> backup.log
        exit 1
      fi
    fi
    
    # 创建备份目录  
    mkdir -p "$BACKUP_DIR"
    if [ $? -ne 0 ]; then
      echo "无法创建备份目录 $BACKUP_DIR。备份中止。" >> backup.log
      # 如果之前停止了服务,则尝试重新启动它  
      if !  systemctl is-active --quiet "$SERVICE_NAME"; then
        systemctl start "$SERVICE_NAME"
      fi
      exit 1
    fi
    
    # 使用rsync进行备份  
    echo "正在将 $WEB_ROOT 同步到 $BACKUP_DIR..."  
    rsync -av "$WEB_ROOT/" "$BACKUP_DIR/"
    if [ $? -ne 0 ]; then
      echo "rsync备份失败。备份中止。" >> backup.log
      # 清理备份目录  
      rm -rf "$BACKUP_DIR"
      # 如果之前停止了服务,则尝试重新启动它  
      if ! systemctl is-active --quiet "$SERVICE_NAME"; then
        systemctl start "$SERVICE_NAME"
      fi
      exit 1
    fi
    
    # (可选)压缩备份目录  
    # 注意:这一步可能会消耗大量时间和CPU资源,对于大型目录可能不适合在备份时立即执行。    
    # tar -czf "${BACKUP_DIR}.tar.gz" -C "$(dirname "$BACKUP_DIR")" "$(basename "$BACKUP_DIR")"  
    
    # 重新启动服务  
    if ! systemctl is-active --quiet "$SERVICE_NAME"; then
      echo "重新启动服务 $SERVICE_NAME..."  
      systemctl start "$SERVICE_NAME"
      if [ $? -ne 0 ]; then
        echo "无法重新启动服务 $SERVICE_NAME。请手动检查。" >> backup.log
      fi
    fi
    
    # 
    echo "备份成功!备份目录:$BACKUP_DIR"  >> backup.log
    
    echo "脚本执行完毕。"
    
    #脚本正文结束
    
    root@hcss-ecs-c2b8:/var/test# ./backup.sh
    服务 nginx 未运行,无需停止。
    正在将 /var/www/html 同步到 /mnt/backups/webserver/20241011_002633...
    sending incremental file list
    rsync: [sender] change_dir "/var/www/html" failed: No such file or directory (2)
    
    sent 19 bytes  received 12 bytes  62.00 bytes/sec
    total size is 0  speedup is 0.00
    rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1333) [sender=3.2.3]
    root@hcss-ecs-c2b8:/var/test# 
    
    

    思考: 我们既然能对web服务进行备份那么一定也可以对游戏后端服务进行备份等也可以吧备份信息从backup.log改到发送邮件到运维人员的邮箱上,使用这个命令mail -s $message_content $emai_address 可以完成邮件发送


原文地址:https://blog.csdn.net/qq_56440330/article/details/142835306

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!