feat: Add tgt_s3cmd

This commit is contained in:
ge 2022-08-21 21:09:19 +03:00
parent ef30cd1561
commit a4e420337b
4 changed files with 91 additions and 6 deletions

View File

@ -202,20 +202,21 @@ for script in "${__args[@]}"; do
# Increase iterator # Increase iterator
((__i++)) || true ((__i++)) || true
# Unset on_error function to prevent misuse # Unset functions and variables to prevent misuse
unset on_error unset on_error
unset backups
# Unset user defined variables # Unset user defined variables
unset backups
unset compression unset compression
unset name_prefix unset name_prefix
unset name_date_format unset name_date_format
unset s3cmd_options
unset s3cmd_config
unset s3_access_key unset s3_access_key
unset s3_secret_key unset s3_secret_key
unset s3_access_token
unset s3_endpoint
unset s3_region unset s3_region
unset s3cmd_config unset s3_host
unset s3_host_bucket
unset tar_options unset tar_options
unset tar_exclude unset tar_exclude
unset mysqldump_options unset mysqldump_options

View File

@ -70,7 +70,7 @@ process_target() {
ftp) handler='tgt_ftp';; ftp) handler='tgt_ftp';;
sftp) handler='tgt_sftp';; sftp) handler='tgt_sftp';;
rsync) handler='tgt_rsync';; rsync) handler='tgt_rsync';;
s3) handler='tgt_s3';; s3) handler='tgt_s3cmd';;
sj) handler='tgt_sj';; sj) handler='tgt_sj';;
swift) handler='tgt_swift';; swift) handler='tgt_swift';;
dav) handler='tgt_dav';; dav) handler='tgt_dav';;

View File

@ -0,0 +1,83 @@
#!/usr/bin/env bash
# s3cmd.sh - upload files to S3 compatible object storage.
# 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
local src_path
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 "$s3cmd_config" ]; then
# Use configuration file
# shellcheck disable=SC2154
set -- s3cmd $s3cmd_options --config "$s3cmd_config" \
put "${backups[@]}" "$uri"
elif [ -n "$s3_access_key" ] && \
[ -n "$s3_secret_key" ] && \
[ -n "$s3_host" ] && \
[ -n "$s3_host_bucket" ]
then
s3_region="${s3_region:-na}" # fallback to 'no available' region
# Use parameters provided from backup script
# shellcheck disable=SC2154
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
# Exit if no configuration
log -p "Error: Bad s3cmd credentials."\
"See boring-backup(1) for details" >&2
exit 1
fi
echo "Run command: $*" |
sed -E 's/--secret_key=[a-zA-Z0-9]+ /--secret_key=xxx /' | log
# ^^^ hide secret_key from output ^^^
# Upload backups
if "$@" 2>> "$__log_file"; then
: # Success
else
handle_error \
"Error: Something went wrong when executing command:\n\t$*"
fi
}

View File

@ -27,6 +27,7 @@ LIBRARY="${LIBRARY:-./lib}"
. "$LIBRARY/handlers/sources/mysqldump.sh" . "$LIBRARY/handlers/sources/mysqldump.sh"
. "$LIBRARY/handlers/sources/pg_dump.sh" . "$LIBRARY/handlers/sources/pg_dump.sh"
. "$LIBRARY/handlers/targets/cp.sh" . "$LIBRARY/handlers/targets/cp.sh"
. "$LIBRARY/handlers/targets/s3cmd.sh"
if [ -f "$LIBRARY/extra.sh" ]; then if [ -f "$LIBRARY/extra.sh" ]; then
. "$LIBRARY/extra.sh" . "$LIBRARY/extra.sh"