]> gitweb.hhaalo.de Git - simple-backup.git/commitdiff
add log rotate
authorBastian Dehn <hhaalo@arcor.de>
Sun, 9 Aug 2026 10:20:54 +0000 (12:20 +0200)
committerBastian Dehn <hhaalo@arcor.de>
Sun, 9 Aug 2026 10:22:49 +0000 (12:22 +0200)
src/backup
tests/backup_tests.bats

index e6e522b67566342ca9d8c3484fc98cf02b0ad3d4..24daa6faf605720e9fd6c3ee3877b45c2406566a 100755 (executable)
@@ -27,6 +27,15 @@ get_backup_type()
        echo "none"
 }
 
+log_rotate()
+{
+       local logfile="$1"
+       local lines=
+       lines=$(wc --lines "$logfile" | awk '{print $1}')
+       [ "$lines" -lt "100" ] && return 0
+       savelog "$logfile"
+}
+
 log()
 {
        local config="$1"
@@ -42,6 +51,7 @@ log()
        logmsg="$datetime [$type] $msg"
        echo "ℹ️ INFO: $logmsg -> $logfile"
        echo "$logmsg" >> "$logfile"
+       log_rotate "$logfile"
 }
 
 exists_needed_commands()
index ac6eb494a44f468c41534828dc95d936f596c9c2..189c74b7b54af036dbe7b6a6d8237748a559c8c2 100755 (executable)
@@ -894,4 +894,28 @@ for type in none day week month; do
                --description "success log has logfile entry for type $type" \
                -- \
                write_log_entry "$type"
-done
\ No newline at end of file
+done
+
+# bats test_tags=backup:log
+@test "success no logrotate by 99 lines" {
+       mkdir --parents build/test
+       printf "logline%.0s\n" {1..99} > build/test/backup.log
+
+       run log_rotate "build/test/backup.log"
+
+       assert_success
+       [ -f "build/test/backup.log" ]
+       [ ! -f "build/test/backup.log.0" ]
+}
+
+# bats test_tags=backup:log
+@test "success logrotate by 100 lines" {
+       mkdir --parents build/test
+       printf "logline%.0s\n" {1..100} > build/test/backup.log
+
+       run log_rotate "build/test/backup.log"
+
+       assert_success
+       [ ! -f "build/test/backup.log" ]
+       [ -f "build/test/backup.log.0" ]
+}
\ No newline at end of file