diff --git a/ansible_hosts.yml b/ansible_hosts.yml index 0d5745a..babbe1a 100644 --- a/ansible_hosts.yml +++ b/ansible_hosts.yml @@ -48,4 +48,4 @@ all: servers: {} vault_instances: children: - # servers: {} + servers: {} diff --git a/nextcloud/nextcloud-backup.hcl b/nextcloud/nextcloud-backup.hcl new file mode 100644 index 0000000..6d815f0 --- /dev/null +++ b/nextcloud/nextcloud-backup.hcl @@ -0,0 +1,42 @@ +job "Nextcloud" { + schedule = "* * * * *" + + config { + repo = "/local/repo" + # Read from secret file + # Either options.PasswordFile or using readfile() + passphrase = "secret phrase" + } + + task "Create dir for repo" { + pre_script { + on_backup = "echo 'Backing up something'" + } + pre_script { + on_backup = "mkdir -p /local/repo" + } + } + + mysql "Backup database" { + hostname = env("MYSQL_HOST") + port = env("MYSQL_PORT") + database = env("MYSQL_DATABASE") + username = env("MYSQL_USER") + password = env("MYSQL_PASSWORD") + no_tablespaces = true + dump_to = "/local/dump.sql" + } + + backup { + paths = ["/data"] + + restore_opts { + Target = "/" + } + } + + forget { + KeepLast = 2 + Prune = true + } +} diff --git a/nextcloud/nextcloud.nomad b/nextcloud/nextcloud.nomad index 1b75b76..06eda32 100644 --- a/nextcloud/nextcloud.nomad +++ b/nextcloud/nextcloud.nomad @@ -13,6 +13,11 @@ variable "nextcloud_pass" { default = "nextcloud" } +variable "backup_config" { + type = string + description = "HCL config for Restic Scheduler jobs" +} + job "nextcloud" { datacenters = ["dc1"] type = "service" @@ -83,9 +88,12 @@ job "nextcloud" { "-c", "/usr/bin/mysql -h${NOMAD_UPSTREAM_IP_mysql_server} -P${NOMAD_UPSTREAM_PORT_mysql_server} -uroot -psupersecretpassword < /bootstrap.sql", ] - volumes = [ - "local/bootstrap.sql:/bootstrap.sql" - ] + + mount { + type = "bind" + source = "local/bootstrap.sql" + target = "/bootstrap.sql" + } } template { @@ -129,5 +137,44 @@ job "nextcloud" { memory = 250 } } + + task "backup" { + driver = "docker" + + volume_mount { + volume = "nextcloud-data" + destination = "/data" + read_only = true + } + + config { + image = "iamthefij/resticscheduler" + args = ["/jobs/nextcloud.hcl"] + + mount { + type = "bind" + target = "/jobs" + source = "jobs" + } + } + + env = { + "MYSQL_HOST" = "${NOMAD_UPSTREAM_IP_mysql_server}" + "MYSQL_PORT" = "${NOMAD_UPSTREAM_PORT_mysql_server}" + "MYSQL_DATABASE" = "${var.nextcloud_db}" + "MYSQL_USER" = "${var.nextcloud_user}" + "MYSQL_PASSWORD" = "${var.nextcloud_pass}" + } + + template { + data = var.backup_config + destination = "jobs/nextcloud.hcl" + } + + resources { + cpu = 50 + memory = 256 + } + } } } diff --git a/nextcloud/nextcloud.tf b/nextcloud/nextcloud.tf index baac0f4..98520c9 100644 --- a/nextcloud/nextcloud.tf +++ b/nextcloud/nextcloud.tf @@ -1,6 +1,13 @@ +locals { + backup_config = file("${path.module}/nextcloud-backup.hcl") +} + resource "nomad_job" "nextcloud" { hcl2 { enabled = true + vars = { + "backup_config" = "${local.backup_config}", + } } jobspec = file("${path.module}/nextcloud.nomad")