Skip to content

Commit 1e393d6

Browse files
authored
Check that there are enough snapshots before diff (#144)
* Check that there are enough snapshots before diff * Implement review feedback Delete duplicate code and pull usage of the calculated stats into the conditional block.
1 parent 4158ee1 commit 1e393d6

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

bin/restic_backup.sh

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -170,14 +170,20 @@ if [[ -n "$RESTIC_BACKUP_STATS_DIR" || -n "$RESTIC_BACKUP_NOTIFICATION_FILE" ]];
170170
| awk '{print $1}' \
171171
| tail -2 \
172172
| tr '\n' ' ')
173-
latest_snapshot_diff=$(echo "$latest_snapshots" | xargs restic diff)
174-
added=$(echo "$latest_snapshot_diff" | grep -i 'added:' | awk '{print $2 " " $3}')
175-
removed=$(echo "$latest_snapshot_diff" | grep -i 'removed:' | awk '{print $2 " " $3}')
176-
snapshot_size=$(restic stats latest --tag "$RESTIC_BACKUP_TAG" | grep -i 'total size:' | cut -d ':' -f2 | xargs) # xargs acts as trim
177-
snapshotId=$(echo "$latest_snapshots" | cut -d ' ' -f2)
178-
statsMsg="Added: ${added}. Removed: ${removed}. Snap size: ${snapshot_size}"
179-
180-
echo "$statsMsg"
181-
test -n "$RESTIC_BACKUP_STATS_DIR" && logBackupStatsCsv "$snapshotId" "$added" "$removed" "$snapshot_size"
182-
test -n "$RESTIC_BACKUP_NOTIFICATION_FILE" && notifyBackupStats "$statsMsg"
173+
174+
snapshot_count=$(echo "$latest_snapshots" | wc -l)
175+
if [[ $snapshot_count -lt 2 ]]; then
176+
echo "Warning: $snapshot_count snapshot(s) found. Skipping diff stats (need at least 2 snapshots)."
177+
else
178+
latest_snapshot_diff=$(echo "$latest_snapshots" | xargs restic diff)
179+
added=$(echo "$latest_snapshot_diff" | grep -i 'added:' | awk '{print $2 " " $3}')
180+
removed=$(echo "$latest_snapshot_diff" | grep -i 'removed:' | awk '{print $2 " " $3}')
181+
snapshot_size=$(restic stats latest --tag "$RESTIC_BACKUP_TAG" | grep -i 'total size:' | cut -d ':' -f2 | xargs) # xargs acts as trim
182+
snapshotId=$(echo "$latest_snapshots" | cut -d ' ' -f2)
183+
statsMsg="Added: ${added}. Removed: ${removed}. Snap size: ${snapshot_size}"
184+
185+
echo "$statsMsg"
186+
test -n "$RESTIC_BACKUP_STATS_DIR" && logBackupStatsCsv "$snapshotId" "$added" "$removed" "$snapshot_size"
187+
test -n "$RESTIC_BACKUP_NOTIFICATION_FILE" && notifyBackupStats "$statsMsg"
188+
fi
183189
fi

0 commit comments

Comments
 (0)