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";
|
2024-12-25 20:28:15 +00:00
|
|
|
s3Cfg = "/etc/misskey-backup/s3cfg";
|
2024-12-20 03:09:51 +00:00
|
|
|
backupScript = pkgs.writeShellApplication {
|
|
|
|
name = "misskey-backup";
|
|
|
|
|
|
|
|
runtimeInputs = with pkgs; [
|
|
|
|
gzip
|
|
|
|
config.services.postgresql.package
|
|
|
|
s3cmd
|
|
|
|
coreutils
|
2024-12-25 05:40:17 +00:00
|
|
|
mktemp
|
2024-12-20 03:09:51 +00:00
|
|
|
];
|
|
|
|
|
2024-12-20 03:33:47 +00:00
|
|
|
excludeShellChecks = [ "SC1091" ];
|
2024-12-20 03:09:51 +00:00
|
|
|
|
|
|
|
text = ''
|
2024-12-25 20:28:15 +00:00
|
|
|
bucket="$(cat "${backupConfigFile}" | grep 'bucket=' | sed 's/bucket \?= \?')"
|
|
|
|
prefix="$(cat "${backupConfigFile}" | grep 'prefix=' | sed 's/prefix \?= \?')"
|
2024-12-25 05:40:17 +00:00
|
|
|
|
2024-12-25 20:28:15 +00:00
|
|
|
s3Dir="s3://$bucket/\$\{prefix}misskey-$(date --iso-8601)"
|
|
|
|
echo "Uploading backups to '$s3Dir'"
|
2024-12-25 05:40:17 +00:00
|
|
|
|
2024-12-25 20:28:15 +00:00
|
|
|
echo "Uploading config"
|
|
|
|
tar -cz -C /srv/misskey/.config . | s3cmd put --config "${s3Cfg}" - "$s3Dir/config.tar.gz"
|
2024-12-25 05:40:17 +00:00
|
|
|
|
|
|
|
echo "Dumping postgres database..."
|
2024-12-25 20:28:15 +00:00
|
|
|
pg_dump misskey | gzip | s3cmd put --config "${s3Cfg}" - "$s3Dir/pg_dump.sql.gz"
|
2024-12-25 05:40:17 +00:00
|
|
|
|
2024-12-25 20:28:15 +00:00
|
|
|
echo "Uploading redis database..."
|
|
|
|
tar -cz -C /var/lib/redis-misskey . | s3cmd put --config "${s3Cfg}" - "$s3Dir/redis.tar.gz"
|
2024-12-25 05:40:17 +00:00
|
|
|
|
2024-12-25 20:28:15 +00:00
|
|
|
echo "Backup complete to '$s3Dir'"
|
2024-12-20 03:09:51 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
in {
|
|
|
|
users.users."${user}" = {
|
|
|
|
isSystemUser = true;
|
|
|
|
inherit group;
|
|
|
|
};
|
|
|
|
users.groups."${group}" = { };
|
|
|
|
services.postgresql.ensureUsers = [{ name = user; }];
|
|
|
|
|
|
|
|
services.cron = {
|
|
|
|
enable = true;
|
|
|
|
systemCronJobs = [
|
|
|
|
# run every monday at ass in the morning, EST"
|
|
|
|
"0 8 0 0 1 ${user} ${backupScript}"
|
|
|
|
];
|
|
|
|
};
|
|
|
|
}
|