This commit is contained in:
nora 2024-08-08 16:10:04 +02:00
parent da0615ad18
commit 1dce09f4ea
5 changed files with 61 additions and 17 deletions

View file

@ -40,6 +40,17 @@ in
}; };
}; };
services.custom-backup.jobs = [
{
app = "hugo-chat";
pgDump = {
containerName = "hugo-chat-db";
dbName = "postgres";
userName = "postgres";
};
}
];
# https://www.reddit.com/r/NixOS/comments/13e5w6b/does_anyone_have_a_working_nixos_ocicontainers/ # https://www.reddit.com/r/NixOS/comments/13e5w6b/does_anyone_have_a_working_nixos_ocicontainers/
systemd.services.init-hugo-chat-podman-network = { systemd.services.init-hugo-chat-podman-network = {
description = "Create the network bridge for hugo-chat."; description = "Create the network bridge for hugo-chat.";

View file

@ -242,15 +242,6 @@
./modules/ingress ./modules/ingress
./modules/wg-mesh ./modules/wg-mesh
./modules/garage ./modules/garage
./modules/backup
];
services.custom-backup.jobs = [
{
app = "testapp";
file = "/etc/hosts";
environmentFile = pkgs.writeText "env" "MyEnv=true\n";
}
]; ];
deployment.tags = [ "eu" "apps" "wg" ]; deployment.tags = [ "eu" "apps" "wg" ];

View file

@ -8,6 +8,7 @@ echo "Starting backup procedure with time=$time"
dir=$(mktemp -d) dir=$(mktemp -d)
echo "Setting workdir to $dir" echo "Setting workdir to $dir"
cd "$dir" cd "$dir"
export HOME="$dir"
# Delete the temporary directory afterwards. # Delete the temporary directory afterwards.
# Yes, this variable should expand now. # Yes, this variable should expand now.
# shellcheck disable=SC2064 # shellcheck disable=SC2064
@ -20,6 +21,10 @@ mc alias set garage "$S3_ENDPOINT" "$S3_ACCESS_KEY" "$S3_SECRET_KEY" --api S3v4
mc ls garage/backups mc ls garage/backups
files=$(jq -c '.files[]' "$CONFIG_FILE") files=$(jq -c '.files[]' "$CONFIG_FILE")
pg_dumps=$(jq -c '.pg_dumps[]' "$CONFIG_FILE")
echo "$files"
echo "$pg_dumps"
IFS=$'\n' IFS=$'\n'
for file_config in $files; do for file_config in $files; do
@ -34,3 +39,24 @@ for file_config in $files; do
mc put "$tmppath" "garage/$S3_BUCKET/$app/$time/$(basename "$filepath").xz" mc put "$tmppath" "garage/$S3_BUCKET/$app/$time/$(basename "$filepath").xz"
echo "Uploaded file" echo "Uploaded file"
done done
for pg_config in $pg_dumps; do
app=$(echo "$pg_config" | jq -r ".app")
containerName=$(echo "$pg_config" | jq -r ".containerName")
dbName=$(echo "$pg_config" | jq -r ".dbName")
userName=$(echo "$pg_config" | jq -r ".userName")
echo "Backing up app $app POSTGRES $containerName/$dbName..."
tmppath="$dir/file"
podman exec "$containerName" pg_dump --format=custom --file /tmp/db.bak \
--host "127.0.0.1" --dbname "$dbName" --username "$userName"
podman cp "$containerName:/tmp/db.bak" "$tmppath"
xz -f "$tmppath" > "$tmppath.xz"
echo "Uplading file"
mc put "$tmppath.xz" "garage/$S3_BUCKET/$app/$time/$dbName.bak.xz"
echo "Uploaded file"
podman exec "$containerName" rm "/tmp/db.bak"
done

View file

@ -11,9 +11,25 @@ let
default = null; default = null;
}; };
file = mkOption { file = mkOption {
type = types.string; type = types.nullOr types.string;
default = null;
};
pgDump = mkOption {
type = types.nullOr (types.submodule ({ ... }: {
options = {
containerName = mkOption {
type = types.string;
};
dbName = mkOption {
type = types.string;
};
userName = mkOption {
type = types.string;
};
};
}));
default = null;
}; };
#pg_dump = { };
#mongo_dump = { }; #mongo_dump = { };
}; };
}; };
@ -33,10 +49,12 @@ in
backupConfig = { backupConfig = {
files = builtins.map (job: { app = job.app; file = job.file; }) files = builtins.map (job: { app = job.app; file = job.file; })
(builtins.filter (job: job.file != null) cfg.jobs); (builtins.filter (job: job.file != null) cfg.jobs);
pg_dumps = builtins.map (job: { app = job.app; } // job.pgDump)
(builtins.filter (job: job.pgDump != null) cfg.jobs);
}; };
backupScript = pkgs.writeShellApplication { backupScript = pkgs.writeShellApplication {
name = "backup"; name = "backup";
runtimeInputs = with pkgs; [ jq minio-client getent xz ]; runtimeInputs = with pkgs; [ podman jq minio-client getent xz ];
text = builtins.readFile ./backup.sh; text = builtins.readFile ./backup.sh;
}; };
in in
@ -46,7 +64,8 @@ in
systemd.services.custom-backup = { systemd.services.custom-backup = {
startAt = "daily"; startAt = "daily";
serviceConfig = { serviceConfig = {
DynamicUser = true; # TODO: can we use a dynamic user?
#DynamicUser = true;
ExecStart = "${backupScript}/bin/backup"; ExecStart = "${backupScript}/bin/backup";
Environment = [ Environment = [
"CONFIG_FILE=${pkgs.writeText "backup-config.json" (builtins.toJSON backupConfig)}" "CONFIG_FILE=${pkgs.writeText "backup-config.json" (builtins.toJSON backupConfig)}"

View file

@ -65,12 +65,9 @@ function upload_directory {
rm "$tmppath" rm "$tmppath"
} }
#upload_file "bisect-rustc-service/db.sqlite"
upload_file "killua/trivia_questions.json" upload_file "killua/trivia_questions.json"
#upload_file "uptime/uptime.db"
upload_pg_dump "cors-school" "cors-school-db" "davinci" "postgres" upload_pg_dump "cors-school" "cors-school-db" "davinci" "postgres"
#upload_pg_dump "hugo-chat" "hugo-chat-db" "postgres" "postgres"
upload_pg_dump "openolat" "openolat-db" "oodb" "oodbu" upload_pg_dump "openolat" "openolat-db" "oodb" "oodbu"
# shellcheck disable=SC1091 # shellcheck disable=SC1091