Please find below script
if [ "$#" -eq 0 ]; then
dbs=('test_db_1' 'test_db_2')
path='/var/backups/'
else
dbs=( "$@" )
path='/app/mybackups/'
fi
dbuser='test_user_name'
dbpassword='test_pass_word'
now="$(date +'%d-%m-%Y_%T')"
currentDate="$(date)"
dayToSubtract=365
cd ${path}
echo "location: $path"
for element in ${dbs[@]}
do
echo $element
echo "Starting backup now for db: $element on $now"
mongodump -u ${dbuser} -p ${dbpassword} --authenticationDatabase 'admin' -d $element --gzip --archive=${element}_$now.archive
dateToBeRemoved=$(date --date="${currentDate} -${dayToSubtract} day" +%d-%m-%Y)
echo $dateToBeRemoved
fileToBeRemoved="${element}_${dateToBeRemoved}"
echo Removing $fileToBeRemoved
rm $fileToBeRemoved*
done
echo All Done!
echo "location: $path"
I have been using this script for more than a year for backing up my dbs on the server on a daily basis and I also use it to take manual backups by passing command-line arguments to this script.
Recently I got a new server having ubuntu and its giving following error there
daily.sh: 2: daily.sh: Syntax error: "(" unexpected (expecting "fi")
Please find below screenshot for reference:
After adding:
#!/usr/bin/env bash
Getting same error:
Ran which bash, got the following
Added the shebang according to which bash result but still facing same issue
When running from the current folder:
check for the BOM:
Ubuntu Info: Distributor ID: Ubuntu Description: Ubuntu 18.04.3 LTS Release: 18.04 Codename: bionic
sh
to run abash
script (that should be in the text, not in the picture), which points todash
in Ubuntu, notbash
.