Sign inSign up

viperadnan/mongodump-rclone

By viperadnan

•Updated 8 months ago

Backup a MongoDB database using mongodump and upload it to a remote storage using rclone

Image
0

5.8K

viperadnan/mongodump-rclone repository overview

⁠mongodump-rclone

⚠️ ARCHIVED - This project is now read-only

This project has been superseded by dbstash⁠, a complete rewrite in Go that supports multiple databases (PostgreSQL, MongoDB, MySQL, MariaDB, Redis) with better performance, scheduling, and features.

Please use dbstash⁠ for new projects.

This is a simple script to backup a MongoDB database using mongodump and upload it to a remote storage using rclone. It is intended to be used as a cron job.

⁠Usage

bash ./entrypoint.sh

⁠Usage with Docker

Run the following command to backup a MongoDB database and upload it to a remote storage using rclone.

docker run \
  -e MONGO_DB=mydb \ 
  -e MONGO_COLLECTION=mycollection \
  -e RCLONE_REMOTE=myremote \
  -e RCLONE_REMOTE_PATH=/path/to/remote \ 
  -e MONGO_URI=mongodb://myuser:xxxxxxxxxx@xxxxxx:27017/mydb \ 
  -v $HOME/.config/rclone:/config/rclone \ 
  --rm \ 
  -it \
  mongodump-rclone

⁠Usage with Docker Compose

You can use the following docker-compose.yml file to backup a MongoDB database and upload it to a remote storage using rclone.

services:
  mongodump:
    image: mongodump-rclone
    environment:
      - MONGO_DB=mydb
      - MONGO_COLLECTION=mycollection
      - RCLONE_REMOTE=myremote
      - RCLONE_REMOTE_PATH=/path/to/remote
      - MONGO_URI=mongodb://myuser:xxxxxxxxxx@xxxxxx:27017/mydb
    volumes:
      - /home/user/.config/rclone:/config/rclone
  mongoexport:
    image: mongodump-rclone
    environment:
      - USE_MONGOEXPORT=true
      - MONGO_DB=mydb
      - MONGO_COLLECTION=mycollection
      - RCLONE_REMOTE=myremote
      - RCLONE_REMOTE_PATH=/path/to/remote
      - MONGO_URI=mongodb://myuser:xxxxxxxxxx@xxxxxx:27017/mydb
    volumes:
      - /home/user/.config/rclone:/config/rclone

Run the following command to start the services.

docker-compose up # for both services
docker-compose up mongodump # for mongodump
docker-compose up mongoexport # for mongoexport

⁠Configuration

The backup can be performed using the mongodump command or the mongoexport command. The script will use mongodump by default. You can change this behavior by setting the USE_MONGOEXPORT environment variable to true.

The script uses environment variables to configure the backup and upload process.

⁠mongodump
VariableDescriptionRequiredDefault
RCLONE_CONFIGThe content of the rclone configuration without it's name.NoNone
RCLONE_REMOTEThe name of the rclone remote to upload the backup to.Nodefault
RCLONE_REMOTE_PATHThe path on the remote storage where the backup will be uploaded.No/
MONGO_DBThe name of the MongoDB database to backup.NoNone
MONGO_COLLECTIONThe name of the MongoDB collection to backup.NoNone
FILENAMEThe base name of the backup file.Nomongodb
FILENAME_TIMESTAMPWhether to include a timestamp in the backup file name (true/false).Notrue
MONGO_URIThe MongoDB connection URI.NoNone
MONGODUMP_ARGSAdditional arguments to pass to the mongodump command.NoNone
RCLONE_ARGSAdditional arguments to pass to the rclone rcat command.NoNone
⁠mongoexport
VariableDescriptionRequiredDefault
RCLONE_CONFIGThe configuration for rclone.YesNone
RCLONE_REMOTEThe name of the remote to use with rclone.No"default"
RCLONE_ARGSAdditional arguments to pass to rclone.NoNone
RCLONE_REMOTE_PATHThe path in the remote to store the exported data.No"/"
MONGOEXPORT_ARGSAdditional arguments to pass to mongoexport.NoNone
FILENAMEThe base name of the output file.No"mongodb"
MONGO_DBThe name of the MongoDB database to export.YesNone
MONGO_COLLECTIONThe name of the MongoDB collection to export.YesNone
FILENAME_TIMESTAMPWhether to append a timestamp to the filename.No"true"
MONGOEXPORT_TYPEThe type of the export file (json or csv).Yes"json"
MONGO_URIThe MongoDB connection string.NoNone

In case the MONGO_URI is not provided, the script will try to connect to the MongoDB server running on localhost using the default port 27017.

If you want to use a rclone config file, you can mount the directory containing the file to /config/rclone. i.e docker run -v /home/user/.config/rclone:/config/rclone.

⁠Contributing

If you find a bug or have an idea for a new feature, please open an issue or submit a pull request.

⁠License

This project is licensed under the GNU General Public License v3.0 - see the LICENSE⁠ file for details.

Tag summary

Content type

Image

Digest

sha256:28e4e987c…

Size

152.8 MB

Last updated

over 2 years ago

docker pull viperadnan/mongodump-rclone