Skip to content

Instantly share code, notes, and snippets.

@bayzi
Created November 29, 2019 10:16
Show Gist options
  • Save bayzi/c3966862df8517b77ee99824ca221f7e to your computer and use it in GitHub Desktop.
Save bayzi/c3966862df8517b77ee99824ca221f7e to your computer and use it in GitHub Desktop.
#!/bin/sh
DateStamp=$(date +%d%m%y)
BackupDirectory="/data/configdb/backup"
mkdir -p $BackupDirectory
sslCAFile="/data/configdb/mongodb-ssl-cert.pem"
DATABASE='database'
USERNAME='user'
PASSWORD='password'
notify(){
echo "$1"
echo "Sending notification ..."
}
lock=$(mongo -u ${USERNAME} -p ${PASSWORD} --authenticationDatabase "admin" admin \
--quiet --ssl --sslAllowInvalidCertificates --sslCAFile ${sslCAFile} \
--eval "JSON.stringify(db.fsyncLock())" | grep -v NETWORK | sed -e 's/: [a-zA-Z]*(\(.*\))/: "\1"/' | jq '.ok')
if [ $lock -eq 1 ]; then
mongodump -u ${USERNAME} -p ${PASSWORD} --db ${DATABASE} \
--authenticationDatabase "admin" --ssl --sslAllowInvalidCertificates \
--sslCAFile ${sslCAFile} -o $BackupDirectory/$DateStamp
if [ $? -eq 0 ] ; then
echo "Backup successful"
unlock=$(mongo -u ${USERNAME} -p ${PASSWORD} --authenticationDatabase "admin" admin \
--quiet --ssl --sslAllowInvalidCertificates --sslCAFile ${sslCAFile}\
--eval "JSON.stringify(db.fsyncUnlock())" | grep -v NETWORK | sed -e 's/: [a-zA-Z]*(\(.*\))/: "\1"/' | jq '.ok')
if [ $unlock -eq 1 ] ; then
echo "db is successfully unlocked"
cd $BackupDirectory
tar -czvf mongobkp_$DateStamp.tar.gz $DateStamp
rm -rf $DateStamp
# echo "TODO ---- send backup files to remote"
notify "Backup OK"; exit 0
else
notify "Error when unlocking database"; exit 1
fi
else
notify "back up failed"; exit 1
fi
else
notify "Could not lock db"; exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment