borgbackup备份软件,像快照一样的机制,只备份有差异的文件
borgbackup这个备份软件真好用,像是快照一样的机制,只备份有差异的文件
300MB的halo博客网站数据,备份了15天共15次,才占用不到1GB
borgbackup增量备份
参考资料
https://borgbackup.readthedocs.io/en/stable/quickstart.html#automating-backups
https://dt27.cn/site/small-bandwidth-vps-server-remote-incremental-backup-solution/
#安装borgbackup
apt install borgbackup
#初始化存储库,设置加密方式,存储库存放的所在位置 /root/borg.bak其实是一个目录
borg init --encryption=none /root/borg.bak
#备份前最好停了服务,防止在备份过程中数据库文件发生改变
#第一次备份 bak1是标识这次备份的名称 /opt/1panel/apps/halo/halo/data
borg create /root/borg.bak::bak1 /opt/1panel/apps/halo/halo/data
#往后的备份用这个命令 bak2是标识这次备份的名称
borg create --stats /root/borg.bak::bak2 /opt/1panel/apps/halo/halo/data
#查看存储库信息,占用多大等信息
borg info /root/borg.bak
#查看存储库,里面有哪些备份
borg list /root/borg.bak
#查看存储库里的叫bak2备份的文件
borg list /root/borg.bak::bak2
#提取叫bak1的备份,只能到当前目录,不能自定义目录
#这只适合目录结构完好的,不会创建被删掉的目录,不会还原被删掉的目录里面的文件
borg extract /root/borg.bak::bak1
#提取叫bak1备份中的这个文件夹,到当前目录
borg extract /root/borg.bak::bak1 opt/1panel/apps/halo/halo/data
#把叫bak6的备份 当作镜像一样挂载到本地/mnt/borg目录,然后把里面的文件拷贝覆盖回去,这样就能还原被删掉的目录,如果想要完全一致,不要多出来文件,那就手动先删掉当前目录
#borg mount /root/borg.bak /mnt/borg #这样可以挂载全部备份,但是可能比较慢,不推荐挂载整个存储库
borg mount /root/borg.bak::bak6 /mnt/borg
cp -a /mnt/borg/* /
#取消挂载,不能被占用着,例如当前正在该目录里,要先cd退出挂载的目录
cd /mnt
borg umount /mnt/borg
#删除叫bak2备份文件(请注意,这不会释放磁盘空间):
borg delete /root/borg.bak::bak2
#通过压缩borg.bak存储库中的段文件来恢复磁盘空间(上面删除后用这条才能释放磁盘空间)
borg compact /root/borg.bak
自动备份的脚本,用cron定时运行
#!/bin/sh
# Setting this, so the repo does not need to be given on the commandline:
export BORG_REPO=/www/cosfs/btbak-hk/btbak
# See the section "Passphrase notes" for more infos.
# 我这里不设置加密密码,所以注释了下面这个变量
# export BORG_PASSPHRASE='0123456789ABCDEF'
# 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 zstd,13 \
::'{hostname}-{now}' \
/www/wwwroot
backup_exit=$?
info "Pruning repository"
# Use the `prune` subcommand to maintain 7 daily, 4 weekly and 6 monthly
# archives of THIS machine. The '{hostname}-*' matching is very important to
# limit prune's operation to this machine's archives and not apply to
# other machines' archives also:
#保留规则,保存最近7天的每天备份,保存最近4个周的每周备份,保存最近6个月的每月备份
borg prune \
--list \
--glob-archives '{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}