This post documents a working setup for backing up ZFS encrypted datasets to cloud storage using Restic, with automatic key loading via systemd. The configuration handles multiple datasets independently and includes integrity checking procedures.
この投稿では、Resticを使用してZFS暗号化データセットをクラウドストレージにバックアップし、systemd経由で自動キーロードを行う構成を記録しています。この設定は複数のデータセットを独立して処理し、整合性チェック手順も含みます。

Why This Setup? / なぜこの構成?

ZFS provides excellent data integrity and snapshot capabilities, while Restic offers efficient deduplication and encryption for cloud backups. Combining them creates a robust backup strategy. The systemd integration ensures encrypted datasets are automatically unlocked at boot.
ZFSは優れたデータ整合性とスナップショット機能を提供し、Resticはクラウドバックアップのための効率的な重複排除と暗号化を提供します。これらを組み合わせることで堅牢なバックアップ戦略が作成されます。systemd統合により、起動時に暗号化データセットが自動的にアンロックされます。

Part 1: Systemd Auto-Loading for ZFS Encryption / パート1:ZFS暗号化のSystemd自動ロード

Setting Up Key Storage / キーストレージの設定

First, create a secure location for encryption keys:
まず、暗号化キーの安全な場所を作成:

# Create secure directory sudo mkdir -p /etc/zfs/keys sudo chmod 700 /etc/zfs/keys # Store your key echo "your-passphrase" | sudo tee /etc/zfs/keys/pool.key sudo chmod 600 /etc/zfs/keys/pool.key

Creating the Systemd Service / Systemdサービスの作成

Create `/etc/systemd/system/zfs-load-keys.service`:
`/etc/systemd/system/zfs-load-keys.service`を作成:

[Unit] Description=Load ZFS encryption keys and mount datasets After=zfs-import.target Before=zfs-mount.service [Service] Type=oneshot RemainAfterExit=yes ExecStart=/usr/local/bin/zfs-load-keys.sh [Install] WantedBy=zfs.target

The Key Loading Script / キーロードスクリプト

Create `/usr/local/bin/zfs-load-keys.sh`:
`/usr/local/bin/zfs-load-keys.sh`を作成:

#!/bin/bash # Auto-load ZFS keys and mount datasets echo "Loading ZFS encryption keys..." # Load keys for your datasets zfs load-key -L file:///etc/zfs/keys/pool.key pool/dataset1 zfs load-key -L file:///etc/zfs/keys/pool.key pool/dataset2 # Mount all datasets echo "Mounting ZFS datasets..." zfs mount -a # Verify mounts echo "Mounted datasets:" zfs mount | grep pool exit 0

Enable and test the service:
サービスを有効化してテスト:

sudo chmod +x /usr/local/bin/zfs-load-keys.sh sudo systemctl daemon-reload sudo systemctl enable zfs-load-keys.service sudo systemctl start zfs-load-keys.service

Part 2: Multi-Dataset Restic Configuration / パート2:マルチデータセットRestic設定

Environment Configuration / 環境設定

Create `/etc/restic/restic-env.sh` to manage multiple repositories:
複数のリポジトリを管理するために`/etc/restic/restic-env.sh`を作成:

#!/bin/bash # Restic Multi-Dataset Configuration # S3-compatible storage credentials export AWS_ACCESS_KEY_ID="your-access-key" export AWS_SECRET_ACCESS_KEY="your-secret-key" # Cache directory export RESTIC_CACHE_DIR="$HOME/.cache/restic" # Function to set repository based on dataset set_restic_repo() { local dataset=$1 case $dataset in dataset1) export RESTIC_REPOSITORY="s3:s3.provider.com/bucket/dataset1" export RESTIC_PASSWORD_FILE="/etc/restic/passwords/dataset1.key" ;; dataset2) export RESTIC_REPOSITORY="s3:s3.provider.com/bucket/dataset2" export RESTIC_PASSWORD_FILE="/etc/restic/passwords/dataset2.key" ;; *) echo "Unknown dataset: $dataset" return 1 ;; esac echo "Repository set to: $RESTIC_REPOSITORY" }

Universal Backup Script / 汎用バックアップスクリプト

This script creates ZFS snapshots before backing up, ensuring consistent backups:
このスクリプトはバックアップ前にZFSスナップショットを作成し、一貫性のあるバックアップを保証します:

