]> gitweb.hhaalo.de Git - simple-backup.git/commitdiff
fix shellcheck issues
authorBastian Dehn <hhaalo@arcor.de>
Sat, 27 Jun 2026 15:25:09 +0000 (17:25 +0200)
committerBastian Dehn <hhaalo@arcor.de>
Sat, 27 Jun 2026 15:32:29 +0000 (17:32 +0200)
Makefile
src/backup
src/backupmount
src/restore

index da19a0423c9fb6b535b89527fb417d735f58297a..c7babd36a005fc572e0b77a55b6896bdaa56ee71 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -29,14 +29,17 @@ $(BUILDDIR)/$(PACKAGENAME)/DEBIAN/control:
 $(BINDIR)/backup:
        mkdir --parents $(@D)
        cp $(SRCDIR)/backup $(@)
+       shellcheck $(@)
 
 $(BINDIR)/backupmount:
        mkdir --parents $(@D)
        cp $(SRCDIR)/backupmount $(@)
+       shellcheck $(@)
 
 $(BINDIR)/restore:
        mkdir --parents $(@D)
        cp $(SRCDIR)/restore $(@)
+       shellcheck $(@)
 
 $(SHAREDIR)/config-example.yaml:
        mkdir --parents $(@D)
index e71dc1aa46020d7bdec82c08ecc676596f7973b5..57772112547b1a0815140de53f4b3a00ed58c5a6 100755 (executable)
@@ -24,12 +24,14 @@ get_backup_type()
 write_current_date()
 {
        local backup_path="$1"
-       local current_date="$(date +%F)"
-       local typ=$(get_backup_type)
+       local current_date=
+       local typ=
+       current_date="$(date +%F)"
+       typ=$(get_backup_type)
 
        mkdir --parents "history"
 
-       echo "$current_date" > $backup_path/$typ
+       echo "$current_date" > "$backup_path/$typ"
        echo "$current_date" > "history/$typ"
 }
 
@@ -39,18 +41,20 @@ rotate_history()
 
        [ -f "$history_file" ] || return 0
 
-       history_lines=$(cat $history_file | wc -l)
+       history_lines=$(wc -l "$history_file")
        echo "ℹ️  INFO: $history_file has $history_lines lines"
