实例备份
实例备份
目录
实例备份
要备份整个实例,我们建议做两件事: 1.) 数据库备份 2.) 文件系统备份。
下面是一个示例,适用于 Debian/Ubuntu 这类 Linux 机器:
apt install borgbackup backupninja
在 /etc/backup.d 下创建两个文件:
第一个:10-db.mysql:
### backupninja mysql config file ###
databases = all
backupdir = /var/backups/mysql
hotcopy = no
sqldump = yes
compress = yes
configfile = /etc/mysql/debian.cnf
sqldumpoptions= --skip-lock-tables
dbusername = [root_at_db]
dbpassword = [root_pass]
dbhost = 127.0.0.1
第二个:90.borg:
## for more options see
## - example.borg
## - http://borgbackup.readthedocs.io/en/stable
[source]
include = /var/spool/cron/crontabs
include = /var/backups
include = /var/www
## for more info see : borg prune -h
prune = yes
keep = "15d"
[dest]
directory = /backup/my_wavelog
host = localhost
port = 22
user = root
archive = {now:%Y-%m-%dT%H:%M:%S}
compression = lz4
encryption = none
passphrase =
完成上述设置后,你的系统会每晚 01:00 为数据库、Wavelog 及其 (e)QSL 卡等创建完整备份。
备份将存储在 /backup/my_wavelog。你可以通过 borg mount /backup/my_wavelog /mnt 轻松访问它。
挂载后,/mnt 的子文件夹中会有最近 15 天的备份。恢复完成后别忘了卸载(borg umount /mnt)。
有关这套工具链的更多信息,请参阅 backupninja 和 borgbackup。
这只是备份方案的一种方式 / 一个示例。
手动 Docker 备份
手动备份你的 Docker 容器相当简单。为确保数据一致,我们必须先停止容器。
sudo docker compose down
下一步我们定义备份路径并确保它存在。不要以 root 身份运行此命令
BACKUP_DIR=/home/$USER/wavelog_backup
mkdir -p $BACKUP_DIR
现在可以复制数据了。我们复制一份 docker compose 文件,并把 Docker 数据卷压缩成 tar.gz 文件
cp docker-compose.yml $BACKUP_DIR/
sudo tar czf "$BACKUP_DIR/wavelog_volumes_$(date +%F).tar.gz" -C /var/lib/docker/volumes $(sudo find /var/lib/docker/volumes -maxdepth 1 -type d -name 'wavelog*' -printf '%f\n')
要恢复备份,请复制 docker compose 文件并解压 tar.gz 文件
sudo tar xzf wavelog_volumes_2025-04-30.tar.gz -C /var/lib/docker/volumes
Docker SQL 导出(dump)
导出
docker exec wavelog-db mariadb-dump -u [DB_USER] -p'[DB_PASS]' [DB_NAME] > wavelog_backup.sql
恢复
docker exec -i wavelog-db mariadb -u [DB_USER] -p'[DB_PASS]' [DB_NAME] < wavelog_backup.sql
使用 Borg 进行 Docker 备份
这是另一种备份方法:备份前停止 Docker 容器,备份完成后重新启动它们,以确保数据库的完整性。停止容器是避免数据丢失的关键。
请根据需要进行调整,并把文件放到 /etc/borgmatic/wavelog.yaml:
source_directories:
- /home/phil/docker/wavelog
- /var/lib/docker/volumes/wavelog*
# A required list of local or remote repositories with paths and
# optional labels (which can be used with the --repository flag to
# select a repository). Tildes are expanded. Multiple repositories are
# backed up to in sequence. Borg placeholders can be used. See the
# output of "borg help placeholders" for details. See ssh_command for
# SSH options like identity file or port. If systemd service is used,
# then add local repository paths in the systemd service file to the
# ReadWritePaths list. Prior to borgmatic 1.7.10, repositories was a
# list of plain path strings.
repositories:
- path: ssh://user@host:22/./borgmatic/docker-wavelog
label: backupserver
# Passphrase to unlock the encryption key with. Only use on
# repositories that were initialized with passphrase/repokey/keyfile
# encryption. Quote the value if it contains punctuation, so it parses
# correctly. And backslash any quote or backslash literals as well.
# Defaults to not set.
# encryption_passphrase: "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~"
encryption_passphrase: changeme
# Keep all archives within this time interval.
# keep_within: 3H
# Number of secondly archives to keep.
# keep_secondly: 60
# Number of minutely archives to keep.
# keep_minutely: 60
# Number of hourly archives to keep.
# keep_hourly: 24
# Number of daily archives to keep.
keep_daily: 31
# Number of weekly archives to keep.
# keep_weekly: 4
# Number of monthly archives to keep.
keep_monthly: 6
# Number of yearly archives to keep.
keep_yearly: 1
# Stop docker container before backup to ensure database integrity; restart containers after backup.
before_backup:
- docker compose -f /home/phil/docker/wavelog/docker-compose.yml down
after_backup:
- docker compose -f /home/phil/docker/wavelog/docker-compose.yml up -d
然后添加一个 systemd 定时器文件(/etc/systemd/system/borgmatic-wavelog.timer)来安排备份。我每天在 04:20 和 15:30 各运行一次:
[Unit]
Description=Run Borgmatic Backup for wavelog
[Timer]
OnCalendar=*-*-* 04:20:00
OnCalendar=*-*-* 15:30:00
Persistent=true
[Install]
WantedBy=timers.target
重新加载 systemd 以应用定时器:sudo systemctl daemon-reload