]> gitweb.hhaalo.de Git - simple-backup.git/commitdiff
change history with split per line and delete log
authorBastian Dehn <hhaalo@arcor.de>
Wed, 28 May 2025 17:16:47 +0000 (19:16 +0200)
committerBastian Dehn <hhaalo@arcor.de>
Wed, 28 May 2025 17:32:06 +0000 (19:32 +0200)
backup

diff --git a/backup b/backup
index 6edd723764d98a85ecc8c6993acafe97045f7474..1677ae81b8dd7284ccc42e33a9b5d3a0662eb8ba 100755 (executable)
--- a/backup
+++ b/backup
@@ -10,6 +10,54 @@ errlog()
        return $lastexit
 }
 
+get_backup_type()
+{
+       local typ="none"
+
+       [ -f "$backup_path/day" ] && typ="day"
+       [ -f "$backup_path/week" ] && typ="week"
+       [ -f "$backup_path/month" ] && typ="month"
+
+       echo "$typ"
+}
+
+write_current_date()
+{
+       local backup_path="$1"
+       local current_date="$(date +%F)"
+       local typ=$(get_backup_type)
+
+       mkdir --parents "history"
+
+       echo "$current_date" > $backup_path/$typ
+       echo "$current_date" > "history/$typ"
+}
+
+rotate_history()
+{
+       local history_file="$1"
+
+       [ -f "$history_file" ] \
+               && history_lines=$(cat $history_file | wc -l)
+       echo "ℹ️  INFO: $history_file has $history_lines lines"
+       [ $history_lines -ge 100 ] \
+               && savelog -c 7 -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)
+
+       mkdir --parents "history"
+       rotate_history "history/history"
+
+       echo "ℹ️  INFO: $current_date $typ $msg"
+       echo "$current_date $typ $msg" >> "history/history"
+}
+
 get_backup_dir()
 {
        local backup_path="$1"
@@ -148,6 +196,7 @@ remove_backup_dir()
        for backup in $backups; do
                echo "🗑️  DELETE: $backup"
                rm --recursive $backup
+               write_history_entry "deleted ${backup##*/}"
        done
 }
 
@@ -177,38 +226,6 @@ cleanup_backups()
        print_line
 }
 
-make_history()
-{
-       local backup_path="$1"
-       local backup_dir="$2"
-       local backup_history_file="history"
-       local date="$(date +%F)"
-       local time="$(date +%T)"
-       local history_path="history"
-       local typ="none"
-       local history_size=0
-
-       [ -f "$backup_path/day" ] && typ="day"
-       [ -f "$backup_path/week" ] && typ="week"
-       [ -f "$backup_path/month" ] && typ="month"
-
-       mkdir --parents "$history_path"
-
-       [ -f "$history_path/$backup_history_file" ] \
-               && history_size=$(cat $history_path/$backup_history_file \
-               | wc -c)
-       echo "ℹ️  INFO: $history_path/$backup_history_file size $history_size bytes"
-       [ $history_size -ge 1024 ] \
-               && savelog -c 7 -l $history_path/$backup_history_file \
-               && echo "ℹ️  INFO: $history_path/$backup_history_file rotate"
-
-       echo "ℹ️  INFO: add history $date $time $typ $backup_dir"
-       echo "$date" > $backup_path/$typ
-       echo "$date" > $history_path/$typ
-       echo "$date $time $typ $backup_dir" \
-               >> $history_path/$backup_history_file
-}
-
 is_backup_old()
 {
        local backup_typ="$1"
@@ -325,17 +342,17 @@ main()
                "$backup_full_path" \
                "$last_backup_dir_path" \
                || return $?
-       make_history \
-               "$backup_path" \
-               "$backup_dir" \
+       write_current_date "$backup_path" \
                || return $?
-       check_backup_date \
+       write_history_entry "created $backup_dir" \
                || return $?
        cleanup_backups \
                "$backup_path" \
                "$backup_name" \
                $backup_count \
                || return $?
+       check_backup_date \
+               || return $?
 
        echo "✅ SUCCESS: backup"
 }