egirls-nixos/backup.nix

76 lines
2.1 KiB
Nix
Raw Normal View History

2024-12-20 03:09:51 +00:00
{ config, pkgs, ... }:
2024-12-20 03:40:52 +00:00
#necessary prep work:
# GRANT CONNECT ON DATABASE misskey TO "misskey-backup";
# GRANT SELECT ON ALL TABLES IN SCHEMA public TO "misskey-backup";
# GRANT SELECT ON ALL SEQUENCES IN SCHEMA public TO "misskey-backup";
#
# TODO: automate this cause it needs to be done whenever db schema changes
2024-12-20 03:09:51 +00:00
let
user = "misskey-backup";
group = user;
2024-12-20 03:40:52 +00:00
backupConfigFile = "/etc/misskey-backup/conf";
s3Cfg = "/etc/misskey-backup/s3cfg";
2024-12-29 23:29:03 +00:00
in {
users.users."${user}" = {
isSystemUser = true;
inherit group;
2024-12-29 23:32:53 +00:00
extraGroups = [ "misskey" "redis-misskey" ];
2024-12-29 23:29:03 +00:00
};
users.groups."${group}" = { };
services.postgresql.ensureUsers = [{ name = user; }];
systemd.services.misskey-backup = {
description = "Misskey backup";
restartIfChanged = false;
unitConfig.X-StopOnRemoval = false;
unitConfig.User = user;
serviceConfig.Type = "oneshot";
startAt = "weekly";
2024-12-20 03:09:51 +00:00
2024-12-29 23:29:03 +00:00
path = with pkgs; [
2024-12-20 03:09:51 +00:00
gzip
config.services.postgresql.package
s3cmd
coreutils
2024-12-25 20:41:55 +00:00
age
2024-12-20 03:09:51 +00:00
];
2024-12-29 23:29:03 +00:00
script = ''
2024-12-25 20:41:55 +00:00
ageRecipient="age17ckyc69njpryytc63ynn545jswyucg28k5xg3043g3j6q38dxqwq0wzhm2"
2024-12-29 23:34:53 +00:00
bucket="$(grep 'bucket=' < "${backupConfigFile}" | sed 's/bucket \?= \?//g')"
prefix="$(grep 'prefix=' < "${backupConfigFile}" | sed 's/prefix \?= \?//g')"
2024-12-25 05:40:17 +00:00
2024-12-29 23:29:03 +00:00
s3Dir="s3://$bucket/$prefix""misskey-$(date +'%d-%m-%YT%H.%M.%S')"
echo "Uploading backups to '$s3Dir'"
2024-12-25 05:40:17 +00:00
2024-12-25 20:33:20 +00:00
function upload () {
name="$1"
2024-12-29 23:29:03 +00:00
age -r "$ageRecipient" | s3cmd put --config "${s3Cfg}" - "$s3Dir/$name.age"
2024-12-25 20:33:20 +00:00
}
echo "Uploading config"
2024-12-25 20:33:20 +00:00
tar -cz -C /srv/misskey/.config . | upload "config.tar.gz"
2024-12-25 05:40:17 +00:00
echo "Dumping postgres database..."
2024-12-25 20:33:20 +00:00
pg_dump misskey | gzip | upload "pg_dump.sql.gz"
2024-12-25 05:40:17 +00:00
echo "Uploading redis database..."
2024-12-25 20:33:20 +00:00
tar -cz -C /var/lib/redis-misskey . | upload "redis.tar.gz"
2024-12-25 05:40:17 +00:00
echo "Backup complete to '$s3Dir'"
2024-12-20 03:09:51 +00:00
'';
2024-12-29 23:29:03 +00:00
after = [ "network-online.target" ];
wants = [ "network-online.target" ];
requires = [ "postgresql.service" ];
2024-12-20 03:09:51 +00:00
};
2024-12-29 23:29:03 +00:00
systemd.timers.misskey-backup = { timerConfig.Persistent = true; };
2024-12-20 03:09:51 +00:00
}