LLLL1007 2023-06-30 23:17 采纳率: 70%
浏览 28
已结题

关于脚本备份复制到webdav相关疑问


#!/bin/bash
# 备份源文件夹路径
source_paths=(
  /www/backup/panel
  /www/backup/database
  /www/backup/path
  /www/backup/site
)


RM_DIR=("/www/backup/panel" )
RM_DIR=("/www/wwwlogs/free_waf_log" )
RM_DIR=("/www/wwwlogs" )

#find $RM_DIR -type f -mtime +1-exec rm {} \;
#find $source_paths -type f -mtime +1 -exec rm {} \;

# 备份挂载目标文件夹路径
backup_path=/mnt

# 遍历需要备份的路径并复制到把备份挂载路径
for source_path in "${source_paths[@]}";
do
  # 检查源文件夹是否存在
  if [ -d "$source_paths" ]; then
  # 检查weddav是否已挂载
    if ! mount | grep $backup_path > /dev/null; then
      # weddav未挂载,尝试自动挂载
      #前提需要在/etc/davfs2/secrets 末尾添加:相关账户密码!
      mount -t davfs -o uid=0,gid=0,rw,file_mode=0777,dir_mode=0777 http://www.*****.top:1029/ssd2/work/baidu $backup_path
    fi
    # 源文件夹存在,复制到weddav挂载路径
    cp -r -u $source_path $backup_path
# 卸载weddav挂载路径
    umount -f davfs http://www.*****.top:1029/ssd2/work/baidu $backup_path
# 自动删除7天前的备份文件
 
  else
    # 源文件夹不存在,给出错误提示
    echo "Error: Source folder $source_path not found."

  fi

done


exit 0

这个脚本,几乎可以满足我备份的需求了,但有些需要优化。

疑问1.

所有的文件同步到wedav,卸载之后,需要自动删除多少天内,指定文件夹的文件,

find $RM_DIR -type f -mtime +1-exec rm {} \;

全部备份完成后,我用了这个命令,并不生效,不知道什么问题,请指点一下。

疑问2.
在这个系统执行这些命令的时候,出错了,并不会同步。

[root@iZj6c7h4enicwfgrnta8wzZ ~]# cat /proc/version
Linux version 3.10.0-1160.92.1.el7.x86_64 (mockbuild@kbuilder.bsys.centos.org) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) ) #1 SMP Tue Jun 20 11:48:01 UTC 2023

命令和ubuntu的一样,可是在这个系统就出错
出错提示:

[root@iZj6c7h4enicwfgrnta8wzZ ~]# ./aliyun_backup.sh
/sbin/mount.davfs: Warning: can't write entry into mtab, but will mount the file system anyway
umount: davfs: mountpoint not found
umount: /mnt:未挂载
/sbin/mount.davfs: found PID file /var/run/mount.davfs/mnt.pid.
Either /mnt is used by another process,
or another mount process ended irregular
umount: davfs: mountpoint not found
umount: http://www.***.top:1029/ssd2/work/aliyun: mountpoint not found
umount: /mnt:未挂载
/sbin/mount.davfs: found PID file /var/run/mount.davfs/mnt.pid.
Either /mnt is used by another process,
or another mount process ended irregular
umount: davfs: mountpoint not found
umount: http://www.***.top:1029/ssd2/work/aliyun: mountpoint not found
umount: /mnt:未挂载
/sbin/mount.davfs: found PID file /var/run/mount.davfs/mnt.pid.
Either /mnt is used by another process,
or another mount process ended irregular
umount: davfs: mountpoint not found
umount: http://www.***.top:1029/ssd2/work/aliyun: mountpoint not found
umount: /mnt:未挂载
[root@iZj6c7h4enicwfgrnta8wzZ ~]# 

请问是为何?

  • 写回答

1条回答 默认 最新

  • CF2301_77400554X 2023-07-01 00:22
    关注

    引用chatgpt内容作答:
    疑问1:
    您在脚本中使用的命令 find $RM_DIR -type f -mtime +1 -exec rm {} ; 是正确的,用于删除指定文件夹中7天前的文件。但是,在您的脚本中,您使用了多个变量 RM_DIR,每次赋值都会覆盖前面的赋值。因此,只有最后一个赋值有效,导致只有最后一个文件夹中的文件被删除。

    您可以将 RM_DIR 定义为一个数组,类似于 source_paths,并在需要删除的文件夹路径后面添加新的元素。然后,使用循环遍历 RM_DIR 中的每个路径,并执行删除操作。以下是修改后的代码示例:

    RM_DIR=(
      "/www/backup/panel"
      "/www/wwwlogs/free_waf_log"
      "/www/wwwlogs"
    )
    
    # 遍历需要删除的文件夹路径并删除7天前的文件
    for dir_path in "${RM_DIR[@]}"; do
      find "$dir_path" -type f -mtime +7 -exec rm {} \;
    done
    

    这样修改后,每个文件夹路径都会被遍历并执行删除操作。

    疑问2:
    您提到在某个系统上执行脚本时遇到了错误,导致同步失败。根据错误提示,似乎是由于 /mnt 目录没有正确挂载,导致无法卸载。

    可能的原因是您在挂载 webdav 之后,立即进行了同步和卸载操作,但挂载操作可能需要一些时间来完成,因此在同步和卸载之前需要等待一段时间。

    您可以尝试在挂载 webdav 后添加一个延迟等待一段时间,然后再进行同步和卸载操作。例如,使用 sleep 命令在挂载后等待几秒钟,再继续执行后续操作。以下是修改后的示例代码:

    # weddav未挂载,尝试自动挂载
    mount -t davfs -o uid=0,gid=0,rw,file_mode=0777,dir_mode=0777 http://www.*****.top:1029/ssd2/work/baidu $backup_path
    
    # 延迟等待一段时间(例如10秒)
    sleep 10
    
    # 源文件夹存在,复制到weddav挂载路径
    cp -r -u "$source_path" "$backup_path"
    
    # 卸载weddav挂载路径
    umount -f davfs "$backup_path"
    

    通过添加适当的延迟,可以确保挂载已完成,然后再进行后续操作。您可以根据实际情况调整延迟的时间。

    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 7月1日
  • 赞助了问题酬金15元 6月30日
  • 创建了问题 6月30日

悬赏问题

  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改
  • ¥15 Windows 系统cmd后提示“加载用户设置时遇到错误”
  • ¥50 vue router 动态路由问题
  • ¥15 关于#.net#的问题:End Function
  • ¥15 无法import pycausal
  • ¥15 VS2022创建MVC framework提示:预安装的程序包具有对缺少的注册表值的引用
  • ¥15 weditor无法连接模拟器Local server not started, start with?
  • ¥20 6-3 String类定义
  • ¥15 嵌入式--定时器使用