Skip to content

didrestore.sh

Carl Joakim Damsleth edited this page Feb 1, 2024 · 2 revisions
#!/bin/zsh

# This is a PoC restore script for did database backups
# It takes the connection string as the first parameter
# and the backup folder as the second parameter

# exit codes
# 0 - success
# 1 - no connection string specified
# 2 - mongosh command not on system
# 3 - mongodump not found on system
# 4 - no backup folder specified

# check if connection string is specified
if [[ $1 == "" ]] || [[ $# -lt 1 ]];then
    echo "USAGE: ./didrestore.sh <connectionstring> <folder>"
    exit 1
fi

# check if backup folder is specified
if [[ $2 == "" ]];then
    echo "ERROR: No backup folder location specified, exiting"
    exit 4
fi

# check if mongosh is installed
if ! command -v mongosh &> /dev/null
then
    echo "ERROR: mongosh not found on system"
    exit 3
fi

# check if mongodump is installed
if ! command -v mongodump &> /dev/null
then
    echo "ERROR: mongodump not found on system"
    exit 3
fi

CONNECTION_STRING=$1
echo "restoring all databases in folder $2"
# --writeConcern="{w:0}" is used to ignore the fact that a collection already exists
# retrywrites=false in the connection string is not sufficient when running against cosmosDB
# and will throw an error if the collection already exists
mongorestore --uri="$CONNECTION_STRING" --writeConcern="{w:0}" $2