Files
cloudron-box/src/scripts/backupapp.sh
T

78 lines
2.1 KiB
Bash
Raw Normal View History

#!/bin/bash
set -eu -o pipefail
if [[ $EUID -ne 0 ]]; then
echo "This script should be run as root." >&2
exit 1
fi
if [[ $# == 1 && "$1" == "--check" ]]; then
echo "OK"
exit 0
fi
2016-04-10 21:41:53 -07:00
if [ $# -lt 8 ]; then
echo "Usage: backupapp.sh <appid> <s3 config url> <s3 data url> <access key id> <access key> <session token> <region> <password>"
exit 1
fi
readonly DATA_DIR="${HOME}/data"
2016-04-10 18:23:29 -07:00
# env vars used by the awscli
2016-04-10 21:41:53 -07:00
readonly app_id="$1"
readonly s3_config_url="$2"
readonly s3_data_url="$3"
export AWS_ACCESS_KEY_ID="$4"
export AWS_SECRET_ACCESS_KEY="$5"
export AWS_SESSION_TOKEN="$6"
export AWS_DEFAULT_REGION="$7"
readonly password="$8"
2016-04-10 18:23:29 -07:00
readonly now=$(date "+%Y-%m-%dT%H:%M:%S")
readonly app_data_dir="${DATA_DIR}/${app_id}"
readonly app_data_snapshot="${DATA_DIR}/snapshots/${app_id}-${now}"
btrfs subvolume snapshot -r "${app_data_dir}" "${app_data_snapshot}"
2016-04-02 18:04:58 -07:00
# Upload config.json first because uploading tarball might take a lot of time, leading to token expiry
for try in `seq 1 5`; do
2016-04-10 18:23:29 -07:00
echo "Uploading config.json to ${s3_config_url} (try ${try})"
error_log=$(mktemp)
2015-08-25 12:27:38 -07:00
2016-04-10 18:23:29 -07:00
# use aws instead of curl because curl will always read entire stream memory to set Content-Length
# aws will do multipart upload
if cat "${app_data_snapshot}/config.json" \
2016-04-10 18:23:29 -07:00
| aws s3 cp - "${s3_config_url}" 2>"${error_log}"; then
break
fi
cat "${error_log}" && rm "${error_log}"
done
2016-01-29 11:44:14 +01:00
if [[ ${try} -eq 5 ]]; then
echo "Backup failed uploading config.json"
2016-04-02 17:57:15 -07:00
btrfs subvolume delete "${app_data_snapshot}"
2016-01-29 11:44:14 +01:00
exit 1
2016-01-29 12:31:59 +01:00
fi
2016-01-29 11:44:14 +01:00
for try in `seq 1 5`; do
2016-04-10 18:23:29 -07:00
echo "Uploading backup to ${s3_data_url} (try ${try})"
2016-01-29 11:44:14 +01:00
error_log=$(mktemp)
if tar -cvzf - -C "${app_data_snapshot}" . \
2016-04-10 21:46:01 -07:00
| openssl aes-256-cbc -e -pass "pass:${password}" \
2016-04-10 18:23:29 -07:00
| aws s3 cp - "${s3_data_url}" 2>"${error_log}"; then
2016-01-29 11:44:14 +01:00
break
fi
cat "${error_log}" && rm "${error_log}"
done
btrfs subvolume delete "${app_data_snapshot}"
if [[ ${try} -eq 5 ]]; then
echo "Backup failed uploading backup tarball"
exit 1
else
echo "Backup successful"
fi