2022-10-09 00:51:08 +03:00

76 lines
2.4 KiB
Bash

#!/usr/bin/env bash
# s3cmd.sh - upload files to S3 compatible object storage via s3cmd.
# Copyright (c) 2022 ge <https://nixhacks.net/>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
tgt_s3cmd() {
# Upload files location from __main_target_path to object storage
# using s3cmd(1). Handle 's3' URI scheme.
#
# Usage: tgt_s3cmd URI
log "Run handler ${FUNCNAME[0]}()"
local uri
uri="$1"
if [[ "${uri%%:*}" != 's3' ]]; then
log -p "Error: Wrong URI scheme: ${uri%%:*}" >&2; exit 1
fi
is_installed s3cmd
if [[ "${uri: -1}" != '/' ]]; then
uri="$uri/" # Add trailing slash
fi
s3cmd_options="${s3cmd_options:---preserve --quiet --no-progress}"
# Set s3cmd comand. See s3cmd(1)
if [ -n "$s3_access_key" ] && \
[ -n "$s3_secret_key" ] && \
[ -n "$s3_host" ] && \
[ -n "$s3_host_bucket" ]
then
s3_region="${s3_region:-na}" # fallback to 'not available' region
# Use parameters provided from backup script
# shellcheck disable=SC2154,SC2086
set -- s3cmd $s3cmd_options \
--access_key="$s3_access_key" \
--secret_key="$s3_secret_key" \
--region="$s3_region" \
--host="$s3_host" \
--host-bucket="$s3_host_bucket" \
put "${backups[@]}" "$uri"
else
# Use configuration file
s3cmd_config="${s3cmd_comfig:-$HOME/.s3cfg}"
# shellcheck disable=SC2154,SC2086
set -- s3cmd $s3cmd_options --config "$s3cmd_config" \
put "${backups[@]}" "$uri"
fi
echo "Run command: $*" |
sed -E 's/--secret_key=[a-zA-Z0-9]+ /--secret_key=xxx /' | log
# ^^^ hide secret_key from output ^^^
# Upload backups
# shellcheck disable=SC2154
#"$@" 2>> "$log_file"
"$@" 2>> "$log_file"
}