视频加载失败

【Linux】文件备份脚本

323 字
2 分钟
【Linux】文件备份脚本
【Linux】文件备份脚本
#!/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"

支持与分享

如果这篇文章对你有帮助,欢迎分享给更多人或打赏支持!

打赏
【Linux】文件备份脚本
https://rehtt.com/posts/256/
作者
Rehtt
发布于
2022-10-26
许可协议
CC BY-NC-SA 4.0

评论区

Profile Image of the Author
Rehtt
Hello, I'm Rehtt.
公告
欢迎来到我的博客!
分类
标签
最新动态
站点统计
文章
150
分类
17
标签
73
总字数
67,772
运行时长
0 天
最后活动
0 天前
站点信息
构建平台
Local
博客版本
Firefly v6.16.8
文章许可
CC BY-NC-SA 4.0

当前页面没有目录

文章目录

当前页面没有目录