Skip to content

Instantly share code, notes, and snippets.

@tbjers
Created August 22, 2012 23:10
Show Gist options
  • Save tbjers/3430452 to your computer and use it in GitHub Desktop.
Save tbjers/3430452 to your computer and use it in GitHub Desktop.
Round Robin MongoDB backups to S3 with Tar
#!/bin/sh
#Change these settings to match your system
ACCESS_KEY='YOUR_ACCESS_KEY'
ACCESS_SECRET='YOUR_SECRET_KEY'
BUCKET="YOUR_BUCKET_NAME"
DATABASE="YOUR_DATABASE_NAME"
# Do not change anything below this line
tool=`which s3multiput s3cmd`
cmdname=`basename $tool`
case $cmdname in
s3cmd|s3multiput) ;;
*)
echo 'Neither s3cmd nor s3multiput is installed on this system.'
exit 1
;;
esac
datename="db_daily_"`eval date +%d`""
filename="$DATABASE-1.tar"
echo "Database Backup: $datename started..."
mongodump -d $DATABASE >> /dev/null
if [ ! -d $datename ]; then
mkdir $datename
fi
# Split tarball up into 100 MB chunks for uploading to S3
tar --multi-volume --tape-length=102400 --info-script=./tarsplitter.sh --totals -c -f "$datename/$filename" dump
# Upload archives to S3
case $cmdname in
s3cmd)
s3cmd sync --delete-removed ./$datename/ s3://$BUCKET/$datename/
;;
s3multiput)
s3multiput --access_key "$ACCESS_KEY" --secret_key "$ACCESS_SECRET" --bucket "$BUCKET" --prefix $PWD/ ./$datename/
;;
esac
echo "Backup finished."
# Clean up after ourselves
rm -Rf $datename/
rm -Rf dump/
#!/bin/sh
archive=`expr $TAR_ARCHIVE : '\([^.-]*\)'`
volume=`printf "%02d" $TAR_VOLUME`
case $TAR_SUBCOMMAND in
-c) ;;
-d|-x|-t) test -r $archive-$volume || exit 1
;;
*) exit 1
esac
echo $archive-$volume.tar >&5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment