Add backuptask.js to work with new storage interface

This commit is contained in:
Johannes Zellner
2017-04-11 11:00:55 +02:00
parent 798c2ff921
commit 7fdf491815
11 changed files with 2885 additions and 351 deletions

View File

@@ -2,6 +2,8 @@
set -eu -o pipefail
readonly script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
if [[ $EUID -ne 0 ]]; then
echo "This script should be run as root." >&2
exit 1
@@ -15,92 +17,16 @@ fi
readonly APPS_DATA_DIR="${HOME}/appsdata"
# verify argument count
if [[ "$1" == "s3" && $# -lt 9 ]]; then
echo "Usage: backupapp.sh s3 <appId> <s3 config url> <s3 data url> <access key id> <access key> <region> <endpoint> <password> [session token]"
if [[ $# -lt 2 ]]; then
echo "Usage: backupbox.sh <backupId> <appId>"
exit 1
fi
if [[ "$1" == "filesystem" && $# -lt 6 ]]; then
echo "Usage: backupapp.sh filesystem <appId> <backupFolder> <configFileName> <dataFileName> <password>"
exit 1
fi
# extract arguments
readonly backup_id="$1"
readonly app_id="$2"
if [[ "$1" == "s3" ]]; then
# env vars used by the awscli
readonly s3_config_url="$3"
readonly s3_data_url="$4"
export AWS_ACCESS_KEY_ID="$5"
export AWS_SECRET_ACCESS_KEY="$6"
export AWS_DEFAULT_REGION="$7"
readonly endpoint_url="$8"
readonly password="$9"
echo "Running app backup task"
DEBUG="box*" ${script_dir}/../backuptask.js "${backup_id}" "${app_id}"
if [ $# -gt 9 ]; then
export AWS_SESSION_TOKEN="${10}"
fi
elif [[ "$1" == "filesystem" ]]; then
readonly backup_folder="$3"
readonly backup_config_fileName="$4"
readonly backup_data_fileName="$5"
readonly password="$6"
fi
# perform backup
readonly app_data_dir="${APPS_DATA_DIR}/${app_id}"
readonly tar_bin="/home/yellowtent/box/helper/tarjs"
# will be checked at the end
try=0
if [[ "$1" == "s3" ]]; then
# may be empty
optional_args=""
if [ -n "${endpoint_url}" ]; then
optional_args="--endpoint-url ${endpoint_url}"
fi
# Upload config.json first because uploading tarball might take a lot of time, leading to token expiry
for try in `seq 1 5`; do
echo "Uploading config.json to ${s3_config_url} (try ${try})"
error_log=$(mktemp)
# 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_dir}/config.json" \
| aws ${optional_args} s3 cp - "${s3_config_url}" 2>"${error_log}"; then
break
fi
cat "${error_log}" && rm "${error_log}"
done
for try in `seq 1 5`; do
echo "Uploading backup to ${s3_data_url} (try ${try})"
error_log=$(mktemp)
if ${tar_bin} "${app_data_dir}" . \
| openssl aes-256-cbc -e -pass "pass:${password}" \
| aws ${optional_args} s3 cp - "${s3_data_url}" 2>"${error_log}"; then
break
fi
cat "${error_log}" && rm "${error_log}"
done
elif [[ "$1" == "filesystem" ]]; then
mkdir -p $(dirname "${backup_folder}/${backup_config_fileName}")
echo "Storing backup config to ${backup_folder}/${backup_config_fileName}"
cat "${app_data_dir}/config.json" > "${backup_folder}/${backup_config_fileName}"
echo "Storing backup data to ${backup_folder}/${backup_data_fileName}"
${tar_bin} "${app_data_dir}" . | openssl aes-256-cbc -e -pass "pass:${password}" > "${backup_folder}/${backup_data_fileName}"
fi
if [[ ${try} -eq 5 ]]; then
echo "Backup failed uploading backup tarball"
exit 3
else
echo "Backup successful"
fi
echo "App backup successful"

View File

@@ -2,6 +2,10 @@
set -eu -o pipefail
readonly script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
BOX_DATA_DIR="${HOME}/boxdata"
if [[ $EUID -ne 0 ]]; then
echo "This script should be run as root." >&2
exit 1
@@ -12,78 +16,19 @@ if [[ $# == 1 && "$1" == "--check" ]]; then
exit 0
fi
# verify argument count
if [[ "$1" == "s3" && $# -lt 7 ]]; then
echo "Usage: backupbox.sh s3 <s3 url> <access key id> <access key> <region> <endpoint> <password> [session token]"
exit 1
fi
if [[ "$1" == "filesystem" && $# -lt 4 ]]; then
echo "Usage: backupbox.sh filesystem <backupFolder> <fileName> <password>"
if [[ $# -lt 1 ]]; then
echo "Usage: backupbox.sh <backupId>"
exit 1
fi
# extract arguments
if [[ "$1" == "s3" ]]; then
# env vars used by the awscli
readonly s3_url="$2"
export AWS_ACCESS_KEY_ID="$3"
export AWS_SECRET_ACCESS_KEY="$4"
export AWS_DEFAULT_REGION="$5"
readonly endpoint_url="$6"
readonly password="$7"
if [ $# -gt 7 ]; then
export AWS_SESSION_TOKEN="$8"
fi
elif [[ "$1" == "filesystem" ]]; then
readonly backup_folder="$2"
readonly backup_fileName="$3"
readonly password="$4"
fi
# perform backup
BOX_DATA_DIR="${HOME}/boxdata"
readonly backup_id="$1"
echo "Creating MySQL dump"
mysqldump -u root -ppassword --single-transaction --routines --triggers box > "${BOX_DATA_DIR}/box.mysqldump"
# will be checked at the end
try=0
echo "Running backup task"
DEBUG="box*" ${script_dir}/../backuptask.js "${backup_id}"
if [[ "$1" == "s3" ]]; then
for try in `seq 1 5`; do
echo "Uploading backup to ${s3_url} (try ${try})"
error_log=$(mktemp)
# may be empty
optional_args=""
if [ -n "${endpoint_url}" ]; then
optional_args="--endpoint-url ${endpoint_url}"
fi
# use aws instead of curl because curl will always read entire stream memory to set Content-Length
# aws will do multipart upload
if tar -czf - -C "${HOME}" --transform="s,^boxdata/\?,box/," --transform="s,^platformdata/mail/\?,mail/," --show-transformed-names boxdata platformdata/mail \
| openssl aes-256-cbc -e -pass "pass:${password}" \
| aws ${optional_args} s3 cp - "${s3_url}" 2>"${error_log}"; then
break
fi
cat "${error_log}" && rm "${error_log}"
done
elif [[ "$1" == "filesystem" ]]; then
echo "Storing backup to ${backup_folder}/${backup_fileName}"
mkdir -p $(dirname "${backup_folder}/${backup_fileName}")
tar -czf - -C "${HOME}" --transform="s,^boxdata/\?,box/," --transform="s,^platformdata/mail/\?,mail/," --show-transformed-names boxdata platformdata/mail \
| openssl aes-256-cbc -e -pass "pass:${password}" > "${backup_folder}/${backup_fileName}"
fi
if [[ ${try} -eq 5 ]]; then
echo "Backup failed"
exit 3
else
echo "Backup successful"
fi
echo "Backup successful"

View File

@@ -24,6 +24,7 @@ if [[ "${BOX_ENV}" == "cloudron" ]]; then
# only the top level ownership is changed because containers own the subdirectores
# and will chown them as necessary
chown yellowtent:yellowtent "${app_data_dir}"
chown yellowtent:yellowtent "${app_data_dir}/data"
else
readonly app_data_dir="${HOME}/.cloudron_test/appsdata/$1"
mkdir -p "${app_data_dir}/data"