Skip to content

Instantly share code, notes, and snippets.

@Ravaelles
Last active March 10, 2023 14:00
Show Gist options
  • Save Ravaelles/8925f1e0d1b1b4d01d5eb7c68b10c39b to your computer and use it in GitHub Desktop.
Save Ravaelles/8925f1e0d1b1b4d01d5eb7c68b10c39b to your computer and use it in GitHub Desktop.
Export all MongoDB databases to directories in "Y-m-d" date format using mongodump
#!/bin/bash
# Set the base directory where the new directory will be created
base_dir="/home/BACKUPS"
# Create a variable for the current date in Y-m-d format
current_date=$(date +%Y-%m-%d)
# Define an array to store the messages
declare -a messages
# ==========================================
function mongodb_backup {
PROJECT_NAME="$1"
URI_CONNECTION_STRING="$2"
OUTPUT_DIR="$base_dir/$PROJECT_NAME/$current_date"
# Do the backup
mongodump --uri $URI_CONNECTION_STRING -o $OUTPUT_DIR
# Create a directory with the current date as the name, relative to the base directory
mkdir -p "$base_dir/$PROJECT_NAME/$current_date"
# Output a message indicating the directory has been created
messages+=("# Backed up $1 to: $OUTPUT_DIR")
}
function print_status {
echo -e "\n"
# Output all the messages at the end
for message in "${messages[@]}"
do
echo -e "$message"
done
echo -e "Finished!\n"
}
# ==========================================
mongodb_backup "MyProject1" "mongodb://Admin:MYPASS@appcluster.15lf4.mongodb.net/mydb1"
mongodb_backup "ProjectName2" "mongodb://Admin:MYPASS@appcluster.15lf4.mongodb.net/mydb2"
mongodb_backup "TheProject3" "mongodb://Admin:MYPASS@appcluster.15lf4.mongodb.net/mydb3"
# ==========================================
print_status
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment