Skip to content

Instantly share code, notes, and snippets.

@balos1
Last active February 9, 2021 22:41
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save balos1/6944fcf6a0f1c5872f1a92e9e0c69e5c to your computer and use it in GitHub Desktop.
Save balos1/6944fcf6a0f1c5872f1a92e9e0c69e5c to your computer and use it in GitHub Desktop.
Dumps a mongodb database from a docker data container / restores a mongodb database dump to a docker data container.
#!/bin/bash
# Run like so:
# docker-mongodump <mongo container name> <path to backups>
MONGO_CONTAINER=$1
BACKUPS_PATH=$2
DAY=`/bin/date +%Y%m%d`
PAST_DAY=`/bin/date -d '3 days ago' +%Y%m%d`
# make the backups and tar
cd $BACKUPS_PATH
docker exec $MONGO_CONTAINER mongodump --out /dump
docker cp $MONGO_CONTAINER:/dump ./
tar zcf $BACKUPS_PATH/dump.$DAY.tar.gz ./dump
# remove raw dump and backups older than 2 days
rm -rf ./dump
rm ./dump.$PAST_DAY.tar.gz
#!/bin/bash
# use like so
echo "docker-mongorestore <container> <path to dump on host> <dump destination in container> [--drop]"
MONGO_CONTAINER=$1
MONGO_DUMP_HOST_PATH=$2
MONGO_DUMP_PATH=$3
DROP=$4
docker cp $MONGO_DUMP_HOST_PATH $MONGO_CONTAINER:$MONGO_DUMP_PATH
docker exec $MONGO_CONTAINER mongorestore $MONGO_DUMP_PATH $DROP
docker exec $MONGO_CONTAINER rm -rf $MONGO_DUMP_PATH
@phanchanra
Copy link

Help I have shell script in my collection and then I execute it inside by project for backup database but not working Please help me
this my shell script inside docker.

#!/bin/bash
DIRECTORY="home"
CONTAINER="mongodb"
DB_NAME="pos-rabbit"
BACKUP_PATH="backup"
BACKUP_NAME="Pos-Rabbit_$(date +%Y-%m-%d)"
docker exec -t $CONTAINER mongodump --db $DB_NAME --out /$BACKUP_PATH/$BACKUP_NAME
docker cp $CONTAINER:/$BACKUP_PATH/$BACKUP_NAME /$DIRECTORY

And then I have method get script to run but it error docker: not found\n' what suggestion with docker container

export const execBySelector = new ValidatedMethod({
  name: 'app.shellJs.execBySelector',
  mixins: [CallPromiseMixin],
  validate: new SimpleSchema({
    selector: {
      type: Object,
      blackbox: true,
    },
  }).validator(),
  run({ selector }) {
    if (Meteor.isServer) {
      try {

        let result = 'Shell executed'
        let getShell = ShellJs.findOne(selector)

        console.log(getShell && getShell.command)
        console.log('----- Shell Command -----')

        if (getShell) {
          const child = shell.exec(getShell.command, { silent: true })
          console.log(child)
          if (child.code == 0) {
            result = child.stdout
          } else {
            throw child
          }
        } else {
          throw 'Shell do not exist'
        }

        return result
      } catch (err) {
        throwError('Shell Exec By Selector', err)
      }
    }
  },
})

Show error

{ [String: '']
  stdout: '',
  stderr: '/bin/sh: 11: docker: not found\n/bin/sh: 12: docker: not found\ntar: Pos-Rabbit_2020-02-19: Cannot stat: No such file or directory\ntar: Exiting with failure status due to previous errors\n/bin/sh: 20: gdrive: not found\n/bin/sh: 25: docker: not found\n',
  code: 127,
  cat: [Function: bound ],
  exec: [Function: bound ],
  grep: [Function: bound ],
  head: [Function: bound ],
  sed: [Function: bound ],
  sort: [Function: bound ],
  tail: [Function: bound ],
  to: [Function: bound ],
  toEnd: [Function: bound ],
  uniq: [Function: bound ] }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment