> "$nas_today" while read -r line do nas_backup=$line done < "$nas_today" rm /home/artinx/.AutoBackup/nas_today.log check="$today_backup" if [ "$nas_backup" = "$check" ] then echo -e "$today\nupload successfully\n" >> /home/artinx/.AutoBackup/upload.log else echo -e "$today\nupload failed\n" >> /home/artinx/.AutoBackup/upload.log fi # Auto delete full system backup(s) ssh nas "cd $nas_target_path; ls" > "$nas_backups" oldest=$(date --date="$today -$savedays days" "> > "$nas_today" while read -r line do nas_backup=$line done < "$nas_today" rm /home/artinx/.AutoBackup/nas_today.log check="$today_backup" if [ "$nas_backup" = "$check" ] then echo -e "$today\nupload successfully\n" >> /home/artinx/.AutoBackup/upload.log else echo -e "$today\nupload failed\n" >> /home/artinx/.AutoBackup/upload.log fi # Auto delete full system backup(s) ssh nas "cd $nas_target_path; ls" > "$nas_backups" oldest=$(date --date="$today -$savedays days" "> > "$nas_today" while read -r line do nas_backup=$line done < "$nas_today" rm /home/artinx/.AutoBackup/nas_today.log check="$today_backup" if [ "$nas_backup" = "$check" ] then echo -e "$today\nupload successfully\n" >> /home/artinx/.AutoBackup/upload.log else echo -e "$today\nupload failed\n" >> /home/artinx/.AutoBackup/upload.log fi # Auto delete full system backup(s) ssh nas "cd $nas_target_path; ls" > "$nas_backups" oldest=$(date --date="$today -$savedays days" ">
#!/bin/bash

savedays=7
num_keep_local=3
nas_backups="/home/artinx/.AutoBackup/nas-backups.log"
# volume path in synology nas
nas_target_path="/volume1/backup/jira"

timeshift --create
today=$(date -I)
cd /timeshift/snapshots
today_backup=$(ls | grep "$today")
# delete old local backups
ls -dt */ | tail -n +$((num_keep_local+1)) | xargs -I {} rm -rf {}
rsync -av "$today_backup" nas:${nas_target_path}/${today}
nas_today="/home/artinx/.AutoBackup/nas_today.log"
ssh nas "cd $nas_target_path; ls | grep $today; exit" >> "$nas_today"
while read -r line
do
    nas_backup=$line
done < "$nas_today"
rm /home/artinx/.AutoBackup/nas_today.log
check="$today_backup"
if [ "$nas_backup" = "$check" ]
then
    echo -e "$today\\nupload successfully\\n" >> /home/artinx/.AutoBackup/upload.log
else
    echo -e "$today\\nupload failed\\n" >> /home/artinx/.AutoBackup/upload.log
fi

# Auto delete full system backup(s)
ssh nas "cd $nas_target_path; ls" > "$nas_backups"
oldest=$(date --date="$today -$savedays days" +%s)
i=0
while read -r line
do
    filenames[$i]=$line
    ((++i))
done < "$nas_backups"
((--i))
first=true
echo "$today" >> /home/artinx/.AutoBackup/delete-history.log
count=0
for ((j=0; j!=i; ++j))
do
    if [ $(date --date="$(echo ${filenames[$j]} | cut -c1-10)" +%s) -le $oldest ]
    then
        ssh nas "rm -rf $nas_target_path/${filenames[$j]}"
        echo "delete ${filenames[$j]} from nas" >> /home/artinx/.AutoBackup/delete-history.log
        first=false
        ((++count))
    fi
done
if [ "$first" = true ]
then
    echo -e "delete nothing from nas\\n" >> /home/artinx/.AutoBackup/delete-history.log
else
    echo -e "totally delete $count file(s)\\n" >> /home/artinx/.AutoBackup/delete-history.log
fi

该脚本的主要功能是:

  1. 创建本地 Timeshift 备份。
  2. 将备份上传到 Synology NAS。
  3. 删除超过指定保留时间(7天)的旧备份文件(无论是本地还是 NAS 上)。
  4. 将每次备份的上传和删除操作结果记录在日志文件中。

详解:

0. 前提条件

需要备份的 Linux 系统可以直接通过 ssh 密钥连接群晖(或者你用来存放备份文件的机器),实现方法为如下:

  1. 生成 SSH 密钥对

    已经生成的情况下请不要再次生成,以免影响运行中的备份服务!

    查看 ~/.ssh 文件夹确认是否已经存在 ssh 密钥。

 ssh-keygen -t rsa -b 4096 -C "[email protected]"
  1. 将公钥复制到远程服务器
 ssh-copy-id username@remote_host

系统会提示输入该远程系统的密码,验证通过后,公钥将被添加到远程服务器的 ~/.ssh/authorized_keys 文件中。

  1. 检查远程服务器的权限配置

确保远程服务器上的 ~/.ssh 目录及 authorized_keys 文件的权限设置正确: