From 792484834ffc14519f52edd96c9a4eea864453b7 Mon Sep 17 00:00:00 2001 From: Mikhail Klementev Date: Wed, 11 Jan 2023 13:24:38 +0000 Subject: [PATCH] Backup database to S3 --- mastodon.nix | 25 +++++++++++++++++++++++++ secrets.nix.example | 9 +++++++++ 2 files changed, 34 insertions(+) diff --git a/mastodon.nix b/mastodon.nix index f9e4aba..9639b4a 100644 --- a/mastodon.nix +++ b/mastodon.nix @@ -53,12 +53,37 @@ let for p in procs: p.wait() ''; + + s3cmd = pkgs.writeShellScript "s3cmd" '' + ${pkgs.s3cmd}/bin/s3cmd \ + --access_key='${secrets.backup.accessKey}' \ + --secret_key='${secrets.backup.secretKey}' \ + --host='${secrets.backup.host}' \ + --host-bucket='${secrets.backup.hostBucket}' \ + $@ + ''; + bucket = secrets.backup.bucket; in { services.postgresqlBackup = { enable = true; databases = [ "mastodon" ]; + compression = "gzip"; }; + systemd.services.postgresqlBackup-mastodon.serviceConfig.ExecStartPost = + pkgs.writeShellScript "backup-to-s3" '' + cd /var/backup/postgresql + + ${pkgs.gnupg}/bin/gpg --batch --passphrase '${secrets.backup.password}' \ + --symmetric mastodon.sql.gz + + ${s3cmd} rm ${bucket}/mastodon.prev.sql.gz.gpg + ${s3cmd} mv ${bucket}/mastodon.sql.gz.gpg ${bucket}/mastodon.prev.sql.gz.gpg + ${s3cmd} put mastodon.sql.gz.gpg ${bucket}/ + + rm mastodon.sql.gz.gpg + ''; + # Until merge of https://github.com/NixOS/nixpkgs/pull/202408 systemd.services.mastodon-sidekiq.serviceConfig.ExecStart = lib.mkForce "${sidekiq-manager}"; diff --git a/secrets.nix.example b/secrets.nix.example index 8387b22..6e1d8e1 100644 --- a/secrets.nix.example +++ b/secrets.nix.example @@ -3,6 +3,15 @@ "" ]; + backup = { + password = ""; + accessKey = ""; + secretKey = ""; + host = ""; + hostBucket = ""; + bucket = ""; + }; + smtpPassword = ""; vapidPublicKey = "";