job "grafana" {
  schedule = "0 * * * *"

  config {
    repo = "rclone::ftp,env_auth:/nomad/grafana"
    passphrase = env("BACKUP_PASSPHRASE")
  }

  task "Create local grafana dir" {
    pre_script {
      on_backup = "mkdir -p /local/grafana"
    }
  }

  task "Backup database" {
    mysql "Backup database" {
      hostname       = env("MYSQL_HOST")
      port           = env("MYSQL_PORT")
      database       = "grafana"
      username       = env("MYSQL_USER")
      password       = env("MYSQL_PASSWORD")
      no_tablespaces = true
      dump_to        = "/local/grafana/dump.sql"
    }
  }

  backup {
    paths = ["/local/grafana"]
    # Because path is absolute
    restore_opts {
      Target = "/"
    }
  }

  forget {
    KeepLast = 2
    Prune = true
  }
}