Last active
August 23, 2021 17:34
-
-
Save samchouse/31736b99cb16ae80e8562d53c97a7b6f to your computer and use it in GitHub Desktop.
Backup your MongoDB database
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| service="Mongo-Backup" | |
| username='xenfo' | |
| me=$(whoami) | |
| as_user() { | |
| if [ "$me" == $username ]; then | |
| bash -c "$1" | |
| else | |
| sudo su - $username -c "$1" | |
| fi | |
| } | |
| start() { | |
| if as_user "screen -ls | grep $service" >/dev/null; then | |
| echo "$service is already running!" | |
| exit 1 | |
| fi | |
| screen -dmS $service forward | |
| if as_user "screen -ls | grep $service" >/dev/null; then | |
| echo "$service is now running." | |
| else | |
| echo "Error! Could not start $service!" | |
| fi | |
| } | |
| stop() { | |
| if as_user "screen -ls | grep $service" >/dev/null; then | |
| screen -X -S $service quit | |
| else | |
| echo "$service is not running!" | |
| exit 1 | |
| fi | |
| if as_user "screen -ls | grep $service" >/dev/null; then | |
| echo "Error! $service could not be stopped." | |
| else | |
| echo "$service is stopped." | |
| fi | |
| } | |
| start | |
| mongodump --uri="mongodb://localhost:28015/?authSource=admin" --db=project --out=/home/xenfo/Documents/project/mongo | |
| mongorestore --uri="mongodb+srv://cluster.mongodb.net/?authSource=admin" /home/xenfo/Documents/project/mongo --nsInclude="*.*" | |
| stop | |
| exit 0 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment