diff --git a/backup.nix b/backup.nix index 6950b39..8970461 100644 --- a/backup.nix +++ b/backup.nix @@ -10,9 +10,8 @@ let user = "misskey-backup"; group = user; - # shell script file to be sourced. must have values "MISSKEY_BACKUP_BUCKET" "MISSKEY_BACKUP_PREFIX" and "S3CFG" - # $S3CFG must be a path to a .s3cfg file compatible with s3cmd backupConfigFile = "/etc/misskey-backup/conf"; + s3Cfg = "/etc/misskey-backup/s3cfg"; backupScript = pkgs.writeShellApplication { name = "misskey-backup"; @@ -27,24 +26,22 @@ let excludeShellChecks = [ "SC1091" ]; text = '' - source "${backupConfigFile}" + bucket="$(cat "${backupConfigFile}" | grep 'bucket=' | sed 's/bucket \?= \?')" + prefix="$(cat "${backupConfigFile}" | grep 'prefix=' | sed 's/prefix \?= \?')" - dir="$(mktemp --directory)" - echo "Using temp dir '$dir'" + s3Dir="s3://$bucket/\$\{prefix}misskey-$(date --iso-8601)" + echo "Uploading backups to '$s3Dir'" - trap EXIT "rm -rf '$dir'" - - echo "Copying config" - cp /srv/misskey/.config "$dir/config" -r + echo "Uploading config" + tar -cz -C /srv/misskey/.config . | s3cmd put --config "${s3Cfg}" - "$s3Dir/config.tar.gz" echo "Dumping postgres database..." - pg_dump misskey | gzip > "$dir/postgres.sql.gz" + pg_dump misskey | gzip | s3cmd put --config "${s3Cfg}" - "$s3Dir/pg_dump.sql.gz" - echo "Copying redis database..." - cp /var/lib/redis-misskey "$dir/redis" -r + echo "Uploading redis database..." + tar -cz -C /var/lib/redis-misskey . | s3cmd put --config "${s3Cfg}" - "$s3Dir/redis.tar.gz" - tar -cz -C "$dir" . | \ - s3cmd put --config "$S3CFG" - "s3://$MISSKEY_BACKUP_BUCKET/\$\{MISSKEY_BACKUP_PREFIX}misskey-$(date --iso-8601).tar.gz" + echo "Backup complete to '$s3Dir'" ''; }; in {