A simple bash script to backup Mongo databases.
Source code is published at github.com/DoctorEvidence/automongobackup .
Image is published at doctorevidence/automongobackup .
- Backup a full cluster
- Specify retention time in days
- Choose a specific day per month to keep forever
# Download the script
curl https://raw.githubusercontent.com/DoctorEvidence/automongobackup/main/automongobackup.sh --output automongobackup.sh && \
chmod +x automongobackup.sh
# Use a connection uri for mongo
export MONGO_URL='mongodb://user@password@localhost:27017/?tls=true&authSource=admin'
# Test connection using mongosh
mongosh $MONGO_URL
# Choose the day of month to keep backups forever
export BACKUP_DAY_TO_KEEP="03"
# Choose backup path
export DB_BACKUP_PATH="dbackup"
# Run the backup
./automongobackup.sh
You can run periodic mongodb backups with swarm-cronjob
services:
mongo-backup:
image: docker.io/mongo-backup:latest
environment:
MONGO_URL: "mongodb://user@password@my-mongodb:27017/?tls=true&authSource=admin"
DB_BACKUP_PATH: /backups
deploy:
mode: replicated
replicas: 0
labels:
- "swarm.cronjob.enable=true"
- "swarm.cronjob.schedule=0 7 * * *"
- "swarm.cronjob.skip-running=false"
restart_policy:
# https://github.com/crazy-max/swarm-cronjob/issues/44
condition: on-failure
max_attempts: 3
volumes:
/path/to/backup:/backups
You will need to have docker-engine installed or podman.
docker login docker.io -u doctorevidence # Build image for arm64 and amd64 docker buildx build --no-cache \ --platform linux/arm64 \ --platform linux/amd64 \ -t docker.io/doctorevidence/automongobackup:6 . \ --push # push image to docker registry docker push docker.io/doctorevidence/automongobackup:6