2021-04-01 13:30:06 +00:00
|
|
|
#!/bin/bash
|
|
|
|
|
2021-10-19 15:10:31 +00:00
|
|
|
# First of all, let's pamper awscli because Python is so special:
|
|
|
|
pip3 install --upgrade awscli
|
|
|
|
|
2021-04-01 13:30:06 +00:00
|
|
|
BACKUP=$1
|
|
|
|
if [[ $BACKUP == "" ]]; then
|
|
|
|
echo "No backup name given. It should look like '2020-01-29'."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2021-04-07 12:04:46 +00:00
|
|
|
# Get current script directory (pwd doesn't cut it)
|
|
|
|
csd=$(cd -P -- "$(dirname -- "$0")" && pwd -P)
|
2021-04-01 13:30:06 +00:00
|
|
|
# Set the environment:
|
|
|
|
set -o allexport
|
2021-04-07 12:04:46 +00:00
|
|
|
cat $csd/../.env | grep "AWS_ACCESS_KEY_ID\|AWS_SECRET_ACCESS_KEY\|S3_BACKUP_PATH\|SKYNET_DB_USER\|SKYNET_DB_PASS\|SKYNET_DB_HOST\|SKYNET_DB_PORT" >.tmpenv
|
2021-04-01 13:30:06 +00:00
|
|
|
source .tmpenv
|
|
|
|
rm .tmpenv
|
|
|
|
set +o allexport
|
|
|
|
# Check for AWS credentials:
|
|
|
|
if [[ $AWS_ACCESS_KEY_ID == "" || $AWS_SECRET_ACCESS_KEY == "" ]]; then
|
|
|
|
echo "Missing AWS credentials!"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
# Check for backup path:
|
|
|
|
if [[ $S3_BACKUP_PATH == "" ]]; then
|
|
|
|
echo "Missing S3_BACKUP_PATH!"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
### MONGO DB ###
|
|
|
|
# Check if the backup exists:
|
|
|
|
totalFoundObjects=$(aws s3 ls $S3_BACKUP_PATH/$BACKUP --recursive --summarize | grep "mongo.tgz" | wc -l)
|
|
|
|
if [ "$totalFoundObjects" -eq "0" ]; then
|
|
|
|
echo "This backup doesn't exist!"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
# Get the backup from S3:
|
|
|
|
aws s3 cp $S3_BACKUP_PATH/$BACKUP/mongo.tgz mongo.tgz
|
|
|
|
# Prepare a clean `to_restore` dir:
|
2021-10-19 15:10:31 +00:00
|
|
|
rm -rf $csd/../docker/data/mongo/db/backups/to_restore
|
|
|
|
mkdir -p $csd/../docker/data/mongo/db/backups/to_restore
|
2021-04-01 13:30:06 +00:00
|
|
|
# Decompress the backup:
|
2021-10-19 15:10:31 +00:00
|
|
|
tar -xzf mongo.tgz -C $csd/../docker/data/mongo/db/backups/to_restore
|
2021-04-01 13:30:06 +00:00
|
|
|
rm mongo.tgz
|
|
|
|
# Restore the backup:
|
|
|
|
# The name of the backup is not `mongo` due to the way we're creating it,
|
|
|
|
# it's $BACKUP.
|
|
|
|
docker exec mongo \
|
2021-10-19 15:12:00 +00:00
|
|
|
mongorestore --drop \
|
2021-04-01 13:30:06 +00:00
|
|
|
mongodb://$SKYNET_DB_USER:$SKYNET_DB_PASS@$SKYNET_DB_HOST:$SKYNET_DB_PORT \
|
|
|
|
/data/db/backups/to_restore/$BACKUP
|
|
|
|
# Clean up:
|
2021-10-19 15:10:31 +00:00
|
|
|
rm -rf $csd/../docker/data/mongo/db/backups/to_restore
|