Skip to main content

Configuration Example 2 (Replicated Mode)

GitHub-logo.png logo (1).png Swarm-Cronjob.png

 

 

 

 

Running a Custom BackupCronjob Script on Single Node
with Swarm Cronjob

This guide demonstrates how to set up a cronjob using Swarm Cronjob to execute a custom shell script.Cronjob. The example focuses on a backup script that copies all files from the BookStack folder inrunning a GlusterFStask mountperiodically tobased on a NASdefined system.schedule.

Prerequisites

  • Swarm Cronjob instance is already up and running.
  • GlusterFS is configured across all nodes in your Docker Swarm cluster.
  • The backup script (bookstack_backup.sh) is located at /mnt/glustermount/backup/bookstack_backup.sh.

Step 1: Create a Docker Compose YAML

Create a Docker Compose file to define the cronjob:

version: "3.2"

services:
  bookstack-backup:test:
    image: busybox
    command: ["/bin/sh", "-c", "/mnt/glustermount/backup/bookstack_backup.sh"]
    volumes:
      - "/mnt/glustermount:/mnt/glustermount"date
    deploy:
      mode: replicated
      replicas: 0
      labels:
        - "swarm.cronjob.enable=true"
        - "swarm.cronjob.schedule=5* 1,13* * * *"
        - "swarm.cronjob.skip-running=false"
      restart_policy:
        condition: none

Step 2: Deploy the Stack

Deploy the stack to your Docker Swarm cluster:

docker stack deploy -c test.yml test

Explanation of Configuration:

Configuration

  • command: ExecutesRuns the backuptask scriptdefined located onby the sharedcommand GlusterFSfield mount.
  • (in
  • volumes:this Mountscase, the GlusterFSdate directory to ensure access to the script.command).
  • mode: replicated: Ensures the task runs only when scheduled.
  • replicas: 0: Prevents the task from running immediately upon deployment.
  • swarm.cronjob.schedule=5* 1,13* * * *: Schedules the task to run dailyevery at 01:05 AM and 01:05 PM.minute.
  • restart_policy.condition=none: Ensures the task does not restart automatically after completion.

StepLogs

2: Deploy

Check the Stack

Deploy the stacklogs to yourverify Dockertask Swarm cluster:execution:

docker stackservice deploylogs -cswarm_cronjob_app
bookstack-backup.yml

Example bookstack-backupLogs:

swarm_cronjob_app.1.nvsjbhdhiagl@default    | Thu, 13 Dec 2018 20:04:37 UTC INF Starting swarm-cronjob v1.2.0
swarm_cronjob_app.1.nvsjbhdhiagl@default    | Thu, 13 Dec 2018 20:04:37 UTC INF Add cronjob with schedule * * * * * service=test
swarm_cronjob_app.1.nvsjbhdhiagl@default    | Thu, 13 Dec 2018 20:05:00 UTC INF Start job last_status=n/a service=test
swarm_cronjob_app.1.nvsjbhdhiagl@default    | Thu, 13 Dec 2018 20:06:00 UTC INF Start job last_status=n/a service=test
swarm_cronjob_app.1.nvsjbhdhiagl@default    | Thu, 13 Dec 2018 20:07:00 UTC INF Start job last_status=n/a service=test
swarm_cronjob_app.1.nvsjbhdhiagl@default    | Thu, 13 Dec 2018 20:08:00 UTC INF Start job last_status=n/a service=test

Once deployed, the backup scriptcronjob will execute the defined task at the scheduledspecified times, ensuring your BookStack data is regularly backed up to your NAS system.schedule.