#!/bin/bash # Restic Backup Script for ZFS Datasets set -euo pipefail DATASET_NAME=$1 ZFS_DATASET="pool/${DATASET_NAME}" # Load environment source /etc/restic/restic-env.sh set_restic_repo "$DATASET_NAME" || exit 1 # Create ZFS snapshot SNAPSHOT_NAME="${ZFS_DATASET}@restic-$(date +%Y%m%d-%H%M%S)" zfs snapshot "$SNAPSHOT_NAME" # Get snapshot path DATASET_MOUNT=$(zfs get -H -o value mountpoint "$ZFS_DATASET") SNAPSHOT_PATH="${DATASET_MOUNT}/.zfs/snapshot/$(basename $SNAPSHOT_NAME)" # Perform backup restic backup "$SNAPSHOT_PATH" \ --tag "zfs" \ --tag "$DATASET_NAME" \ --exclude-caches \ --one-file-system BACKUP_STATUS=$? # Cleanup if [ $BACKUP_STATUS -eq 0 ]; then zfs destroy "$SNAPSHOT_NAME" else echo "Backup failed. Keeping snapshot: $SNAPSHOT_NAME" fi exit $BACKUP_STATUS

Part 3: Integrity Checking / パート3:整合性チェック

Regular integrity checks ensure backup reliability. There are three levels of checking:
定期的な整合性チェックはバックアップの信頼性を保証します。3つのレベルのチェックがあります:

  • Quick check: Verifies repository structure only
  • Subset check: Reads a percentage of data (5-10%)
  • Full check: Reads all data (bandwidth-intensive)

# Quick structural check restic check # Check 5% of data randomly restic check --read-data-subset=5% # Full data verification (use sparingly) restic check --read-data

Automated Check Script / 自動チェックスクリプト

Create a script to check all repositories:
すべてのリポジトリをチェックするスクリプトを作成:

#!/bin/bash # Integrity check for all repositories source /etc/restic/restic-env.sh DATASETS="dataset1 dataset2 dataset3" CHECK_TYPE="${1:-quick}" for dataset in $DATASETS; do set_restic_repo "$dataset" case $CHECK_TYPE in quick) restic check ;; subset) restic check --read-data-subset=5% ;; full) restic check --read-data ;; esac done

Part 4: Scheduling with Cron / パート4:Cronでのスケジューリング

Set up automated backups and checks:
自動バックアップとチェックを設定:

# Daily backups at 2 AM 0 2 * * * /usr/local/bin/restic-backup-zfs.sh dataset1 30 2 * * * /usr/local/bin/restic-backup-zfs.sh dataset2 # Weekly subset integrity check 0 3 * * 0 /usr/local/bin/restic-check-all.sh subset # Monthly full check 0 2 1 * * /usr/local/bin/restic-check-all.sh full

Best Practices / ベストプラクティス

Repository Organization / リポジトリの整理

• Separate repositories for different data types
• Independent snapshot retention policies
• Easier restoration and maintenance
• 異なるデータタイプ用の個別リポジトリ
• 独立したスナップショット保持ポリシー
• より簡単な復元とメンテナンス

Security Considerations / セキュリティの考慮事項

• Store passwords in files, not environment variables
• Restrict key file permissions (600)
• Consider using different passwords per repository
• パスワードは環境変数ではなくファイルに保存
• キーファイルの権限を制限(600)
• リポジトリごとに異なるパスワードを検討

Testing Your Setup / セットアップのテスト

Always verify your backups work by performing test restores:
テストリストアを実行してバックアップが機能することを常に確認:

# List available snapshots restic snapshots # Restore latest snapshot to test directory restic restore latest --target /tmp/test-restore # Verify restored data ls -la /tmp/test-restore # Clean up rm -rf /tmp/test-restore

Conclusion / まとめ

This setup provides automated, reliable backups for ZFS datasets with minimal manual intervention. The combination of ZFS snapshots and Restic's deduplication creates an efficient backup system suitable for both personal and production use.
この設定は、最小限の手動介入でZFSデータセットの自動化された信頼性の高いバックアップを提供します。ZFSスナップショットとResticの重複排除の組み合わせは、個人使用と本番使用の両方に適した効率的なバックアップシステムを作成します。

Remember to regularly test your restore procedures — a backup is only as good as its ability to restore when needed.
定期的にリストア手順をテストすることを忘れないでください — バックアップは必要な時にリストアできる能力と同じくらい良いものです。

Ara / アラ

Questions about ZFS or Restic? Feel free to reach out.
ZFSやResticについて質問がありますか?お気軽にお問い合わせください。