【Linux】文件备份脚本
323 字
2 分钟
【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 shiftdone
if [ x"$in_path" = x ]; then helpfiif [ x"$out" = x ]; then helpfi
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"firm -f "$tmp_file"支持与分享
如果这篇文章对你有帮助,欢迎分享给更多人或打赏支持!
相关文章智能推荐
1
Linux开机启动脚本编写
Linux笔记2017-12-04
2
【linux】目录
Linux笔记2024-02-19
3
【Linux】硬盘休眠
Linux笔记2018-12-10
4
【Linux】常用命令
Linux笔记2018-06-19
5
Linux硬盘操作
Linux笔记2018-06-13
随机文章随机推荐















加载更多…