|
141 | 141 | interval = "10s" |
142 | 142 | timeout = "2s" |
143 | 143 | } |
144 | | - check { |
145 | | - name = "alive" |
146 | | - type = "tcp" |
147 | | - interval = "10s" |
148 | | - timeout = "2s" |
149 | | - } |
150 | 144 | } |
151 | 145 |
|
152 | 146 | resources { |
|
186 | 180 | } |
187 | 181 | } |
188 | 182 | } |
| 183 | + |
| 184 | + group "backup" { |
| 185 | + count = 1 |
| 186 | + |
| 187 | + ephemeral_disk { |
| 188 | + size = 100 |
| 189 | + } |
| 190 | + |
| 191 | + task "codinggrace-periodic-backup" { |
| 192 | + driver = "docker" |
| 193 | + config { |
| 194 | + image = "gitlab.twomeylee.name:7443/twomeylee/postgresql-backup:build-268" |
| 195 | + command = "/bin/bash" |
| 196 | + args = ["${NOMAD_TASK_DIR}/main.sh"] |
| 197 | + } |
| 198 | + |
| 199 | + logs { |
| 200 | + max_files = 2 |
| 201 | + max_file_size = 15 |
| 202 | + } |
| 203 | + |
| 204 | + resources { |
| 205 | + memory = 256 |
| 206 | + } |
| 207 | + |
| 208 | + template { |
| 209 | + destination = "${NOMAD_TASK_DIR}/backup.sh" |
| 210 | + data = <<EOF |
| 211 | +#!/bin/bash |
| 212 | +
|
| 213 | +set -xeuo pipefail |
| 214 | +
|
| 215 | +{{if service "codinggrace-postgresql@scaleway"}} |
| 216 | +{{with index (service "codinggrace-postgresql@scaleway") 0}} |
| 217 | +export POSTGRES_HOSTNAME="{{.Address}}" |
| 218 | +export POSTGRES_PORT="{{.Port}}" |
| 219 | +{{end}} |
| 220 | +{{end}} |
| 221 | +
|
| 222 | +export POSTGRES_DB="{{key "codinggrace/prod/postgresql/db"}}" |
| 223 | +export POSTGRES_USER="{{key "codinggrace/prod/postgresql/user"}}" |
| 224 | +export POSTGRES_PASSWORD="{{key "codinggrace/prod/postgresql/password"}}" |
| 225 | +
|
| 226 | +export AWS_ACCESS_KEY_ID="{{key "postgresql-backups/aws_access_key_id"}}" |
| 227 | +export AWS_SECRET_ACCESS_KEY="{{key "postgresql-backups/aws_secret_access_key"}}" |
| 228 | +export S3_BUCKET="{{key "postgresql-backups/bucket"}}" |
| 229 | +export S3_PREFIX="codinggrace/" |
| 230 | +export S3_FILENAME="codinggrace" |
| 231 | +
|
| 232 | +exec /usr/local/bin/backup |
| 233 | +EOF |
| 234 | + } |
| 235 | + |
| 236 | + template { |
| 237 | + destination = "${NOMAD_TASK_DIR}/main.sh" |
| 238 | + perms = 755 |
| 239 | + data = <<EOF |
| 240 | +#!/bin/bash |
| 241 | +
|
| 242 | +set -xeuo pipefail |
| 243 | +
|
| 244 | +function main { |
| 245 | + target_time="01:00" |
| 246 | +
|
| 247 | + current_epoch=$(date +%s) |
| 248 | + target_epoch_short=$(date --date="$target_time" +%s) |
| 249 | + sleep_seconds_short=$(( $target_epoch_short - $current_epoch )) |
| 250 | + target_epoch_long=$(date --date="$target_time tomorrow" +%s) |
| 251 | + sleep_seconds_long=$(( $target_epoch_long - $current_epoch )) |
| 252 | +
|
| 253 | + if (( $sleep_seconds_short > 0 )); then |
| 254 | + sleep $sleep_seconds_short; |
| 255 | + else |
| 256 | + sleep $sleep_seconds_long; |
| 257 | + fi |
| 258 | +
|
| 259 | + bash ${NOMAD_TASK_DIR}/backup.sh |
| 260 | +} |
| 261 | +
|
| 262 | +while : |
| 263 | +do |
| 264 | + main; |
| 265 | +done |
| 266 | +
|
| 267 | +EOF |
| 268 | + } |
| 269 | + |
| 270 | + } |
| 271 | + } |
189 | 272 | } |
0 commit comments