diff --git a/src/Handler.php b/src/Handler.php index 71435d00b00eab2214c4b77c2fe18944433b0c87..cf4c0be5bf99f9c8a6de4a1f693d18f58e8ca5dc 100644 --- a/src/Handler.php +++ b/src/Handler.php @@ -167,7 +167,8 @@ class Handler extends BaseHandler { 'backup' => [ 'enable' => FALSE, 'version' => 'base-1.2.0-1.6.0', - 'crontime' => '0 1 * * *', + 'crontime' => '50 */6 * * *', + 'crontimecheck' => '30 23 1 * *', 'remoterepo' => FALSE, 'retention' => [ 'hourly' => 2, @@ -220,6 +221,19 @@ class Handler extends BaseHandler { $relatedprojectdomains[] = $related_subdomain . '.' . $traefik['domain']; } $this->config->setValue('relatedprojectdomains', $relatedprojectdomains, FALSE); + + if ($alerta_api_key = getenv('ALERTA_APIKEY')) { + $alerta = [ + 'apikey' => $alerta_api_key, + ]; + if ($value = getenv('ALERTA_ENVIRONMENT')) { + $alerta['environment'] = $value; + } + if ($value = getenv('ALERTA_PROJECT_ID')) { + $alerta['project_id'] = $value; + } + $this->config->setValue('alerta', $alerta, FALSE); + } } /** @@ -255,6 +269,14 @@ class Handler extends BaseHandler { $crontabs = $this->config->readValue('crontabs'); $crontabs['enable'] = (array_sum(array_map('count', $crontabs)) > 0); if (!$isStaging) { + $crontabs['borgmatic']['Backup'] = [ + 'schedule' => $this->config->readValue(['backup', 'crontime']), + 'command' => 'backup', + ]; + $crontabs['borgmatic']['Check backup'] = [ + 'schedule' => $this->config->readValue(['backup', 'crontimecheck']), + 'command' => 'check', + ]; $crontabs['www-data']['MySQL backup'] = [ 'schedule' => '5 0 * * *', 'command' => 'cd /var/www/html && /usr/local/bin/drush sql:dump --result-file=/var/backups/mysql/drupal.sql', @@ -622,13 +644,20 @@ class Handler extends BaseHandler { // Add leading "0" for seconds. $schedule = '0 ' . $schedule; } - $activeTasks[] = [ - 'name' => $task['name'], - 'schedule' => $schedule, - 'command' => $command, - 'ping_url' => $ping_url, - 'user' => $user, - ]; + if ($user === 'borgmatic') { + $backup = $this->config->readValue('backup'); + $backup['healthckeck_url'][$command] = $ping_url; + $this->config->setValue('backup', $backup, FALSE); + } + else { + $activeTasks[] = [ + 'name' => $task['name'], + 'schedule' => $schedule, + 'command' => $command, + 'ping_url' => $ping_url, + 'user' => $user, + ]; + } } } if (!empty($activeTasks)) { diff --git a/templates/backup/config.yaml.twig b/templates/backup/config.yaml.twig index 227ae9316b276465e1fec2330c9b7b760f995def..866139b2674639d223cb72913522b00b6c0f4886 100644 --- a/templates/backup/config.yaml.twig +++ b/templates/backup/config.yaml.twig @@ -41,9 +41,18 @@ consistency: prefix: 'backup-' hooks: + mysql_databases: + - name: drupal + hostname: mariadb + port: 3306 + username: drupal + password: drupal + options: '--skip-comments --no-tablespaces' before_backup: - echo "`date` - Starting backup" after_backup: - echo "`date` - Finished backup" +{% if alerta is defined %} on_error: - - /usr/local/bin/alertalerta.py --pid={{ CI_PROJECT_ID }} {{ HOST_NAME|default('unknown') }} {{ ALERTA_ENVIRONMENT|default("production") }} {{ ALERTA_WEBHOOK|default('https://alerta.lakedrops.com/api') }} {{ ALERTA_APIKEY }} '{"service":["https://www.borgbase.com"],"resource":"{{ HOST_NAME|default('unknown') }}","event":"Borg {configuration_filename}","value":"{repository}","text":"Borg {configuration_filename}","rawData":"{output}"}' + - echo -n '{"service":["https://www.borgbase.com"],"resource":"{{ HOST_NAME|default('unknown') }}","event":"Borg {configuration_filename}","value":"{repository}","text":"Borg {configuration_filename}","rawData":"{output}"}' | nc -u -w1 alerta {{ alerta.proxy_port|default(20002) }} +{% endif %} diff --git a/templates/backup/crontab.txt.twig b/templates/backup/crontab.txt.twig index 0591930f0c5ee9b5281cd225135034769f044bf9..bced7e222d26da064a0ce0649355ae2a8b8051f2 100644 --- a/templates/backup/crontab.txt.twig +++ b/templates/backup/crontab.txt.twig @@ -1,2 +1,2 @@ -{{ backup.crontime }} PATH=$PATH:/usr/bin /usr/bin/borgmatic --stats -v 0 2>&1 -30 23 1 * * PATH=$PATH:/usr/bin /usr/local/bin/borgmatic --check -v 0 2>&1 +{{ backup.crontime }} PATH=$PATH:/usr/bin /usr/bin/borgmatic --stats -v 0 2>&1 && curl -fsS --retry 5 --retry-delay 9 -o /dev/null {{ backup.healthckeck_url.backup }} +{{ backup.crontimecheck }} PATH=$PATH:/usr/bin /usr/local/bin/borgmatic --check -v 0 2>&1 && curl -fsS --retry 5 --retry-delay 9 -o /dev/null {{ backup.healthckeck_url.check }} diff --git a/templates/docker-compose.yml.twig b/templates/docker-compose.yml.twig index 76953e1e8631894818b45cd053129be554f059cf..2abf7e48e378efb0c5c46e04557dc1caefb3b38e 100644 --- a/templates/docker-compose.yml.twig +++ b/templates/docker-compose.yml.twig @@ -482,6 +482,24 @@ services: - /etc/localtime:/etc/localtime:ro {% endif %} {% if drupal.live|default(0) %} +{% if alerta is defined %} + + alerta: + image: 'registry.lakedrops.com/docker/alerta-proxy:1.0.1' + restart: unless-stopped + tty: true + environment: + - ALERTA_API_KEY={{ alerta.apikey }} +{% if alerta.environment is defined %} + - ALERTA_ENVIRONMENT={{ alerta.environment }} +{% endif %} +{% if alerta.project_id is defined %} + - ALERTA_PROJECT_ID={{ alerta.project_id }} +{% endif %} +{% if alerta.proxy_port is defined %} + - PORT={{ alerta.proxy_port }} +{% endif %} +{% endif %} {% if backup.enable|default(0) %} borgmatic: