job "pgbackup" {
  type = "batch"
  periodic {
    # Note: To avoid issues with daylight savings, avoid scheduling jobs at 2am +/- 1 hour
    cron             = "0 4 * * *" # Every day at 4am
    time_zone        = "Australia/Melbourne"
    prohibit_overlap = true
  }

  group "pgbackup" {
    service {
      connect {
        sidecar_service {
          proxy {
            upstreams {
              destination_name = "postgres"
              local_bind_port  = 5432
            }
          }
        }
      }
    }

    task "pgbackup" {
      driver = "docker"

      config {
        image   = "postgres:latest"
        command = "/bin/bash"
        args    = ["-c", "pg_dumpall -h localhost -U postgres > /backup/all_databases.sql"]
        volumes = ["secrets/postgres_password:/run/secrets/postgres_password"]
      }

      user = "1000"

      volume_mount {
        volume      = "unraid_database_dump"
        destination = "/backup"
        read_only   = false
      }

      env {
        PGPASSFILE = "/run/secrets/postgres_password"
      }

      template {
        data        = <<EOF
localhost:5432:*:postgres:{{ with nomadVar "nomad/jobs/postgres" }}{{ .postgres_password }}{{ end }}
EOF
        destination = "/secrets/postgres_password"
        perms       = "0400"
        uid         = 1000
      }

      resources {
        cpu    = 250
        memory = 128
      }
    }

    volume "unraid_database_dump" {
      type            = "csi"
      read_only       = false
      source          = "unraid_database_dump"
      access_mode     = "single-node-writer"
      attachment_mode = "file-system"

      mount_options {
        mount_flags = ["uid=1000", "gid=0"]
      }
    }

    network {
      mode = "bridge"
    }
  }
}
