Schedule backup Mongodb to AWS S3 with DroneCI cronjob

To schedule backup mongodb to AWS S3 could be done extremely easy in any docker pipeline, here is an example using DroneCI docker pipeline and cronjob feature. The complete code example could be found here: https://github.com/Asing1001/mongodb-s3-backup.

  1. Create .drone.jsonnet file using docker pipeline
  2. Use mongodump in step1 to create archive
  3. Upload the archive by s3 plugin in step2

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    local backupLocation = 'mongo-archive.gz';

    [{
    kind: 'pipeline',
    name: 'default',
    steps: [
    {
    name: 'mongodump',
    image: 'mongo:3.6.5',
    environment: {
    DB_URI: {
    from_secret: 'db_uri'
    },
    },
    commands: [
    'mongodump --gzip --uri="$DB_URI" --archive=' + backupLocation,
    ],
    },
    {
    name: 'upload',
    image: 'plugins/s3',
    settings: {
    bucket: 'mongo-backup',
    access_key: {
    from_secret: 'aws_access_key_id',
    },
    secret_key: {
    from_secret: 'aws_secret_access_key',
    },
    source: backupLocation,
    target: '/',
    },
    },
    ],
    trigger: {
    event: ['cron', 'push'],
    branch: 'master'
    },
    }]
  4. Activate the project in DroneCI and fill up variables aws_access_key_id, aws_secret_access_key, db_uri

  5. Setup Cron jobs in Drone project’s settings
    drone-mongo

  6. Push the repository and verify the job :)

Reference