mcpField
B
backup-recovery

backup-recovery

github.com/bagelholeクラウド・DevOps1.2k412 回インストール23 週間前に更新
リポジトリ

backup-recovery について

サーバーやアプリケーションのデータを管理する人向けのバックアップ・リカバリースキルです。rsync、Restic、ローカルおよびクラウドストレージ、定期実行、復元検証を扱います。

主な機能

  1. 01rsync と Restic によるデータバックアップ
  2. 02ローカル、S3、B2 のバックエンド
  3. 03Restic による暗号化と重複排除
  4. 04systemd timer または cron による定期実行
  5. 05スナップショットと保持ポリシーの管理
  6. 06データ復元とリポジトリ整合性の確認

活用シーン

  1. 01サーバーやアプリケーションデータのバックアップ設計
  2. 02ローカルまたはクラウドストレージへの定期バックアップ
  3. 03誤削除や災害後のデータ復元
  4. 04バックアップの整合性確認と復旧手順のテスト

インストール

コマンドをコピーしてターミナルで実行します。

$npx skills add https://github.com/bagelhole/devops-security-agent-skills --skill backup-recovery

インストール後、Claude Code で/backup-recoveryを入力して読み込まれたか確認します。

詳細

バックアップと復旧

rsync、Restic、クラウドストレージのバックエンドを使用して、包括的なバックアップと復旧の戦略を実装します。3-2-1 ルール、自動スケジュール設定、S3/B2 バックエンド、暗号化、復元手順、検証テストを扱います。

使用する場面

  • サーバー、データベース、アプリケーションデータのバックアップ戦略を設計する
  • ローカルまたはクラウドストレージへの暗号化・重複排除バックアップ用に Restic を設定する
  • systemd タイマーまたは cron でバックアップを自動化する
  • 誤削除、破損、災害の発生後にデータを復元する
  • バックアップ/復元のワークフローを使用して、環境間でデータを移行する
  • バックアップの整合性を検証し、復旧手順をテストする

前提条件

  • rsync がインストールされていること(ほとんどの Linux ディストリビューションに含まれています)
  • restic がインストールされていること(v0.16+ を推奨)
  • バックエンド用のクラウド CLI が設定されていること:S3 用の AWS CLI、Backblaze B2 用の b2 CLI
  • バックアップ先に十分なストレージがあること(保持期間に応じてソースサイズの 2~3 倍)
  • リモート rsync ターゲットへの SSH アクセス
  • スケジュール設定用の systemd または cron

3-2-1 バックアップルール

  • データのコピーを3つ保持します(プライマリ 1 つ+バックアップ 2 つ)。
  • 異なるストレージメディアまたは種類を2つ使用します(例:ローカルディスク+クラウド)。
  • オフサイトにコピーを1つ保管します(クラウドストレージ、リモートデータセンター)。

rsync バックアップ

基本操作
# Sync a local directory to a backup location
rsync -avz --delete /data/ /backup/data/

# Flags explained:
# -a  archive mode (preserves permissions, ownership, timestamps, symlinks)
# -v  verbose output
# -z  compress data during transfer
# --delete  remove files at destination that no longer exist at source

# Sync to a remote server over SSH
rsync -avz -e "ssh -i ~/.ssh/backup_key" /data/ backup@remote:/backups/server01/

# Exclude patterns
rsync -avz --delete \
  --exclude='*.tmp' \
  --exclude='*.log' \
  --exclude='.cache/' \
  --exclude='node_modules/' \
  /data/ /backup/data/

# Use an exclude file for complex patterns
rsync -avz --delete --exclude-from=/etc/backup-excludes.txt /data/ /backup/data/

# Dry run (preview what would change)
rsync -avzn --delete /data/ /backup/data/

# Limit bandwidth to 50 MB/s and show progress
rsync -avz --bwlimit=50000 --progress /data/ backup@remote:/backups/
ハードリンクを使用した増分バックアップ
# Incremental: unchanged files hard-linked to previous backup (saves space)
rsync -avz --delete \
  --link-dest=/backup/daily/latest \
  /data/ /backup/daily/$(date +%Y-%m-%d)/

# Update the 'latest' symlink
ln -snf /backup/daily/$(date +%Y-%m-%d) /backup/daily/latest

# Remove backups older than 30 days
find /backup/daily -maxdepth 1 -type d -name "20*" -mtime +30 -exec rm -rf {} \;

Restic バックアップ

インストール
# Debian / Ubuntu
apt install -y restic

# RHEL / CentOS
dnf install -y restic

