From a4e420337b727cf82be833678463c4c269fdf0fe Mon Sep 17 00:00:00 2001 From: ge Date: Sun, 21 Aug 2022 21:09:19 +0300 Subject: [PATCH] feat: Add tgt_s3cmd --- src/boring-backup | 11 ++-- src/lib/backup.sh | 2 +- src/lib/handlers/targets/s3cmd.sh | 83 +++++++++++++++++++++++++++++++ src/lib/lib.sh | 1 + 4 files changed, 91 insertions(+), 6 deletions(-) create mode 100644 src/lib/handlers/targets/s3cmd.sh diff --git a/src/boring-backup b/src/boring-backup index 9eb1ed4..6512a78 100755 --- a/src/boring-backup +++ b/src/boring-backup @@ -202,20 +202,21 @@ for script in "${__args[@]}"; do # Increase iterator ((__i++)) || true - # Unset on_error function to prevent misuse + # Unset functions and variables to prevent misuse unset on_error + unset backups # Unset user defined variables - unset backups unset compression unset name_prefix unset name_date_format + unset s3cmd_options + unset s3cmd_config unset s3_access_key unset s3_secret_key - unset s3_access_token - unset s3_endpoint unset s3_region - unset s3cmd_config + unset s3_host + unset s3_host_bucket unset tar_options unset tar_exclude unset mysqldump_options diff --git a/src/lib/backup.sh b/src/lib/backup.sh index 463d2dc..dc52243 100644 --- a/src/lib/backup.sh +++ b/src/lib/backup.sh @@ -70,7 +70,7 @@ process_target() { ftp) handler='tgt_ftp';; sftp) handler='tgt_sftp';; rsync) handler='tgt_rsync';; - s3) handler='tgt_s3';; + s3) handler='tgt_s3cmd';; sj) handler='tgt_sj';; swift) handler='tgt_swift';; dav) handler='tgt_dav';; diff --git a/src/lib/handlers/targets/s3cmd.sh b/src/lib/handlers/targets/s3cmd.sh new file mode 100644 index 0000000..2be2cbd --- /dev/null +++ b/src/lib/handlers/targets/s3cmd.sh @@ -0,0 +1,83 @@ +#!/usr/bin/env bash + +# s3cmd.sh - upload files to S3 compatible object storage. +# Copyright (c) 2022 ge +# +# 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 . + +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 +} diff --git a/src/lib/lib.sh b/src/lib/lib.sh index e8e3ed2..3d63d85 100644 --- a/src/lib/lib.sh +++ b/src/lib/lib.sh @@ -27,6 +27,7 @@ LIBRARY="${LIBRARY:-./lib}" . "$LIBRARY/handlers/sources/mysqldump.sh" . "$LIBRARY/handlers/sources/pg_dump.sh" . "$LIBRARY/handlers/targets/cp.sh" +. "$LIBRARY/handlers/targets/s3cmd.sh" if [ -f "$LIBRARY/extra.sh" ]; then . "$LIBRARY/extra.sh"