0

I need to configure some value from cron tab and get those into my shell script file. I am explaining my script file below.

#!/bin/sh

#=====================================================================
# Set the following variables as per your requirement
#=====================================================================
# Database Name to backup
MONGO_DATABASE="ubot-db"
# Database host name
MONGO_HOST="127.0.0.1"
# Database port
MONGO_PORT="27017"
# Backup directory
BACKUPS_DIR="/home/subhrajp/UBOT/"
# Database user name
DBUSERNAME="username"
# Database password
DBPASSWORD="passw0rd"
# Authentication database name
DBAUTHDB="admin"
# Days to keep the backup
DAYSTORETAINBACKUP="14"
#=====================================================================

TIMESTAMP=$(date +'%Y_%m_%d_%H-%M-%S')
BACKUP_NAME="mongobackup_$TIMESTAMP"

echo "Performing backup of $MONGO_DATABASE"
echo "--------------------------------------------"
# Create dump
mongodump --host $MONGO_HOST -d $MONGO_DATABASE --port $MONGO_PORT --out "${BACKUPS_DIR}${BACKUP_NAME}"
echo "--------------------------------------------"
echo "Database backup complete!"

In this file I have hardcoded the BACKUPS_DIR value but I need to configure from crontab as I am running this file as cron job. My crontab setting is given below.

* * * * * /home/subhrajp/UBOT/git/uBot-Hosting/container/app/mean-stack/node-js/utils/backup-mongodb.sh

Here My requirement is I need to configure some values as environment variable from crontab and fetch those values inside my shell script which is given above.

subhra_user
  • 439
  • 5
  • 19
  • I have fixed and got help from this link. – subhra_user Feb 10 '21 at 09:02
  • You can set them [in the crontab itself](https://stackoverflow.com/questions/2229825/where-can-i-set-environment-variables-that-crontab-will-use). In general, I would recommend against it in the case of your `BACKUPS_DIR`, because those variables then apply to **all** crontab scripts equally. However, it is up to you to choose. – user1934428 Feb 10 '21 at 09:02

0 Answers0