跳转到内容
View in the app

A better way to browse. Learn more.

彼岸论坛

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.
欢迎抵达彼岸 彼岸花开 此处谁在 -彼岸论坛

[VPS] 给你免费的甲骨文小鸡上个保险:如何用 restic 备份 vaultwarden 数据

发表于

在甲骨文免费的机器上用 docker 运行着一个 vaultwarden 服务,用来管理自己的所有密码。

因为是免费的 vps ,为了避免机器被忽然回收导致数据丢失,因此需要定期备份 vaultwarden 的数据库。

首先 vaultwarden 使用了 docker compose 部署,部署文件如下:

services:
  vaultwarden:
    image: vaultwarden/server:latest
    container_name: vaultwarden
    restart: always
    environment:
      SIGNUPS_ALLOWED: false # Deactivate this with "false" after you have created your account so that no strangers can register
    volumes:
      - ./vw-data:/data # the path before the : can be changed
    ports:
      - 11001:80 # you can replace the 11001 with your preferred port

因此在 vw-data 下面,保存着所有密码数据,只要备份这个目录,就可以还原。

备份工具使用 restic ,它支持将数据直接备份到远程,下面步骤主要是设置好备份地址,然后定期执行的操作细节:

一: 首先安装 restic

sudo apt update
sudo apt install restic

二: 初始化 restic 仓库

先设置好环境变量:

export RESTIC_REPOSITORY=s3:https://<minio-host>:<minio-port>/<bucket-name>/<subpath>
export AWS_ACCESS_KEY_ID=<your-access-key>
export AWS_SECRET_ACCESS_KEY=<your-secret-key>
export RESTIC_PASSWORD=<your-password>

然后执行:

restic init

执行成功后,会在远程 S3 目录 https://<minio-host>:<minio-port>/<bucket-name>/<subpath> 看到一些初始化 meta 数据。

三: 创建备份并清理旧数据的脚本 backup_and_prune.sh ,内容如下:

#!/bin/bash

# 设置环境变量
export RESTIC_REPOSITORY=s3:https://<minio-host>:<minio-port>/<bucket-name>/<subpath>
export AWS_ACCESS_KEY_ID=<your-access-key>
export AWS_SECRET_ACCESS_KEY=<your-secret-key>
export RESTIC_PASSWORD=<your-password>

# 备份目录
BACKUP_SOURCE="/home/ubuntu/vaultwarden/vwdata"

# 日志文件
LOG_FILE="/home/ubuntu/back.log"

# 执行备份并记录日志
restic backup $BACKUP_SOURCE >> $LOG_FILE 2>&1

# 删除超过 7 天的旧备份并记录日志
restic forget --keep-daily 7 --prune >> $LOG_FILE 2>&1

# 记录快照信息
restic snapshots >> $LOG_FILE 2>&1

请自行将里面的变量和路径替换为实际值。

四: 设置定期执行:

使用 cron 来设置每天定期执行备份任务。

crontab -e

添加以下行来设置每天凌晨 2 点执行备份脚本:

0 2 * * * /home/ubuntu/backup_and_prune.sh

确保你的脚本有执行权限:

chmod +x /home/ubuntu/backup_and_prune.sh

这样你的备份脚本就会每天凌晨 2 点执行一次了。

五: 验证备份

验证数据是否正常的最好办法是让 vaultwarden 加载备份的数据,然后在网页查看数据是否是最新的。因此接下来的操作是将远程的备份下载到本地,然后在本地执行的具体操作。

首先设置环境变量

export RESTIC_REPOSITORY=s3:https://<minio-host>:<minio-port>/<bucket-name>/<subpath>
export AWS_ACCESS_KEY_ID=<your-access-key>
export AWS_SECRET_ACCESS_KEY=<your-secret-key>
export RESTIC_PASSWORD=<your-password>

列出所有快照:

restic snapshots

恢复某个快照到指定目录:

restic restore <snapshot-id> --target ~/Downloads/

根据本文的例子,最终的文件还原到了 ~/Downloads/vw-data 下面。

本地临时运行一个 docker compose 文件:

services:
  vaultwarden:
    image: vaultwarden/server:latest
    container_name: vaultwarden
    restart: always
    environment:
      SIGNUPS_ALLOWED: false # Deactivate this with "false" after you have created your account so that no strangers can register
    volumes:
      - ~/Downloads/vw-data:/data # the path before the : can be changed
    ports:
      - 11001:80 # you can replace the 11001 with your preferred port

docker compose up 启动后,访问 localhost:11001 查看数据正常就表示备份没有问题了。

本文首发于: https://blog.tomyail.com/backup-vaultwarden-data-using-restic/ 转载请注明出处

Featured Replies

No posts to show

创建帐户或登录来提出意见

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.