Skip to main content

Configuration Example 2 (Replicated Mode)

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

 

 

 

 

Running a Custom Backup 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. The example focuses on a backup script that copies all files from the BookStack folder in a GlusterFS mount to a NAS system.

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:
    image: busybox
    command: ["/bin/sh", "-c", "/mnt/glustermount/backup/bookstack_backup.sh"]
    volumes:
      - "/mnt/glustermount:/mnt/glustermount"
    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

Explanation of Configuration:

  • command: Executes the backup script located on the shared GlusterFS mount.
  • volumes: Mounts the GlusterFS directory to ensure access to the script.
  • 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 daily at 01:05 AM and 01:05 PM.
  • restart_policy.condition=none: Ensures the task does not restart automatically after completion.

Step 2: Deploy the Stack

Deploy the stack to your Docker Swarm cluster:

docker stack deploy -c bookstack-backup.yml bookstack-backup

Once deployed, the backup script will execute at the scheduled times, ensuring your BookStack data is regularly backed up to your NAS system.