Commit 50033dfb authored by Sven Greiner's avatar Sven Greiner
Browse files

Merge branch 'cleanup' into 'master'

Implement suggestions from shellcheck

See merge request !4
parents c0e8a433 aeddede6
......@@ -30,8 +30,8 @@ error() {
}
require_command() {
if [[ ! "$(which $1)" ]]; then
error "Prerequisite \"$1\" not found in PATH" $2
if [[ ! -x "$(command -v "$1")" ]]; then
error "Prerequisite \"$1\" not found in PATH" "$2"
fi
}
......@@ -74,14 +74,14 @@ check_prerequisites() {
fi
if wants_compression; then
require_command ${COMPRESS%% *}
require_command "${COMPRESS%% *}"
fi
if wants_encryption; then
require_command "gpg"
if [[ -n "$GPG_KEYID" ]]; then
gpg --list-keys $GPG_KEYID &> /dev/null \
gpg --list-keys "$GPG_KEYID" &> /dev/null \
|| error "GPG key \"$GPG_KEYID\" not available"
fi
fi
......@@ -120,9 +120,9 @@ set_umask() {
delete_old_backups() {
if [[ "$NUMBACKUPS" -gt 0 ]]; then
info "Delete local backups older than $NUMBACKUPS latest backups"
for dir in $(ls -t1 "$BACKUP_PATH" | tail -n +$NUMBACKUPS); do
for dir in $(ls -t1 "$BACKUP_PATH" | tail -n +"$NUMBACKUPS"); do
info "Delete backup \"$dir\""
rm -r "$BACKUP_PATH/$dir" &> /dev/null \
rm -r "${BACKUP_PATH:?}/${dir:?}" &> /dev/null \
|| error "Deletion of \"$dir\" failed."
done
fi
......@@ -148,24 +148,23 @@ backup () {
compression() {
if wants_tar || wants_compression; then
info "Starting compression"
cd "$backupdir"
cd "$backupdir" || error "Could not enter $backupdir"
# format: db.table.sql or db-schema-create.sql
dbs=$(ls *.sql | cut -d "." -f 1 | cut -d "-" -f 1 | sort | uniq)
dbs=$(ls ./*.sql | cut -d "." -f 1 | cut -d "-" -f 1 | sort | uniq)
for db in $dbs; do
info "Compressing: $db"
if wants_tar; then
tar cf "$db.tar" "$db"[.-]* \
&& rm "$db"[.-]*.sql
[[ $? -ne 0 ]] \
&& error "Tar of \"$db\" failed."
|| error "Tar of \"$db\" failed."
rm "$db"[.-]*.sql \
|| error "Removing SQL files after TAR failed for \"$db\"."
fi
if wants_compression; then
$COMPRESS "$db"[.-]*
[[ $? -ne 0 ]] \
&& error "Compression of \"$db\" failed."
$COMPRESS "$db"[.-]* \
|| error "Compression of \"$db\" failed."
fi
done
cd - &> /dev/null
cd - &> /dev/null || error "Could not return to workdir"
fi
}
......@@ -177,8 +176,7 @@ encryption() {
info "Encrypt using symmetric encryption"
fi
cd "$backupdir"
for i in *; do
for i in "${backupdir:?}/"*; do
if [[ -n "$GPG_KEYID" ]]; then
gpg --batch --encrypt --recipient "$GPG_KEYID" --trust-model always "$i" \
&& rm "$i"
......@@ -190,7 +188,6 @@ encryption() {
[[ $? -ne 0 ]] \
&& error "Encryption of \"$i\" failed."
done
cd - &> /dev/null
fi
}
......@@ -202,10 +199,8 @@ exec_ftp() {
[[ -n "$FTP_KEYFILE" ]] \
&& lftp_con_prog="set sftp:connect-program \"ssh -a -x -i $FTP_KEYFILE\""
lftp -c "$lftp_con_prog; set net:max-retries 3; open $lftp_port -u $FTP_USER,$FTP_PASS $FTP_TYPE://$FTP_HOST; $1" 2> /dev/null
[[ $? -ne 0 ]] \
&& error "Failed to execute command on remote server: $1"
lftp -c "$lftp_con_prog; set net:max-retries 3; open $lftp_port -u $FTP_USER,$FTP_PASS $FTP_TYPE://$FTP_HOST; $1" 2> /dev/null \
|| error "Failed to execute command on remote server: $1"
}
upload() {
......@@ -215,19 +210,18 @@ upload() {
# FTP: delete old backups
info "Delete old backups on FTP..."
ftpfiles=$(exec_ftp "cd \"$FTP_DIR\"; nlist; exit" | sort)
## filter files starting with a dot
ftpfiles=( $(echo $ftpfiles | sed 's,\.[A-Za-z0-9_\/\.]* , ,g') )
## retrieve and filter filenames starting with a dot
mapfile -t ftpfiles < <(exec_ftp "cd \"$FTP_DIR\"; nlist; exit" | grep -v "^\." | sort)
## find old backups
ftpfilesdelete=()
while [[ ${#ftpfiles[@]} -ge $FTP_NUMBACKUPS ]]; do
ftpfilesdelete+=(${ftpfiles[0]})
ftpfilesdelete+=("${ftpfiles[0]}")
ftpfiles=("${ftpfiles[@]:1}")
done
## delete on ftp
[[ ${#ftpfilesdelete[@]} -ge 1 ]] \
&& ftpfilesdelete=${ftpfilesdelete[@]} \
&& exec_ftp "cd \"$FTP_DIR\"; rm -r $ftpfilesdelete; exit"
&& todelete=${ftpfilesdelete[*]} \
&& exec_ftp "cd \"$FTP_DIR\"; rm -r $todelete; exit"
# FTP: upload
info "Upload to FTP..."
......@@ -236,7 +230,7 @@ upload() {
}
# get config
config_file=$(dirname $0)/"myftpdumper.conf"
config_file=$(dirname "$0")/"myftpdumper.conf"
if [[ $# -gt 0 ]]; then
config_file=$1
fi
......@@ -244,6 +238,7 @@ if [[ ! -f "$config_file" ]]; then
echo -e "USAGE: $0 path/to/config \nOr myftpdumper.conf file in same directory" >&2
exit 1
fi
# shellcheck source=/dev/null
source "$config_file"
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment