Daily MongoDB Backup with Cron
Use mongodump with --gzip and --archive to a dated file, scheduled once a day. Add --uri or auth flags for a secured database, and use absolute paths so cron finds mongodump.
The crontab line
0 3 * * * /usr/bin/mongodump --gzip --archive=/backup/mongo-$(date +\%F).gz
--archive writes a single file, --gzip compresses it, and $(date +%F) dates it. Escape % as \% in the crontab.
With authentication
0 3 * * * /usr/bin/mongodump --uri="mongodb://user:pass@localhost:27017/mydb" --gzip --archive=/backup/mydb-$(date +\%F).gz
Prune old backups
Chain a cleanup to keep 14 days: && find /backup -name 'mongo-*.gz' -mtime +14 -delete.
FAQ
What cron line backs up MongoDB daily?
0 3 * * * mongodump --gzip --archive=/backup/mongo-$(date +\%F).gz runs a compressed daily dump. Escape % as \% in crontab.
How do I back up a MongoDB with authentication in cron?
Pass --uri="mongodb://user:pass@host:27017/db" to mongodump, or use --username/--password/--authenticationDatabase flags.
How do I delete old MongoDB backups automatically?
Chain find /backup -name 'mongo-*.gz' -mtime +14 -delete after the dump to keep only the last 14 days.