Skip to content

Instantly share code, notes, and snippets.

@Intelrunner
Last active July 29, 2024 04:53
Show Gist options
  • Save Intelrunner/92eb70c4373eb08b9a7fb436b67c2651 to your computer and use it in GitHub Desktop.
Save Intelrunner/92eb70c4373eb08b9a7fb436b67c2651 to your computer and use it in GitHub Desktop.
mongodb_backup_setup
#!/bin/bash
# Check to see if the backup directory exists
# If not, create it
if [ ! -d "/var/mongobackup" ]; then
sudo mkdir /var/mongobackup
fi
sudo chmod 777 /var/mongobackup
mongodb_pwd=$(curl "http://metadata.google.internal/computeMetadata/v1/instance/attributes/MONGODB_PWD" -H "Metadata-Flavor: Google")
target_db=$(curl "http://metadata.google.internal/computeMetadata/v1/instance/attributes/TARGET_DB" -H "Metadata-Flavor: Google")
logging_bucket=$(curl "http://metadata.google.internal/computeMetadata/v1/instance/attributes/LOGGING_BUCKET" -H "Metadata-Flavor: Google")
tdate=$(date +%H-%M-%S)
# Backup the database
archive=$(echo "/var/mongobackup/"$tdate"_"$target_db".gzip")
mongodump --username admin \
--password $mongodb_pwd \
-h localhost:27017 \
--authenticationDatabase admin \
--gzip \
--archive=$archive
gsutil cp $archive gs://$logging_bucket/$target_db/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment