Borg 增量备份方案

一、为什么选择 BorgBackup

Borg Backup 是目前最受欢迎,用户量最大的一个备份支持程序,支持去重和压缩,同时也支持认证加密。其主要目的是提供一个高效而且安全的方法用于数据备份。数据的去重技术用于每日增量备份。它支持Linux、MacOS和BSD,并遵循BSD许可协议

BorgBackup 的主要优势:

  • 高效:BorgBackup 会将文件按数据块去重,只有改动的数据块才会被备份。一个 25 GiB 的虚拟机磁盘文件,只改动了 1 GiB,那就只会新增备份这 1 GiB 的数据;
  • 高速:核心算法使用 C 编译,使用缓存快速跳过未改动过的文件以加快备份速度;
  • 加密:数据默认是 AES-256 加密并且 HMAC-SHA256 校验的;
  • 压缩:支持多种压缩算法,可自动检测数据是否属于可被压缩的类型;
  • 异地备份:原生支持 SSH 备份到异地服务器,也可使用 NFS 等网络存储;
  • 可挂载:可以直接用 FUSE 挂载一个备份存档读取里面的数据;
  • 跨平台:支持 Linux, macOS, BSD, Windows (Cygwin / WSL) 等多种平台;
  • 开源:安全可审计,易于修改。

官网

https://www.borgbackup.org/

#!/bin/sh

# Setting this, so the repo does not need to be given on the commandline:
export BORG_REPO=ssh://username@example.com:2022/~/backup/main

# See the section "Passphrase notes" for more infos.
export BORG_PASSPHRASE='XYZl0ngandsecurepa_55_phrasea&&123'

# some helpers and error handling:
info() { printf "\n%s %s\n\n" "$( date )" "$*" >&2; }
trap 'echo $( date ) Backup interrupted >&2; exit 2' INT TERM

info "Starting backup"

# Backup the most important directories into an archive named after
# the machine this script is currently running on:

borg create                         \
    --verbose                       \
    --filter AME                    \
    --list                          \
    --stats                         \
    --show-rc                       \
    --compression lz4               \
    --exclude-caches                \
    --exclude 'home/*/.cache/*'     \
    --exclude 'var/tmp/*'           \
                                    \
    ::'{hostname}-{now}'            \
    /etc                            \
    /home                           \
    /root                           \
    /var                            \

backup_exit=$?

info "Pruning repository"

# Use the `prune` subcommand to maintain 7 daily, 4 weekly and 6 monthly
# archives of THIS machine. The '{hostname}-' prefix is very important to
# limit prune's operation to this machine's archives and not apply to
# other machines' archives also:

borg prune                          \
    --list                          \
    --prefix '{hostname}-'          \
    --show-rc                       \
    --keep-daily    7               \
    --keep-weekly   4               \
    --keep-monthly  6               \

prune_exit=$?

# actually free repo disk space by compacting segments

info "Compacting repository"

borg compact

compact_exit=$?

# use highest exit code as global exit code
global_exit=$(( backup_exit > prune_exit ? backup_exit : prune_exit ))
global_exit=$(( compact_exit > global_exit ? compact_exit : global_exit ))

if [ ${global_exit} -eq 0 ]; then
    info "Backup, Prune, and Compact finished successfully"
elif [ ${global_exit} -eq 1 ]; then
    info "Backup, Prune, and/or Compact finished with warnings"
else
    info "Backup, Prune, and/or Compact finished with errors"
fi

exit ${global_exit}

  

posted @ 2022-09-28 14:27  凡人半睁眼  阅读(839)  评论(0编辑  收藏  举报