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/backup-aws-s3.sh

58 lines
2.7 KiB
Bash
Raw Normal View History

2022-02-11 13:25:34 +00:00
#! /usr/bin/env bash
set -e # exit on first error
echo "`date +"%Y-%m-%d %H:%M"` Starting backup process"
# ensure aws cli is upgraded
pip3 install --quiet --upgrade awscli
# import required environment variables from .env file
ENV_VARIABLES=("AWS_ACCESS_KEY_ID" "AWS_SECRET_ACCESS_KEY" "PORTAL_DOMAIN" "SERVER_DOMAIN" "SERVER_UID")
for ENV_VARIABLE in "${ENV_VARIABLES[@]}"; do
ENV_VARIABLE_VALUE=$(grep -v '^#' /home/user/skynet-webportal/.env | grep ${ENV_VARIABLE} || true)
if test -z "${ENV_VARIABLE_VALUE}"; then
2022-03-31 12:38:06 +00:00
# all variables except SERVER_DOMAIN are required
if [ "${ENV_VARIABLE}" != "SERVER_DOMAIN" ]; then
2022-02-11 13:25:34 +00:00
echo "Environment variable ${ENV_VARIABLE} is not set" && exit 1
fi
2022-03-31 12:38:06 +00:00
else
export ${ENV_VARIABLE_VALUE}
2022-02-11 13:25:34 +00:00
fi
done
2022-02-22 15:23:35 +00:00
# create bucket skynet-backup-[portaldomain] (replace dots with dashes and strip anything other than alnum)
# ie. siasky.net backup results in skynet-backup-siasky-net basket name
BUCKET_NAME=$(echo skynet-backup-${PORTAL_DOMAIN} | tr '.' '-' | tr -cd '[[:alnum:]]-')
2022-02-11 13:25:34 +00:00
# create server prefix
if test -z "${SERVER_DOMAIN}"; then
2022-02-22 15:23:35 +00:00
# if domain name is empty use just uid (replace dots with dashes and strip anything other than alnum)
SERVER_PREFIX=$(echo ${SERVER_UID} | tr '.' '-' | tr -cd '[[:alnum:]]-')
2022-02-11 13:25:34 +00:00
else
2022-02-22 15:23:35 +00:00
# use both uid and server domain if available (replace dots with dashes and strip anything other than alnum)
2022-03-31 12:38:06 +00:00
SERVER_PREFIX=$(echo ${SERVER_DOMAIN}-${SERVER_UID} | tr '.' '-' | tr -cd '[[:alnum:]]-')
SERVER_PREFIX_LEGACY=$(echo ${SERVER_UID}-${SERVER_DOMAIN} | tr '.' '-' | tr -cd '[[:alnum:]]-')
2022-02-11 13:25:34 +00:00
fi
aws s3api create-bucket --acl private --bucket ${BUCKET_NAME}
2022-03-31 12:38:06 +00:00
# move old backup dir to new location if legacy backup path exists
if test -n "${SERVER_PREFIX_LEGACY}"; then
aws s3 mv --recursive s3://${BUCKET_NAME}/${SERVER_PREFIX_LEGACY} s3://${BUCKET_NAME}/${SERVER_PREFIX}
fi
2022-02-11 13:25:34 +00:00
# sync all nginx logs
2022-02-22 15:23:35 +00:00
mkdir -p /home/user/skynet-webportal/docker/data/nginx/logs # ensure path exists
2022-02-11 13:25:34 +00:00
aws s3 sync --no-progress /home/user/skynet-webportal/docker/data/nginx/logs s3://${BUCKET_NAME}/${SERVER_PREFIX}/docker/data/nginx/logs
2022-02-22 15:23:35 +00:00
# generate and sync skylinks dump
SKYLINKS_PATH=logs/skylinks/$(date +"%Y-%m-%d").log
mkdir -p /home/user/skynet-webportal/logs/skylinks # ensure path exists
2022-09-29 17:48:59 +00:00
find /home/user/skynet-webportal/logs/skylinks -type f -mtime +7 -delete # delete skylink dumps older than 7 days
2022-02-22 15:23:35 +00:00
docker exec sia siac skynet ls --recursive --alert-suppress > /home/user/skynet-webportal/${SKYLINKS_PATH}
aws s3 cp --no-progress /home/user/skynet-webportal/${SKYLINKS_PATH} s3://${BUCKET_NAME}/${SERVER_PREFIX}/${SKYLINKS_PATH}
2022-02-11 13:25:34 +00:00
echo "`date +"%Y-%m-%d %H:%M"` Backup finished successfully"