#!/bin/bash
backup_num=3
function help() {
echo "-in 源文件路径"
echo "-out 备份路径"
echo "-backup_num 备份数量"
echo "-inKey 使用公钥加密,详情:https://github.com/Rehtt/archive"
echo "-genKey 生成密钥对,详情:https://github.com/Rehtt/archive"
exit 0
}
while [ $# -gt 0 ]; do
case $1 in
-in)
in_path="$2"
shift
;;
-out)
out="$2"
shift
;;
-backup_num)
backup_num=$2
shift
;;
-inKey)
inKey="$2"
shift
;;
-genKey)
archive -generateRsaKey
exit 0
shift
;;
-help)
help
;;
-h)
help
;;
esac
shift
done
if [ x"$in_path" = x ]; then
help
fi
if [ x"$out" = x ]; then
help
fi
filename=$(basename "$in_path")
tmp_file="/tmp/$filename-$(date +%Y-%m-%d).tar.xz"
out="$out/$filename"
function get_last_file() {
local time=1
del_file=""
file_num=0
while [ $# -gt 0 ]; do
local name=$1
local name=${name##*/}
local t=$(echo $name | grep -o '[[:digit:]]\{4,\}-[[:digit:]]\{1,2\}-[[:digit:]]\{1,2\}')
if [ x"$t" = x ]; then
shift
continue
fi
local date_str=$(date +%s -d "$t")
file_num=$(($file_num + 1))
# echo $name
local now=$(date +%s)
if [[ $(($now - $date_str)) -gt $time ]]; then
local time=$(($now - $date_str))
del_file=$name
fi
shift
done
}
#判断是否加密
if [ x"$inKey" = x ]; then
tar -Jcvf "$tmp_file" "$in_path"
else
tmp_file=$tmp_file.d
if [ x"$(which archive)" = x ]; then
echo "缺少archive"
help
fi
archive -a -e -inKey "$inKey" -in "$in_path" -out "$tmp_file"
fi
# 判断是本地保存还是用rclone保存
if [[ $(echo "$out" | grep ":") != "" ]]; then
if [ x"$(which rclone)" = x ]; then
echo "缺少rclone"
help
fi
rclone mkdir "$out"
list=$(rclone ls "$out")
get_last_file $list
while [ $file_num -ge $backup_num ]; do
rclone delete "$out/$del_file"
list=$(rclone ls "$out")
get_last_file $list
done
rclone copy "$tmp_file" "$out"
else
mkdir -p "$out"
list=$(ls "$out")
get_last_file $list
while [ $file_num -ge $backup_num ]; do
rm -f "$out/$del_file"
list=$(ls "$out")
get_last_file $list
done
cp "$tmp_file" "$out"
fi
rm -f "$tmp_file"
Last modification:October 26, 2022
© Allow specification reprint
5 comments
是大佬ヾ(≧∇≦*)ゝ
你这是搭建的个人博客网站嘛?
是啊
妙啊OωO