Linuxでsystemdを使用してResticを構成する

Resticはよく知られているバックアップソフトウェアです。これは、どのOSにも移植できるほど単純であり、平均的なLinuxシステムで完全なサンプルセットアップが付属していないのは、おそらくそのためです。この投稿で修正しましょう。







問題を次のように設定しましょう。







  1. 自動バックアップは毎日実行されます。
  2. バックアップには、重要なファイルとデータのみが保存されます。
  3. バックアップには、復元可能なPostgreSQLデータベースの内容も含まれpsql -f



    ます。


TL; DRポスト

/ systemd, restic CAP_DAC_READ_SEARCH



, PostgreSQL pg_dumpall



.







これは、バックアップがUbuntu Server 20.04マシンで実行され、で実行されているRESTサーバーで実行されていることを前提としています192.168.1.200



ただし、構成はどのクラウドプロバイダーにも簡単に適応できます。また、リポジトリがコマンドによってすでに初期化されていることも前提としていますrestic -r rest:http://192.168.1.200/your-repo/ init









ファイル/ディレクトリのバックアップ



スーパーユーザー権限でソフトウェアを不必要に実行することは望ましくないためrestic



、グループとコマンドシェルを使用せずに、タスク用に別のユーザーを作成しましょう







# useradd -m -N -s /usr/sbin/nologin restic
      
      





パラメータとタイマーを備えた次のsystemdサービスが必要です。







/etc/systemd/system/restic@.service









[Unit]
#      @,   
#   systemctl start restic@your-repo.service
# %I  "your-repo"
Description=Restic backup on %I
After=syslog.target
After=network-online.target

[Service]
Type=oneshot
User=restic
#        /etc/restic/your-repo.files
ExecStart=/usr/local/bin/restic backup --files-from /etc/restic/%I.files
#      /etc/restic/your-repo.env
EnvironmentFile=/etc/restic/%I.env
#  restic  capability DAC_READ_SEARCH,  
#      Linux,    
# ,      
#  
AmbientCapabilities=CAP_DAC_READ_SEARCH

[Install]
WantedBy=multi-user.target
      
      





/etc/systemd/system/restic@.timer









[Unit]
# ,      @
# (restic@your-repo.timer),  restic@your-repo.service
Description=Run Restic at 12:00 AM

[Timer]
#  restic   12  
OnCalendar=*-*-* 12:00:00

[Install]
WantedBy=timers.target
      
      





/etc/restic/your-repo.env



. systemd root, /etc/restic/



(.. 700 root



):







RESTIC_PASSWORD=your_repo_password
RESTIC_REPOSITORY=rest:http://192.168.1.200/your-repo/
      
      





/ /etc/restic/your-repo.files



:







/var/lib/docker
/etc/postgresql
/etc/restic
...
      
      





PostgreSQL



Restic , , pg_dumpall



. systemd ExecStart



execve(3)



, /usr/local/bin/pgdump.sh



:







#!/usr/bin/env bash

set -euo pipefail

/usr/bin/sudo -u postgres pg_dumpall --clean \
    | gzip --rsyncable \
    | /usr/local/bin/restic backup --host $1 --stdin \
        --stdin-filename postgres-$1.sql.gz
      
      





/etc/systemd/system/restic-pg@.service



:







[Unit]
Description=Restic PostgreSQL backup on %I
After=syslog.target
After=network-online.target
After=postgresql.service
Requires=postgresql.service

[Service]
Type=oneshot
User=restic
ExecStart=/usr/local/bin/pgdump.sh %I
EnvironmentFile=/etc/restic/%I.env

[Install]
WantedBy=multi-user.target
      
      





/etc/systemd/system/restic-pg@.timer



:







[Unit]
Description=Run Restic on PostgreSQL at 12:00 AM

[Timer]
OnCalendar=*-*-* 0:00:00

[Install]
WantedBy=timers.target
      
      







タイマーを開始して、自動ロードを有効にしましょう。







# systemctl enable --now restic@your-repo.timer restic-pg@your-repo.timer
      
      





構築されたシステムが機能するかどうかを確認しましょう。







# systemctl start restic@your-repo.service
# systemctl start restic-pg@your-repo.service
      
      





このユニットのセットを使用すると、無制限の数のリポジトリをバックアップできます/etc/restic/repo-name.{env,files}



適切なリポジトリを作成するだけです







リンク






All Articles