Mupx cron job mongo backup


#1

Does anyone has luck taking backup from mongodb from cron jobs?

I have simple bash script which works fine If I run it standalone but not in cronjob?

Anyone has scripts to take backup from cronjobs for mupx.

Here is my script

PATH=$PATH:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:/root/
SHELL=/bin/sh PATH=/bin:/sbin:/usr/bin:/usr/sbin:/root/
containerName="mongodb"
containerBackupLoc="/dump"
dbName="meteor"
curDate=`date +"%y-%m-%d"`
backupLoc="/usr/backups"
# Mongo Backup
docker exec -it $containerName mongodump -d $dbName -o $containerBackupLoc/$dbName-$curDate
echo "copying data to local"
docker cp mongodb:$containerBackupLoc/$dbName-$curDate $backupLoc/$dbName-$curDate
echo "data copied"
#docker exec -it mongodb rm -rf $containerBackupLoc/$dbName-$curDate
echo "removed copy from mongodb docker"

which I added to cron job

51 16 * * * /usr/scripts/backup.sh 2>&1 >> /root/cron.log

this is not working and haven’t had any info in log file too.

NOTE: this script works fine as standalone but not in cronjob.


#2

Having a similar issue. Mine works well as a standalone, but not as a cron job.

APP_NAME="submithub"
TIMESTAMP=`date +%F-%H%M`
MONGODUMP_PATH="/usr/bin/mongodump"
BACKUPS_DIR="$APP_NAME"
BACKUP_NAME="$APP_NAME-$TIMESTAMP"
docker exec -it mongodb mongodump
docker cp mongodb:dump .
mkdir -p $BACKUPS_DIR
mv dump $BACKUP_NAME
tar -zcvf $BACKUPS_DIR/$BACKUP_NAME.tgz $BACKUP_NAME
rm -rf $BACKUP_NAME

#3

The usual issues with scripts which run correctly from the terminal, but fail as a cron job is that cron uses sh (rather than bash) and does not source the user’s environment. I suggest editing the “top” of the script to (a) use bash and (b) source the environment. Something like:

#!/bin/bash
. ~root/.bash_profile
# rest of script here