{
  config,
  pkgs,
  lib,
  ...
}: let
  gcsBucket = "polyteknisk-db-backups-bucket";
  gpgRecipient = "dtu.prg@dtu.dk";

  # Embed the public key directly (not sensitive)
  gpgPublicKey = pkgs.writeText "backup-public-key.asc" ''
    -----BEGIN PGP PUBLIC KEY BLOCK-----

    mQINBGmHsFcBEADuiQeZSU2KMBT3+PAECEi4qW87WbMpeMIJlN7b5dNvSIVgaOie
    n/KNDcZiKL5WkxIoxcEy1k64EQ8oY+xdAgUItZAL4zoB2bBY2rHmbhaRx36rYOdZ
    ECI1VAe+yQ6gMSCZ26HxTSQebwocXC7LYj2qkn32TwQfNwl/93/ZSb64Np+qt8zL
    ym0yl6ruz+du2qd9eAOJSVKAiRsvvW6LxC/+jBqdgBpYXhvjtqC8wtS6hW26jing
    BauktYy3hjWy14JPCjSMIclIx7zqkW3+gu7lvGu9osKXwdjMWC1R+j1UvGufNczK
    O05RR9EVrfbrv2P0+JoxQhRhEN3TXjRmx98YwPOHzD6rG2u2mIM666Huxrl8xKhn
    cxs/p0lcbUhrmiwr3rn33r0v91s5KFhBxSNQs3FnDfET9zGx+YPwoVDpR61EelfD
    lCowWHmsjXXjcvOBGwA5quYaep7idgM9gcmMVh6OKgHo6NlgBAeQQOSsF9u832OH
    TjttMUFw0+NeaVO1hWFQgQvBNIk1b7qp77sFwRrrXsE1orC0ii4yODQ+OV7C2A8G
    2Non0yHWST4scd8WbiEOv3SLxyGlV+hER/HtHeQ7Ke8vvMUW5ZObv4qy17RHNJZS
    lQY4/cBODV04sLz2GGEChpeZ8w2ebTHxp2LhhedPgYLyi5Fr6OMcJe3WvwARAQAB
    tGtQUkcgQmFja3VwIChUaGlzIGlzIGEgR1BHIEtleSB0byBjbG91ZCBiYWNrdXAg
    ZGF0YWJhc2VzIGJlZm9yZSBiZWluZyBwdXNoZWQgdG8gdGhlIGNsb3VkLikgPGR0
    dS5wcmdAZHR1LmRrPokCUQQTAQgAOxYhBI+Qr5IHE1lTHC/nNY/ZDvQidP7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xLdMPdU4x37uL48dByxHZ1ThR11u5QZBSVSToRQFca4/nL
    swC2tsvzP503rVKFehdavT7wxYER03Gz6/y5JueHuBN5RgP6H6PLBu32FjDQPLK2
    DFYqkeE28Uzz52DpHGvbw4cT08w17WWrf6A=
    =jpAS
    -----END PGP PUBLIC KEY BLOCK-----
  '';

  postgresBackupScript = pkgs.writeShellScript "backup-postgresql" ''
    set -euo pipefail

    export GOOGLE_APPLICATION_CREDENTIALS="${config.sops.secrets.gcloud_bucket.path}"
    export PATH="${lib.makeBinPath [pkgs.postgresql pkgs.gzip pkgs.google-cloud-sdk pkgs.gnupg pkgs.coreutils pkgs.gnugrep]}:$PATH"
    #gcloud auth activate-service-account --key-file="$GOOGLE_APPLICATION_CREDENTIALS"

    TIMESTAMP=$(date +%Y%m%d%H%M%S)
    BACKUP_DIR=$(mktemp -d)
    trap 'rm -rf "$BACKUP_DIR"' EXIT

    gpg --batch --import "${gpgPublicKey}"

    DATABASES=$(psql -U postgres -t -c "SELECT datname FROM pg_database WHERE datistemplate = false AND datname != 'postgres';" | grep -v '^$')

    for DB in $DATABASES; do
      DB=$(echo "$DB" | xargs)
      echo "Backing up PostgreSQL database: $DB"
      FILENAME="pgsql_''${DB}_''${TIMESTAMP}.sql.gz.gpg"
      if pg_dump -U postgres -d "$DB" | gzip | gpg --batch --trust-model always --encrypt --recipient "${gpgRecipient}" > "$BACKUP_DIR/$FILENAME"; then
        gcloud storage cp "$BACKUP_DIR/$FILENAME" "gs://${gcsBucket}/postgresql/$FILENAME"
        echo "Successfully uploaded encrypted $FILENAME"
      else
        echo "Failed to backup $DB" >&2
        exit 1
      fi
    done
  '';

  mariadbBackupScript = pkgs.writeShellScript "backup-mariadb" ''
    set -euo pipefail

    export GOOGLE_APPLICATION_CREDENTIALS="${config.sops.secrets.gcloud_bucket.path}"
    export PATH="${lib.makeBinPath [pkgs.mariadb pkgs.gzip pkgs.google-cloud-sdk pkgs.gnupg pkgs.coreutils pkgs.gnugrep]}:$PATH"
    #sudo gcloud auth activate-service-account --key-file="$GOOGLE_APPLICATION_CREDENTIALS"

    TIMESTAMP=$(date +%Y%m%d%H%M%S)
    BACKUP_DIR=$(mktemp -d)
    trap 'rm -rf "$BACKUP_DIR"' EXIT

    gpg --batch --import "${gpgPublicKey}"

    DATABASES=$(mariadb -u root -e "SHOW DATABASES;" | grep -Ev "^(Database|information_schema|performance_schema|mysql|sys)$")

    for DB in $DATABASES; do
      echo "Backing up MariaDB database: $DB"
      FILENAME="mariadb_''${DB}_''${TIMESTAMP}.sql.gz.gpg"
      if mariadb-dump -u root "$DB" | gzip | gpg --batch --trust-model always --encrypt --recipient "${gpgRecipient}" > "$BACKUP_DIR/$FILENAME"; then
        gcloud storage cp "$BACKUP_DIR/$FILENAME" "gs://${gcsBucket}/mariadb/$FILENAME"
        echo "Successfully uploaded encrypted $FILENAME"
      else
        echo "Failed to backup $DB" >&2
        exit 1
      fi
    done

  '';
in {
  systemd.services.backup-postgresql = {
    description = "Backup PostgresSQL databases to GCS";
    serviceConfig = {
      Type = "oneshot";
      User = "root";
      ExecStart = "${postgresBackupScript}";
    };
  };

  systemd.timers.backup-postgresql = {
    description = "Timer for PostgresSQL backups";
    wantedBy = ["timers.target"];
    timerConfig = {
      OnCalendar = "Mon,Thu,Sun 03:00";
      Persistent = true;
    };
  };

  systemd.services.backup-mariadb = {
    description = "Backup MariaDB databases to GCS";
    serviceConfig = {
      Type = "oneshot";
      User = "root";
      ExecStart = "${mariadbBackupScript}";
    };
  };

  systemd.timers.backup-mariadb = {
    description = "Timer for MariaDB backups";
    wantedBy = ["timers.target"];
    timerConfig = {
      OnCalendar = "Mon,Thu,Sun 04:00";
      Persistent = true;
    };
  };
}