# Or download the latest binary
curl -L https://github.com/restic/restic/releases/latest/download/restic_0.17.3_linux_amd64.bz2 \
  | bunzip2 > /usr/local/bin/restic
chmod +x /usr/local/bin/restic

# Verify installation
restic version
リポジトリの初期化
# Local repository
restic init --repo /backup/restic-repo

# AWS S3 backend
export AWS_ACCESS_KEY_ID="AKIAEXAMPLE"
export AWS_SECRET_ACCESS_KEY="secretkey"
restic init --repo s3:s3.amazonaws.com/my-backup-bucket

# S3-compatible (MinIO)
export AWS_ACCESS_KEY_ID="minioadmin"
export AWS_SECRET_ACCESS_KEY="miniosecret"
restic init --repo s3:http://minio.example.com:9000/backup-bucket

# Backblaze B2 backend
export B2_ACCOUNT_ID="accountid"
export B2_ACCOUNT_KEY="accountkey"
restic init --repo b2:my-backup-bucket:server01

# SFTP backend
restic init --repo sftp:backup@remote:/backups/server01

# Restic will prompt for a repository password -- store it securely
# Use a password file for automation
echo "my-secure-repo-password" > /etc/restic/password.txt
chmod 600 /etc/restic/password.txt
バックアップ操作
# Basic backup
restic backup /data --repo /backup/restic-repo --password-file /etc/restic/password.txt

# Backup multiple directories
restic backup /data /etc /var/lib/postgresql \
  --repo s3:s3.amazonaws.com/my-backup-bucket \
  --password-file /etc/restic/password.txt

# Backup with exclusions
restic backup /data \
  --exclude='*.tmp' \
  --exclude='*.log' \
  --exclude-file=/etc/restic/excludes.txt \
  --repo /backup/restic-repo \
  --password-file /etc/restic/password.txt

# Backup with tags (useful for filtering snapshots later)
restic backup /data \
  --tag server01 --tag production --tag daily \
  --repo /backup/restic-repo \
  --password-file /etc/restic/password.txt

# Backup stdin (e.g., database dump)
pg_dump -U postgres mydb | restic backup --stdin --stdin-filename mydb.sql \
  --repo s3:s3.amazonaws.com/my-backup-bucket \
  --password-file /etc/restic/password.txt

# Verbose output showing files processed
restic backup /data -v \
  --repo /backup/restic-repo \
  --password-file /etc/restic/password.txt
スナップショットの管理
# List all snapshots (add --tag <tag> to filter)
restic snapshots --repo /backup/restic-repo --password-file /etc/restic/password.txt

# Browse files in the latest snapshot
restic ls latest --repo /backup/restic-repo --password-file /etc/restic/password.txt

# Compare two snapshots
restic diff abc123 def456 --repo /backup/restic-repo --password-file /etc/restic/password.txt
保持ポリシー(forget + prune)
# Apply retention policy and reclaim space
restic forget \
  --keep-daily 7 --keep-weekly 4 --keep-monthly 12 --keep-yearly 3 \
  --prune \
  --repo /backup/restic-repo \
  --password-file /etc/restic/password.txt

# Dry run to preview what would be removed
restic forget --keep-daily 7 --keep-weekly 4 --keep-monthly 12 \
  --dry-run --repo /backup/restic-repo --password-file /etc/restic/password.txt
復元手順
# Restore the latest snapshot to a target directory
restic restore latest --target /restore \
  --repo /backup/restic-repo \
  --password-file /etc/restic/password.txt

# Restore a specific snapshot by ID
restic restore abc123 --target /restore \
  --repo /backup/restic-repo \
  --password-file /etc/restic/password.txt

# Restore only specific files or directories
restic restore latest --target /restore --include "/data/config" \
  --repo /backup/restic-repo \
  --password-file /etc/restic/password.txt

# Mount a snapshot as a FUSE filesystem (browse and copy individual files)
mkdir -p /mnt/restic
restic mount /mnt/restic --repo /backup/restic-repo --password-file /etc/restic/password.txt &
# Browse: ls /mnt/restic/snapshots/latest/
# Unmount when done: umount /mnt/restic
検証
# Verify repository integrity (checks all data and metadata)
restic check --repo /backup/restic-repo --password-file /etc/restic/password.txt

# Full data verification (reads all pack files -- slow but thorough)
restic check --read-data --repo /backup/restic-repo --password-file /etc/restic/password.txt

# Verify a random subset of data (faster than full read)
restic check --read-data-subset=5% --repo /backup/restic-repo --password-file /etc/restic/password.txt

環境ファイルを使ったバックアップの自動化

