This repository has been archived on 2022-10-07. You can view files and clone it, but cannot push or open issues or pull requests.
skynet-webportal/scripts/db_backup.sh

76 lines
2.6 KiB
Bash
Raw Normal View History

#!/bin/bash
echo "`date +"%Y-%m-%d-%H%M"` Starting a DB backup."
# First of all, let's pamper awscli because Python is so special:
pip3 install --upgrade awscli
2021-04-13 10:08:26 +00:00
2021-04-07 12:04:46 +00:00
# Get current script directory (pwd doesn't cut it)
csd=$(cd -P -- "$(dirname -- "$0")" && pwd -P)
# Set the environment. We only grab the entries we need because otherwise we
# need to deal with the edge cases presented by problematic values.
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
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
# Take the current datetime:
DT=$(date +%Y-%m-%d)
### MONGO DB ###
echo "Creating a backup of MongoDB:"
# Check if a backup already exists:
totalFoundObjects=$(aws s3 ls $S3_BACKUP_PATH/$DT --recursive --summarize | grep "mongo" | wc -l)
if [ "$totalFoundObjects" -ge "1" ]; then
echo "Backup already exists for today. Skipping."
else
# Create the backup:
docker exec mongo \
mongodump \
-o /data/db/backups/$DT \
mongodb://$SKYNET_DB_USER:$SKYNET_DB_PASS@$SKYNET_DB_HOST:$SKYNET_DB_PORT
docker exec mongo chmod o+rw /data/db/backups/
if [[ $? > 0 ]]; then
echo "Creating a MongoDB backup failed. Skipping."
else
# Compress the backup:
2021-04-07 12:04:46 +00:00
cd $csd/../docker/data/mongo/db/backups/ && ls -l && tar -czf mongo.tgz $DT && cd -
# Upload the backup to S3:
2021-04-07 12:04:46 +00:00
aws s3 cp $csd/../docker/data/mongo/db/backups/mongo.tgz $S3_BACKUP_PATH/$DT/mongo.tgz
# Clean up
2021-04-07 12:04:46 +00:00
rm -rf $DT.tgz $csd/../docker/data/mongo/db/backups/mongo.tgz
echo "Finished MongoDB backup."
fi
docker exec mongo rm -rf /data/db/backups/$DT
fi
### COCKROACH DB ###
echo "Creating a backup of CockroachDB:"
# Check if a backup already exists:
totalFoundObjects=$(aws s3 ls $S3_BACKUP_PATH/$DT --recursive --summarize | grep "cockroach" | wc -l)
if [ "$totalFoundObjects" -ge "1" ]; then
echo "Backup already exists for today. Skipping."
else
# Create a cockroachdb backup:
docker exec cockroach \
cockroach sql \
--host cockroach:26257 \
--certs-dir=/certs \
--execute="BACKUP TO '$S3_BACKUP_PATH/$DT/cockroach/?AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY_ID&AWS_SECRET_ACCESS_KEY=$AWS_SECRET_ACCESS_KEY';"
if [[ $? > 0 ]]; then
echo "Creating a CockroachDB backup failed. Skipping."
else
echo "Successfully backed up CockroachDB."
fi
fi