#!/bin/bash # Backup and Upload Script # Author: zhaozhuang time_now=$(date +%Y-%m-%d) packages_path="./packages" function delete_files() { local filename=$1 local work_path=$2 files=$(ls "$work_path") for f in $files; do if [[ $f == *bak2022-* && $f == $filename* ]]; then echo "删除之前备份的文件$f" rm "$work_path/$f" fi done } function backup_and_upload() { num=0 while IFS= read -r file; do file=${file//\\//} file_name=$(basename "$file") file_bak="$file"_bak"$time_now" file_name_bak="$file_name"_bak"$time_now" parent_file_path=$(dirname "$file") echo "文件$file的父路径为$parent_file_path" delete_files "$file_name" "$parent_file_path" if [ -e "$file" ]; then mv "$file" "$file_bak" echo "将$file_name重命名为$file_name_bak" else echo "源文件$file不存在,该文件为新增" fi cp "$packages_path/$file_name" "$file" echo "成功复制:$file_name" ((num++)) done <<< "$1" echo "num=$num" } # 读取 config.txt 文件中的两行路径,并调用 backup_and_upload 函数 while IFS= read -r line; do backup_and_upload "$line" done < "config.txt"