/etc/restic/env
# Repository configuration
export RESTIC_REPOSITORY="s3:s3.amazonaws.com/my-backup-bucket"
export RESTIC_PASSWORD_FILE="/etc/restic/password.txt"
export AWS_ACCESS_KEY_ID="AKIAEXAMPLE"
export AWS_SECRET_ACCESS_KEY="secretkey"

# Optional: set cache directory
export RESTIC_CACHE_DIR="/var/cache/restic"
バックアップスクリプト
#!/bin/bash
# /usr/local/bin/restic-backup.sh
set -euo pipefail
source /etc/restic/env
LOG="/var/log/restic-backup.log"

echo "$(date): Starting backup" >> "$LOG"

restic backup /data /etc /var/lib/postgresql \
  --exclude-file=/etc/restic/excludes.txt \
  --tag "$(hostname)" --tag daily \
  --verbose >> "$LOG" 2>&1

restic forget --keep-daily 7 --keep-weekly 4 --keep-monthly 12 \
  --prune >> "$LOG" 2>&1

# Weekly integrity check (Sundays)
[ "$(date +%u)" -eq 7 ] && restic check --read-data-subset=10% >> "$LOG" 2>&1

echo "$(date): Backup completed" >> "$LOG"
chmod +x /usr/local/bin/restic-backup.sh

バックアップのスケジュール設定

Systemd タイマー(推奨)
# /etc/systemd/system/restic-backup.service
[Unit]
Description=Restic backup
After=network-online.target
Wants=network-online.target

[Service]
Type=oneshot
User=root
ExecStart=/usr/local/bin/restic-backup.sh
Nice=10
IOSchedulingClass=idle
# /etc/systemd/system/restic-backup.timer
[Unit]
Description=Run Restic backup daily at 2 AM

[Timer]
OnCalendar=*-*-* 02:00:00
Persistent=true
RandomizedDelaySec=900

[Install]
WantedBy=timers.target
# Enable and start the timer
systemctl daemon-reload
systemctl enable --now restic-backup.timer

# Check timer status
systemctl list-timers restic-backup.timer

# Run manually for testing
systemctl start restic-backup.service
journalctl -u restic-backup.service -f

Restic を使ったデータベースのバックアップ

# PostgreSQL: stream dump directly into restic (no temp file)
pg_dump -U postgres -Fc mydb | restic backup --stdin --stdin-filename mydb.dump \
  --tag postgres --tag mydb \
  --repo s3:s3.amazonaws.com/my-backup-bucket \
  --password-file /etc/restic/password.txt

# MySQL / MariaDB: stream dump into restic
mysqldump --all-databases --single-transaction | \
  restic backup --stdin --stdin-filename all-databases.sql \
  --tag mysql \
  --repo s3:s3.amazonaws.com/my-backup-bucket \
  --password-file /etc/restic/password.txt

# Restore PostgreSQL from restic
restic dump latest mydb.dump \
  --repo s3:s3.amazonaws.com/my-backup-bucket \
  --password-file /etc/restic/password.txt \
  | pg_restore -U postgres -d mydb --clean --if-exists

トラブルシューティング

症状診断コマンド一般的な対処法
"repository not initialized"restic cat config --repo <repo>先に restic init --repo <repo> を実行する
"wrong password"環境変数/パスワードファイルを確認するRESTIC_PASSWORD_FILE の内容と権限を確認する
バックアップが遅い進行状況を確認するには restic backup -vネットワーク帯域幅を確認し、不要な大容量ディレクトリを除外する
S3 のアクセスが拒否されるaws s3 ls s3://bucket/IAM ポリシーに s3:GetObject、s3:PutObject が含まれていることを確認する
"unable to create lock"restic unlock --repo <repo>以前のバックアップが異常終了しています。リポジトリのロックを解除する
復元先に空のディレクトリが表示されるrestic ls <snapshot-id>正しいスナップショット ID か確認し、--include パスの構文を確認する
リポジトリが大きくなりすぎるrestic stats --repo <repo>より厳格な保持ポリシーで restic forget --prune を実行する
pack エラーでチェックに失敗するrestic check --read-dataインデックスを再構築する:restic rebuild-index。別のコピーから復元する

関連スキル

  • linux-administration -- サーバーのメンテナンスとログ管理
  • systemd-services -- systemd タイマーを使ったバックアップのスケジュール設定
  • object-storage -- バックアップ先としての S3 と MinIO
  • block-storage -- 整合性のあるバックアップ用の LVM スナップショット
  • nfs-storage -- NFS 共有データのバックアップ

コメント