Skip to content

Instantly share code, notes, and snippets.

@crudh
Created September 25, 2013 08:35
Show Gist options
  • Select an option

  • Save crudh/6696735 to your computer and use it in GitHub Desktop.

Select an option

Save crudh/6696735 to your computer and use it in GitHub Desktop.
Exports MongoDB to BSON, creates an archive and mails it for backup purposes. Make sure that mailx is installed and in the path.
#!/bin/sh
# CONFIGURE THIS
outputBase=/Users/crudh/Code/shell-scripts/mongo-backup/test
resultPrefix=system-name
mailSubject="Backup - mongodb"
mailBody="Backup - mongodb"
mailRecipient=christian@primian.se
# DO NOT CHANGE BELOW HERE--------------------------
current=`date +"%Y%m%d-%H%M"`
complete=$outputBase/$resultPrefix-$current
resultFile=$complete.tar.bz2
# Create the folder
mkdir -p $complete
# Make the backup
mongodump -o $complete >/dev/null 2>&1
# Create the archive file
tar -c --bzip2 -f $resultFile $complete >/dev/null 2>&1
# Remove the folder
rm -rf $complete
# Mail the file
echo "$mailBody" | mailx -s "$mailSubject" -a "$resultFile" $mailRecipient
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment