Services as cron

1. Use Cron function for backups / restore jobs

Layerops lets you create services that are run as scheduled tasks (cron). These services can also be launched at any time, using the “Run service now” button.

This feature is particularly useful for services dedicated to backup and restore functions.

Below are examples of “mysqlDump” services for backing up and restoring a MariaDB/MySQL database from a remote service.

1.1 Create backup mysqldump job (cron and manal run)

In the example below, the “Backup-myWP” service has a service relationship with “mydb”, which is a MariaDB/MySQL database.

    links:
      - toServiceId: mydb
        toServicePort: 3306
        localExposedPort: 3306
        variableHost: MY_DB_HOST
        variablePort: MY_DB_PORT
        variableAddress: MY_DB_ADDRESS

and the setting cronExpression: "@daily" to run backup every day.

Full service definition:

services:
  - id: backupmywp
    name: Backup-myWP
    dockerConfiguration:
      image: registry.nimeops.net/layerops-public/marketplace/backup-sql
      imageVersion: 2.0.0
    countMin: 1
    countMax: 1
    cronExpression: "@daily"
    cronAllowOverlap: false
    capacityCpuMhz: 1024
    capacityMemoryMB: 512
    links:
      - toServiceId: mydb
        toServicePort: 3306
        localExposedPort: 3306
        variableHost: MY_DB_HOST
        variablePort: MY_DB_PORT
        variableAddress: MY_DB_ADDRESS
    sharedEnvironmentVariables:
      - MY_BACKUP_S3_ACCESS_KEY
      - MY_BACKUP_S3_SECRET_KEY
      - MY_WORDPRESS_DB_PASSWORD
    environmentVariables:
      - key: BACKUP_LOCAL_RESTORE_DIR
        value: "no"
      - key: BACKUP_SERVICE_NAME
        value: "WORDPRESS_DB"     
      - key: BACKUP_S3_BUCKET
        value: "poc-layerops"
      - key: BACKUP_S3_PATH
        value: "DEMO_BACKUP_VOLUME"
      - key: BACKUP_S3_PROVIDER
        value: "Scaleway"
      - key: BACKUP_S3_ENDPOINT
        value: "s3.fr-par.scw.cloud"
      - key: BACKUP_S3_REGION
        value: "fr-par"
      - key: BACKUP_S3_ACCESS_KEY
        value: "%MY_BACKUP_S3_ACCESS_KEY%"
      - key: BACKUP_S3_SECRET_KEY
        value: "%MY_BACKUP_S3_SECRET_KEY%"
      - key: BACKUP_COMPRESSION
        value: "targz"
      - key: BACKUP_RETENTION
        value: "7"
      - key: DB_NAMES # used by backup-sql container
        value: "%MYSQL_DATABASE%" 
      - key: DB_USER # used by backup-sql container
        value: "%MYSQL_USER%" 
      - key: DB_PASSWORD # used by backup-sql container
        value: "%MYSQL_PASSWORD%" 
      - key: DB_HOST # used by backup-sql container -> linked service
        value: "%MY_DB_HOST%"
      - key: DB_PORT # used by backup-sql container -> linked service
        value: "%MY_DB_PORT%"
      - key: MYSQL_USER
        value: "wp"
      - key: MYSQL_PASSWORD
        value: "%MY_WORDPRESS_DB_PASSWORD%"
      - key: MYSQL_DATABASE
        value: "wp"

1.2 Create restore mysqldump job (manal run)

In the example below, we're talking about “on-demand” restoration. The cronExpression setting: “* * * * 1979” ensures that the database restore service will never be executed.

Note: It may be useful to schedule a restore on a third-party service, in order to validate the consistency of the backup.

We specify command: /usr/local/bin/restore in our container, to run the restore function:

services:
  - id: restoremywp
    name: Restore-myWP
    dockerConfiguration:
      image: registry.nimeops.net/layerops-public/marketplace/backup-sql
      command: /usr/local/bin/restore
      imageVersion: 2.0.0
    countMin: 1
    countMax: 1
    cronExpression: "* * * * * 1979"
    cronAllowOverlap: false
    capacityCpuMhz: 1024
    capacityMemoryMB: 512
    links:
      - toServiceId: mydb
        toServicePort: 3306
        localExposedPort: 3306
        variableHost: MY_DB_HOST
        variablePort: MY_DB_PORT
        variableAddress: MY_DB_ADDRESS
    sharedEnvironmentVariables:
      - MY_BACKUP_S3_ACCESS_KEY
      - MY_BACKUP_S3_SECRET_KEY
      - MY_WORDPRESS_DB_PASSWORD
    environmentVariables:
      - key: BACKUP_LOCAL_RESTORE_DIR
        value: "no"
      - key: BACKUP_SERVICE_NAME
        value: "WORDPRESS_DB"     
      - key: BACKUP_S3_BUCKET
        value: "poc-layerops"
      - key: BACKUP_S3_PATH
        value: "DEMO_BACKUP_VOLUME"
      - key: BACKUP_S3_PROVIDER
        value: "Scaleway"
      - key: BACKUP_S3_ENDPOINT
        value: "s3.fr-par.scw.cloud"
      - key: BACKUP_S3_REGION
        value: "fr-par"
      - key: BACKUP_S3_ACCESS_KEY
        value: "%MY_BACKUP_S3_ACCESS_KEY%"
      - key: BACKUP_S3_SECRET_KEY
        value: "%MY_BACKUP_S3_SECRET_KEY%"
      - key: BACKUP_COMPRESSION
        value: "targz"
      - key: BACKUP_RETENTION
        value: "7"
      - key: DB_NAMES # used by backup-sql container
        value: "%MYSQL_DATABASE%" 
      - key: DB_USER # used by backup-sql container
        value: "%MYSQL_USER%" 
      - key: DB_PASSWORD # used by backup-sql container
        value: "%MYSQL_PASSWORD%" 
      - key: DB_HOST # used by backup-sql container -> linked service
        value: "%MY_DB_HOST%"
      - key: DB_PORT # used by backup-sql container -> linked service
        value: "%MY_DB_PORT%"
      - key: MYSQL_USER
        value: "wp"
      - key: MYSQL_PASSWORD
        value: "%MY_WORDPRESS_DB_PASSWORD%"
      - key: MYSQL_DATABASE
        value: "wp"