-       [ $history_lines -ge 100 ] \
-               && savelog -c 10 -l $history_file \
+       [ "$history_lines" -ge 100 ] \
+               && savelog -c 10 -l "$history_file" \
                && echo "ℹ️  INFO: $history_file rotated"
 }
 
 write_history_entry()
 {
        local msg="$1"
-       local current_date=$(date --iso-8601=seconds)
-       local typ=$(get_backup_type)
+       local current_date=
+       local typ=
+       current_date=$(date --iso-8601=seconds)
+       typ=$(get_backup_type)
 
        mkdir --parents "history"
        rotate_history "history/history"
@@ -64,24 +68,26 @@ get_backup_dir()
        local backup_path="$1"
        local backup_name="$2"
        local last_backup_number="$3"
-       local date=$(date +%F)
+       local date=
+       date=$(date +%F)
 
        local backup_dir=
        local number=
-       for number in $(seq --format "%02g" $last_backup_number 99); do
+       for number in $(seq --format "%02g" "$last_backup_number" 99); do
                backup_dir="$date"_"$number"_"$backup_name"
                [ -d "$backup_path/$backup_dir" ] || break;
        done
 
-       echo $backup_dir
+       echo "$backup_dir"
 }
 
 get_last_backup_dir_path()
 {
        local backup_path="$1"
        local backup_name="$2"
+       local last_backup_dir_path=
 
-       local last_backup_dir_path=$(find $backup_path \
+       last_backup_dir_path=$(find "$backup_path" \
                -maxdepth 1 \
                -type d \
                -name "*_""$backup_name" \
@@ -93,30 +99,27 @@ get_last_backup_dir_path()
 
 print_line()
 {
-       local i=
-       for i in {1..80}; do
-               printf "#"
-       done
-
+       printf "#%.0s" {1..80}
        printf "\n"
 }
 
 get_machine_excludes()
 {
        local machine="$1"
-       local excludes=$(cat config.yaml \
-               | yq --raw-output ".machines.\"$machine\".excludes[]" \
+       local excludes=
+       excludes=$(yq --raw-output ".machines.\"$machine\".excludes[]" \
+               config.yaml \
                2> /dev/null)
 
        [ "$excludes" == "null" ] && return
        [ -z "$excludes" ] && return
 
-       local options=""
+       local options=()
        for exclude in $excludes; do
-               options=$(echo "$options --exclude=$exclude")
+               options+=("--exclude=$exclude")
        done
 
-       echo "$options"
+       echo "${options[@]}"
 }
 
 backup_remote_pathes()
@@ -125,27 +128,29 @@ backup_remote_pathes()
        local machine="$2"
        local last_backup_dir_path="$3"
        local hardlinkopt=
-       local remote_user=$(cat config.yaml \
-               | yq --raw-output ".machines.\"$machine\".remote_user")
-       [ -n "$remote_user" ] && [ $remote_user != "null" ] \
+       local remote_user=
+       local host=
+       local port=
+       local pathes=
+       local excludes=
+       remote_user=$(yq \
+               --raw-output ".machines.\"$machine\".remote_user" config.yaml)
+       [ -n "$remote_user" ] && [ "$remote_user" != "null" ] \
                || errlog "config.yaml missing remote_user for $machine" \
                || return $?
-       local host=$(cat config.yaml \
-               | yq --raw-output ".machines.\"$machine\".host")
+       host=$(yq --raw-output ".machines.\"$machine\".host" config.yaml)
        [ -n "$host" ] && [ "$host" != "null" ] \
                || errlog "config.yaml missing host for $machine" \
                || return $?
-       local port=$(cat config.yaml \
-               | yq --raw-output ".machines.\"$machine\".port")
+       port=$(yq --raw-output ".machines.\"$machine\".port" config.yaml)
        [ -n "$port" ] && [ "$port" != "null" ] \
                || errlog "config.yaml missing port for $machine" \
                || return $?
-       local pathes=$(cat config.yaml \
-               | yq --raw-output ".machines.\"$machine\".pathes[]")
+       pathes=$(yq --raw-output ".machines.\"$machine\".pathes[]" config.yaml)
        [ -n "$pathes" ] \
                || errlog "config.yaml missing pathes for $machine" \
                || return $?
-       local excludes=$(get_machine_excludes "$machine")
+       excludes=$(get_machine_excludes "$machine")
 
        local YELLOW="\e[0;33m"
        local LIGHTPURPLE="\e[1;35m"
@@ -170,11 +175,11 @@ backup_remote_pathes()
                        --hard-links \
                        --relative \
                        --human-readable \
-                       $excludes \
-                       $hardlinkopt \
+                       "$excludes" \
+                       "$hardlinkopt" \
                        --rsh "ssh -p $port" \
-                       $hostpath \
-                       $backup_path/$machine \
+                       "$hostpath" \
+                       "$backup_path/$machine" \
                        || errlog "could not backup $machine with path $path" \
                        || return $?
 
@@ -187,7 +192,8 @@ backup_remotes()
        local backup_path="$1"
        local last_backup_dir_path="$2"
        local hardlinkopt=
-       local machines=$(cat config.yaml | yq --raw-output '.machines | keys[]')
+       local machines=
+       machines=$(yq --raw-output '.machines | keys[]' config.yaml)
        [ -n "$machines" ] \
                || errlog "config.yaml no machines" \
                || return $?
@@ -195,7 +201,7 @@ backup_remotes()
        local machine=
        for machine in $machines; do
                echo "ℹ️  INFO: create directory $backup_path/$machine"
-               mkdir --parents $backup_path/$machine \
+               mkdir --parents "$backup_path/$machine" \
                        || errlog "could not create directory $backup_path/$machine" \
                        || return $?
 
@@ -211,15 +217,16 @@ remove_backup_dir()
        local backup_path="$1"
        local backup_name="$2"
        local delete_count=$3
+       local backups=
 
-       local backups=$(find $backup_path -maxdepth 1 -name "*$backup_name" \
+       backups=$(find "$backup_path" -maxdepth 1 -name "*$backup_name" \
                | sort \
-               | head --lines $delete_count)
+               | head --lines "$delete_count")
 
        local backup=
        for backup in $backups; do
                echo "🗑️  DELETE: $backup"
-               rm --recursive $backup
+               rm --recursive "$backup"
                write_history_entry "deleted ${backup##*/}"
        done
 }
@@ -229,8 +236,9 @@ cleanup_backups()
        local backup_path="$1"
        local backup_name="$2"
        local backup_count=$3
+       local backup_counted=
 
-       local backup_counted=$(ls --directory $backup_path/*/ \
+       backup_counted=$(find "$backup_path" -max-depth 1 -type d \
                | grep "_$backup_name" \
                | sort \
                | wc --lines)
@@ -240,7 +248,7 @@ cleanup_backups()
        echo
        echo "ℹ️  INFO: backup count: $backup_counted"
 
-       if [ $backup_counted -gt $backup_count ]; then
+       if [ "$backup_counted" -gt "$backup_count" ]; then
                echo "ℹ️  INFO: delete backup count: $delete_count"
                echo
                remove_backup_dir "$backup_path" "$backup_name" $delete_count
@@ -254,14 +262,17 @@ is_backup_old()
 {
        local backup_typ="$1"
        local backup_date="$2"
-       local backup_month=$(date --date="$backup_date" '+%Y-%m')
-       local now_month=$(date '+%Y-%m')
-       local diff=$(( ($(date --date="$backup_date" +%s) - $(date +%s)) / 86400 ))
+       local backup_month=
+       local now_month=
+       local diff=
+       backup_month=$(date --date="$backup_date" '+%Y-%m')
+       now_month=$(date '+%Y-%m')
+       diff=$(( ($(date --date="$backup_date" +%s) - $(date +%s)) / 86400 ))
 
        case "$backup_typ" in
-       "day") [ $diff -ge 0 ] || return $? ;;
-       "week") [ $diff -ge -6 ] || return $? ;;
-       "month") [ "$backup_month" == "$now_month" ] || return $? ;;
+       "day") [ $diff -ge 0 ] || return 1 ;;
+       "week") [ $diff -ge -6 ] || return 1 ;;
+       "month") [ "$backup_month" == "$now_month" ] || return 1 ;;
        *) return 0;;
        esac
 }
@@ -280,13 +291,13 @@ check_backup_date()
        for typ in day week month; do
                if [ -f "$history_path/$typ" ]; then
                        color=$LIGHTGREEN
-                       is_backup_old $typ $(cat $history_path/$typ) \
+                       is_backup_old "$typ" "$(cat $history_path/$typ)" \
                                || color=$LIGHTRED
                        printf "${color}last backup %s " "$typ"
-                       printf "am %s${RESET}\n" "$(cat $history_path/$typ)"
+                       printf "am %s%s\n" "$(cat $history_path/$typ)" "$RESET"
                else
                        printf "${LIGHTRED}no backup %s " $typ
-                       printf "exists${RESET}\n"
+                       printf "exists%s}\n" "$RESET"
                fi
        done
        echo
@@ -296,6 +307,9 @@ check_backup_date()
 main()
 {
        local only_check="$1"
+       local last_backup_dir_path=
+       local today_date=
+       local backup_dir=
 
        [ "$only_check" == "check" ] && check_backup_date && return 0
 
@@ -314,9 +328,9 @@ main()
                || errlog "file config.yaml not found" \
                || return $?
 
-       backup_path=$(cat config.yaml | yq --raw-output '.backup_path')
-       backup_name=$(cat config.yaml | yq --raw-output '.backup_name')
-       backup_count=$(cat config.yaml | yq --raw-output '.backup_count')
+       backup_path=$(yq --raw-output '.backup_path' config.yaml)
+       backup_name=$(yq --raw-output '.backup_name' config.yaml)
+       backup_count=$(yq --raw-output '.backup_count' config.yaml)
 
        [ -n "$backup_path" ] && [ "$backup_path" != "null" ] \
                || errlog "config.yaml missing backup_path" \
@@ -334,17 +348,17 @@ main()
                || return $?
        echo "ℹ️  INFO: backup count $backup_count"
 
-       local last_backup_dir_path=$(get_last_backup_dir_path \
+       last_backup_dir_path=$(get_last_backup_dir_path \
                "$backup_path" \
                "$backup_name")
        echo "ℹ️  INFO: last backup dir path $last_backup_dir_path"
 
-       local today_date=$(date +%F)
+       today_date=$(date +%F)
        local last_backup_number=1
-       [[ "$last_backup_dir_path" =~ "$today_date" ]] \
+       [[ "$last_backup_dir_path" =~ $today_date ]] \
                && last_backup_number=${last_backup_dir_path##*/} \
                && last_backup_number=${last_backup_number:11:2}
-       local backup_dir=$(get_backup_dir \
+       backup_dir=$(get_backup_dir \
                "$backup_path" \
                "$backup_name" \
                "$last_backup_number")
@@ -364,7 +378,7 @@ main()
        cleanup_backups \
                "$backup_path" \
                "$backup_name" \
-               $backup_count \
+               "$backup_count" \
                || return $?
        check_backup_date \
                || return $?
@@ -372,4 +386,4 @@ main()
        echo "✅ SUCCESS: backup"
 }
 
-main $*
+main "$*"
index 8bb65d7d3103aa483a8b953a4f69867df283edf3..f547491674c09aba8a7e0a7d5a958f776369d1f7 100755 (executable)
@@ -22,16 +22,16 @@ infolog()
 partition_mount()
 {
        local media="$1"
-       local partition=$(cat config.yaml \
-               | yq --raw-output ".media.\"$media\".partition")
+       local partition=
+       local backup_path=
+       partition=$(yq --raw-output ".media.\"$media\".partition" config.yaml)
        [ "$partition" != "null" ] \
                || errlog "config.yaml partition for $media missing" \
                || return $?
        [ -b "$partition" ] \
                || errlog "partition $partition not found" \
                || return $?
-       local backup_path=$(cat config.yaml \
-               | yq --raw-output ".backup_path")
+       backup_path=$(yq --raw-output ".backup_path" config.yaml)
        [ "$backup_path" != "null" ] \
                || errlog "config.yaml backup path missing" \
                || return $?
@@ -50,13 +50,14 @@ device_mount()
 {
        local media="$1"
        local device="$2"
+       local key=
+       local volume=
 
        [ -b "$device" ] \
                || infolog "block device $device for $media not found" \
                || return $?
        infolog "media $media with device $device found"
-       local key=$(cat "config.yaml" \
-               | yq --raw-output ".media.\"$media\".key")
+       key=$(yq --raw-output ".media.\"$media\".key" config.yaml)
        [ "$key" != "null" ] \
                || errlog "config.yaml key for $media missing" \
                || return $?
@@ -64,8 +65,7 @@ device_mount()
                || errlog "key $key not exists" \
                || return $?
        infolog "use key $key"
-       local volume=$(cat "config.yaml" \
-               | yq --raw-output ".backup_volume")
+       volume=$(yq --raw-output ".backup_volume" config.yaml)
        [ "$volume" != "null" ] \
                || errlog "config.yaml volume for $media missing" \
                || return $?
@@ -82,8 +82,8 @@ device_mount()
 
 mount_backup()
 {
-       local medias=$(cat config.yaml \
-               | yq --raw-output '.media | keys[]')
+       local medias=
+       medias=$(yq --raw-output '.media | keys[]' config.yaml)
 
        [ -n "$medias" ] && [ "$medias" != "null" ] \
                || errlog "config.yaml missing media section" \
@@ -92,8 +92,7 @@ mount_backup()
        local media=
        local device=
        for media in $medias; do
-               device=$(cat config.yaml \
-                       | yq --raw-output ".media.\"$media\".device")
+               device=$(yq --raw-output ".media.\"$media\".device" config.yaml)
                [ "$device" != "null" ] \
                        || errlog "missing path for $media" \
                        || return $?
@@ -105,9 +104,10 @@ mount_backup()
 
 umount_backup()
 {
+       local backup_path=
+       local volume=
 
-       local backup_path=$(cat config.yaml \
-               | yq --raw-output ".backup_path")
+       backup_path=$(yq --raw-output ".backup_path" config.yaml)
        [ "$backup_path" != "null" ] \
                || errlog "config.yaml backup path missing" \
                || return $?
@@ -122,8 +122,7 @@ umount_backup()
                || errlog "clound not remote $backup_path" \
                || return $?
 
-       local volume=$(cat "config.yaml" \
-               | yq --raw-output ".backup_volume")
+       volume=$(yq --raw-output ".backup_volume" config.yaml)
        [ "$volume" != "null" ] \
                || errlog "config.yaml volume for $media missing" \
                || return $?
@@ -165,4 +164,4 @@ main()
        echo "✅ SUCCESS: backupmount"
 }
 
-main $*
+main "$*"
index c411919c42fa68d7e6b981f2da0efcd9c8cc6cb8..89c65f6dd200812df5da0b980b492b2cd87c21f6 100755 (executable)
@@ -24,10 +24,7 @@ useage()
 
 print_line()
 {
-       for i in {1..80}; do
-               printf "#"
-       done
-
+       printf "#%.0s" {1..80}
        printf "\n"
 }
 
@@ -38,20 +35,22 @@ restore()
        local machine="$3"
        local path="$4"
        local origin="$5"
-
-       local backup_path=$(cat config.yaml \
-               | yq --raw-output ".backup_path")
-       local backup_name=$(cat config.yaml \
-               | yq --raw-output ".backup_name")
-       local machine=$(cat config.yaml \
-               | yq --raw-output \
-               ".machines | keys[] | select(. == \"$machine\")")
-       local remote_user=$(cat config.yaml \
-               | yq --raw-output ".machines.\"$machine\".remote_user")
-       local remote_machine=$(cat config.yaml \
-               | yq --raw-output ".machines.\"$machine\".host")
-       local remote_port=$(cat config.yaml \
-               | yq --raw-output ".machines.\"$machine\".port")
+       local backup_path=
+       local backup_name=
+       local machine=
+       local remote_user=
+       local remote_machine=
+       local remote_port=
+
+       backup_path=$(yq --raw-output ".backup_path" config.yaml)
+       backup_name=$(yq --raw-output ".backup_name" config.yaml)
+       machine=$(yq --raw-output \
+               ".machines | keys[] | select(. == \"$machine\")" config.yaml)
+       remote_user=$(yq --raw-output ".machines.\"$machine\".remote_user" \
+               config.yaml)
+       remote_machine=$(yq --raw-output ".machines.\"$machine\".host" \
+               config.yaml)
+       remote_port=$(yq --raw-output ".machines.\"$machine\".port" config.yaml)
 
        [ -d "$backup_path" ] \
                || errlog "could not find directory $backup_path" \
@@ -105,10 +104,10 @@ restore()
                && echo "⚠️  WARN: delete mode on" && echo
 
        local ask_restore="N"
-       read -p "❓ QUESTION: Do you want restore? [N/y] " ask_restore
+       read -rp "❓ QUESTION: Do you want restore? [N/y] " ask_restore
        local ask_restore=${ask_restore:-N}
        local ask_restore=${ask_restore,,}
-       echo ${ask_restore,,}
+       echo "${ask_restore,,}"
 
        [ "$ask_restore" == "y" ] \
                || errlog "you want not do restore" \
@@ -122,10 +121,10 @@ restore()
                --acls \
                --hard-links \
                --human-readable \
-               $deletemode \
+               "$deletemode" \
                --rsh "ssh -p $remote_port" \
-               $restore_path \
-               $remote_path \
+               "$restore_path" \
+               "$remote_path" \
                || errlog "could not restore" \
                || return $?
 
@@ -138,12 +137,11 @@ main()
        local number=
        local machine=
        local path=
-       local key=
        local origin="deletemode-off"
 
        echo "⭐ START: restore"
 
-       while getopts "d:k:n:r:p:ho" optname; do
+       while getopts "d:n:r:p:ho" optname; do
                case "$optname" in
                "d") date="$OPTARG" ;;
                "n") number="$OPTARG" ;;
@@ -151,6 +149,7 @@ main()
                "p") path="$OPTARG" ;;
                "o") origin="deletemode-on" ;;
                "h") useage && return 0 ;;
+               *) usage && return 0 ;;
                esac
        done
 
@@ -181,4 +180,4 @@ main()
        echo "✅ SUCCESS: restore"
 }
 
-main $*
+main "$"*