From 1e92a40689d6f357bc1bebbbea8748ea135f55e1 Mon Sep 17 00:00:00 2001 From: Captain ALM Date: Sun, 23 Feb 2025 14:52:29 +0000 Subject: [PATCH] Add backup time support. --- backup-image/backup.sh | 5 +- backup-image/btime.sh | 57 +++++++++++++++ backup-image/copy.sh | 18 +++++ backup-image/move.sh | 2 + backup-image/remove.sh | 2 + backup-image/sync-all-btime.sh | 40 +++++++++++ backup-image/sync-btime.sh | 127 +++++++++++++++++++++++++++++++++ backup-image/sync.sh | 73 +------------------ backup-image/verify.sh | 14 +++- 9 files changed, 265 insertions(+), 73 deletions(-) create mode 100755 backup-image/btime.sh create mode 100755 backup-image/sync-all-btime.sh create mode 100755 backup-image/sync-btime.sh diff --git a/backup-image/backup.sh b/backup-image/backup.sh index f536c8b..f6cf112 100755 --- a/backup-image/backup.sh +++ b/backup-image/backup.sh @@ -10,11 +10,13 @@ echo "Press Enter to Start..."; read; mkdir -p /tmp/a; dd "if=/dev/$1" status=progress conv=sync,noerror | gzip -6 -c > "/mnt/parts/$2.part.gz"; +echo -n "$(date +%s)" > "/mnt/times/$2.time; echo "Calculating SHA512 sums..."; ((sha512sum "/dev/$1" > "/tmp/a/$1.sum") && dd "if=/tmp/a/$1.sum" bs=128 count=1 "of=/tmp/a/$1.hash" && touch "/tmp/a/1.complete") & ((sha512sum "/mnt/parts/$2.part.gz" > "/tmp/a/$2.part.gz.sum") && dd "if=/tmp/a/$2.part.gz.sum" bs=128 count=1 "of=/tmp/a/$2-gz.hash" && touch "/tmp/a/2.complete") & ((gunzip -c "/mnt/parts/$2.part.gz" | sha512sum > "/tmp/a/$2.part.sum") && dd "if=/tmp/a/$2.part.sum" bs=128 count=1 "of=/tmp/a/$2.hash" && touch "/tmp/a/3.complete") & -until [ -f "/tmp/a/1.complete" ] && [ -f "/tmp/a/2.complete" ] && [ -f "/tmp/a/3.complete" ]; do sleep 0.1; done; +((sha512sum "/mnt/times/$2.time" > "/tmp/a/$2.time.sum") && dd "if=/tmp/a/$2.time.sum" bs=128 count=1 "of=/tmp/a/$2-time.hash" && touch "/tmp/a/4.complete") & +until [ -f "/tmp/a/1.complete" ] && [ -f "/tmp/a/2.complete" ] && [ -f "/tmp/a/3.complete" ] && [ -f "/tmp/a/4.complete" ]; do sleep 0.1; done; echo "Hashes:"; cat /tmp/a/*.sum; echo; @@ -25,6 +27,7 @@ if [ $? -ne 0 ]; then fi cp -f "/tmp/a/$2-gz.hash" "/mnt/hsums/$2-gz.hash"; cp -f "/tmp/a/$2.hash" "/mnt/hsums/$2.hash"; +cp -f "/tmp/a/$2-time.hash" "/mnt/hsums/$2-time.hash"; echo; echo "Backing up partition table..."; sfdisk -d "/dev/$1" > "/mnt/tabls/$2.tbl"; diff --git a/backup-image/btime.sh b/backup-image/btime.sh new file mode 100755 index 0000000..69bcac2 --- /dev/null +++ b/backup-image/btime.sh @@ -0,0 +1,57 @@ +#!/bin/bash +echo "Backup Time!"; +if [ $# -eq 0 ]; then + echo "Usage: btime.sh [Compare from disk root path]"; + echo "Exit code:"; + echo "0 : Same Time" + echo "1 : Compare to newer" + echo "2 : Compare from newer" + echo "3 : Compare to non-existent" + echo "4 : Compare from non-existent" + echo "5 : Command Failed" + exit 5; +fi +ssrc=""; +if [ -z $3 ]; then + ssrc="/mnt"; +else + ssrc="$3"; +fi +echo "Compare disk name: $1"; +echo "Compare to disk root path: $2"; +echo "Compare from disk root path: $ssrc"; +if [[ "$4" != "ikwid" ]]; then + echo "Press Enter to Start..."; + read; +fi +echo "Comparing..."; +tostamp=0; +fromstamp=0; +if [ -f "$2/times/$1.time" ]; then + tostamp=$(cat "$2/times/$1.time"); +elif [ -f "$2/parts/$1.part.gz" ]; then + tostamp=$(stat -c '%Y' "$2/parts/$1.part.gz"); +else + echo "Compare to non-existent!" + exit 3; +fi +echo "Compare to: $tostamp"; +if [ -f "$ssrc/times/$1.time" ]; then + fromstamp=$(cat "$ssrc/times/$1.time"); +elif [ -f "$ssrc/parts/$1.part.gz" ]; then + fromstamp=$(stat -c '%Y' "$ssrc/parts/$1.part.gz"); +else + echo "Compare from non-existent!" + exit 4; +fi +echo "Compare from: $fromstamp"; +if [ "$tostamp" -gt "$fromstamp" ]; then + echo "Compare to newer!" + exit 1; +elif [ "$tostamp" -lt "$fromstamp" ]; then + echo "Compare from newer!" + exit 2; +else + echo "Same Time!" + exit 0; +fi diff --git a/backup-image/copy.sh b/backup-image/copy.sh index 3dead94..54a1c93 100755 --- a/backup-image/copy.sh +++ b/backup-image/copy.sh @@ -16,12 +16,30 @@ echo "Storage disk root path: $ssrc"; echo "Press Enter to Start..."; read; echo "Copying..."; +hST=0; +if [ -f "$ssrc/times/$1.time" ]; then + cp -f "$ssrc/times/$1.time" "$ssrc/times/$2.time"; + cp -f "$ssrc/hsums/$1-time.hash" "$ssrc/hsums/$2-time.hash"; + hST=1; +fi cp -f "$ssrc/hsums/$1-gz.hash" "$ssrc/hsums/$2-gz.hash"; cp -f "$ssrc/hsums/$1-tbl.hash" "$ssrc/hsums/$2-tbl.hash"; cp -f "$ssrc/hsums/$1.hash" "$ssrc/hsums/$2.hash"; cp -f "$ssrc/parts/$1.part.gz" "$ssrc/parts/$2.part.gz"; cp -f "$ssrc/tabls/$1.tbl" "$ssrc/tabls/$2.tbl"; echo "Verifying..."; +if [ $hST -eq 1 ]; then + cmp -s "$ssrc/hsums/$1-time.hash" "$ssrc/hsums/$2-time.hash"; + if [ $? -ne 0 ]; then + echo "Byte mismatch with $1-time.hash !"; + exit 1; + fi + cmp -s "$ssrc/times/$1.time" "$ssrc/times/$2.time"; + if [ $? -ne 0 ]; then + echo "Byte mismatch with $1.time !"; + exit 1; + fi +fi cmp -s "$ssrc/hsums/$1-gz.hash" "$ssrc/hsums/$2-gz.hash"; if [ $? -ne 0 ]; then echo "Byte mismatch with $1-gz.hash !"; diff --git a/backup-image/move.sh b/backup-image/move.sh index e484444..070bcc6 100755 --- a/backup-image/move.sh +++ b/backup-image/move.sh @@ -21,5 +21,7 @@ mv -f "$ssrc/hsums/$1-tbl.hash" "$ssrc/hsums/$2-tbl.hash"; mv -f "$ssrc/hsums/$1.hash" "$ssrc/hsums/$2.hash"; mv -f "$ssrc/parts/$1.part.gz" "$ssrc/parts/$2.part.gz"; mv -f "$ssrc/tabls/$1.tbl" "$ssrc/tabls/$2.tbl"; +mv -f "$ssrc/times/$1.time" "$ssrc/times/$2.time"; +mv -f "$ssrc/hsums/$1-time.hash" "$ssrc/hsums/$2-time.hash"; echo "Finished!"; exit 0; diff --git a/backup-image/remove.sh b/backup-image/remove.sh index 336afad..38fb8e7 100755 --- a/backup-image/remove.sh +++ b/backup-image/remove.sh @@ -20,5 +20,7 @@ rm -f "$ssrc/hsums/$1-tbl.hash"; rm -f "$ssrc/hsums/$1.hash"; rm -f "$ssrc/parts/$1.part.gz"; rm -f "$ssrc/tabls/$1.tbl"; +rm -f "$ssrc/times/$1.time"; +rm -f "$ssrc/hsums/$1-time.hash"; echo "Finished!"; exit 0; diff --git a/backup-image/sync-all-btime.sh b/backup-image/sync-all-btime.sh new file mode 100755 index 0000000..d5f11f3 --- /dev/null +++ b/backup-image/sync-all-btime.sh @@ -0,0 +1,40 @@ +#/bin/bash +echo "Sync All Backups!"; +if [ $# -eq 0 ]; then + echo "Usage: sync-all-btime.sh [o/n/i] [Source disk root path]"; + echo "o : Replace if source older" + echo "n : Replace if source newer" + echo "i : Ignore backup times" + exit 2; +fi +ssrc=""; +if [ -z $3 ]; then + ssrc="/mnt"; +else + ssrc="$3"; +fi +echo "Target disk root path: $2"; +echo "Source disk root path: $ssrc"; +if [[ "$1" == "o" ]]; then + echo "Targets will be replaced if source older."; +elif [[ "$1" == "n" ]]; then + echo "Targets will be replaced if source newer."; +else + echo "Targets will be replaced if different." +fi +if [[ "$4" != "ikwid" ]]; then + echo "Press Enter to Start..."; + read; +fi +rval=0; +for cf in $ssrc/tabls/*; do + if [ -e "$cf" ] && [ -f "$cf" ]; then + ./sync-btime.sh "$1" $(basename "$cf" ".tbl") "$2" "$ssrc" "ikwid"; + if [ $? -ne 0 ]; then + echo "Sync failed: $cf"; + rval=1; + fi + fi +done +echo "Finished!"; +exit $rval; diff --git a/backup-image/sync-btime.sh b/backup-image/sync-btime.sh new file mode 100755 index 0000000..6a553dc --- /dev/null +++ b/backup-image/sync-btime.sh @@ -0,0 +1,127 @@ +#!/bin/bash +echo "Sync Backup!"; +if [ $# -eq 0 ]; then + echo "Usage: sync-btime.sh [o/n/i] [Sync source disk root path]"; + echo "o : Replace if source older" + echo "n : Replace if source newer" + echo "i : Ignore backup times" + exit 2; +fi +ssrc=""; +if [ -z $4 ]; then + ssrc="/mnt"; +else + ssrc="$4"; +fi +echo "Source Backup disk name: $2"; +echo "Sync target disk root path: $3"; +echo "Sync source disk root path: $ssrc"; +if [[ "$1" == "o" ]]; then + echo "Target will be replaced if source older."; +elif [[ "$1" == "n" ]]; then + echo "Target will be replaced if source newer."; +else + echo "Target will be replaced if different." +fi +if [[ "$5" != "ikwid" ]]; then + echo "Press Enter to Start..."; + read; +fi +mkdir -p /tmp/a; +echo "Syncing..."; +cmp -s "$ssrc/hsums/$2-tbl.hash" "$3/hsums/$2-tbl.hash" +sTbl=$?; +cmp -s "$ssrc/hsums/$2-gz.hash" "$3/hsums/$2-gz.hash"; +sPart=$?; +if [ $sPart -eq 0 ]; then + cmp -s "$ssrc/hsums/$2.hash" "$3/hsums/$2.hash"; + sPart=$?; +fi +if [ $sTbl -ne 0 ] || [ $sPart -ne 0 ]; then + if [[ "$1" == "o" ]]; then + ./btime.sh "$2" "$3" "$4" "ikwid"; + sComp=$?; + if [ $? -ne 1 ]; then + sPart=0; + sTbl=0; + fi + elif [[ "$1" == "n" ]]; then + ./btime.sh "$2" "$3" "$4" "ikwid"; + sComp=$?; + if [ $? -ne 2 ]; then + sPart=0; + sTbl=0; + fi + fi +fi +sO=0; +if [ $sTbl -ne 0 ]; then + echo "Syncing: $2.tbl"; + cp -f "$ssrc/tabls/$2.tbl" "$3/tabls/$2.tbl"; + cp -f "$ssrc/hsums/$2-tbl.hash" "$3/hsums/$2-tbl.hash"; + sO=1; +fi +if [ $sPart -ne 0 ]; then + echo "Syncing: $2.part.gz"; + cp -f "$ssrc/hsums/$2-gz.hash" "$3/hsums/$2-gz.hash"; + cp -f "$ssrc/hsums/$2.hash" "$3/hsums/$2.hash"; + cp -f "$ssrc/parts/$2.part.gz" "$3/parts/$2.part.gz"; + sO=1; +fi +hT=0; +if [ $sO -eq 1 ] && [ -f "$ssrc/times/$2.time" ]; then + echo "Syncing: $2.time"; + cp -f "$ssrc/hsums/$2-time.hash" "$3/hsums/$2-time.hash"; + cp -f "$ssrc/times/$2.time" "$3/times/$2.time"; + hT=1; +fi +echo "Verifying..."; +if [ $sTbl -ne 0 ]; then + echo "Verifying: $2.tbl"; + cmp -s "$ssrc/hsums/$2-tbl.hash" "$3/hsums/$2-tbl.hash"; + if [ $? -ne 0 ]; then + echo "Hash mismatch with $2-tbl.hash !"; + exit 1; + fi + (sha512sum "$3/tabls/$2.tbl" > "/tmp/a/$2.tbl.sum") && dd "if=/tmp/a/$2.tbl.sum" bs=128 count=1 "of=/tmp/a/$2-tbl.hash"; + cmp -s "/tmp/a/$2-tbl.hash" "$3/hsums/$2-tbl.hash"; + if [ $? -ne 0 ]; then + echo "Hash mismatch with $2.tbl !"; + exit 1; + fi +fi +if [ $sPart -ne 0 ]; then + echo "Verifying: $2.part.gz"; + cmp -s "$ssrc/hsums/$2-gz.hash" "$3/hsums/$2-gz.hash"; + if [ $? -ne 0 ]; then + echo "Hash mismatch with $2-gz.hash !"; + exit 1; + fi + cmp -s "$ssrc/hsums/$2.hash" "$3/hsums/$2.hash"; + if [ $? -ne 0 ]; then + echo "Hash mismatch with $2.hash !"; + exit 1; + fi + (sha512sum "$3/parts/$2.part.gz" > "/tmp/a/$2.part.gz.sum") && dd "if=/tmp/a/$2.part.gz.sum" bs=128 count=1 "of=/tmp/a/$2-gz.hash"; + cmp -s "/tmp/a/$2-gz.hash" "$3/hsums/$2-gz.hash"; + if [ $? -ne 0 ]; then + echo "Hash mismatch with $2.part.gz !"; + exit 1; + fi +fi +if [ $hT -eq 1 ]; then + echo "Verifying: $2.time"; + cmp -s "$ssrc/hsums/$2-time.hash" "$3/hsums/$2-time.hash"; + if [ $? -ne 0 ]; then + echo "Byte mismatch with $2-time.hash !"; + exit 1; + fi + cmp -s "$ssrc/times/$2.time" "$3/times/$2.time"; + if [ $? -ne 0 ]; then + echo "Byte mismatch with $2.time !"; + exit 1; + fi +fi +rm -rf /tmp/a; +echo "Finished!"; +exit 0; diff --git a/backup-image/sync.sh b/backup-image/sync.sh index 32679ad..29dbdc7 100755 --- a/backup-image/sync.sh +++ b/backup-image/sync.sh @@ -4,74 +4,5 @@ if [ $# -eq 0 ]; then echo "Usage: sync.sh [Sync source disk root path]"; exit 2; fi -ssrc=""; -if [ -z $3 ]; then - ssrc="/mnt"; -else - ssrc="$3"; -fi -echo "Source Backup disk name: $1"; -echo "Sync target disk root path: $2"; -echo "Sync source disk root path: $ssrc"; -if [[ "$4" != "ikwid" ]]; then - echo "Press Enter to Start..."; - read; -fi -mkdir -p /tmp/a; -echo "Syncing..."; -cmp -s "$ssrc/hsums/$1-tbl.hash" "$2/hsums/$1-tbl.hash" -sTbl=$?; -cmp -s "$ssrc/hsums/$1-gz.hash" "$2/hsums/$1-gz.hash"; -sPart=$?; -if [ $sPart -eq 0 ]; then - cmp -s "$ssrc/hsums/$1.hash" "$2/hsums/$1.hash"; - sPart=$?; -fi -if [ $sTbl -ne 0 ]; then - echo "Syncing: $1.tbl"; - cp -f "$ssrc/tabls/$1.tbl" "$2/tabls/$1.tbl"; - cp -f "$ssrc/hsums/$1-tbl.hash" "$2/hsums/$1-tbl.hash"; -fi -if [ $sPart -ne 0 ]; then - echo "Syncing: $1.part.gz"; - cp -f "$ssrc/hsums/$1-gz.hash" "$2/hsums/$1-gz.hash"; - cp -f "$ssrc/hsums/$1.hash" "$2/hsums/$1.hash"; - cp -f "$ssrc/parts/$1.part.gz" "$2/parts/$1.part.gz"; -fi -echo "Verifying..."; -if [ $sTbl -ne 0 ]; then - echo "Verifying: $1.tbl"; - cmp -s "$ssrc/hsums/$1-tbl.hash" "$2/hsums/$1-tbl.hash"; - if [ $? -ne 0 ]; then - echo "Hash mismatch with $1-tbl.hash !"; - exit 1; - fi - (sha512sum "$2/tabls/$1.tbl" > "/tmp/a/$1.tbl.sum") && dd "if=/tmp/a/$1.tbl.sum" bs=128 count=1 "of=/tmp/a/$1-tbl.hash"; - cmp -s "/tmp/a/$1-tbl.hash" "$2/hsums/$1-tbl.hash"; - if [ $? -ne 0 ]; then - echo "Hash mismatch with $1.tbl !"; - exit 1; - fi -fi -if [ $sPart -ne 0 ]; then - echo "Verifying: $1.part.gz"; - cmp -s "$ssrc/hsums/$1-gz.hash" "$2/hsums/$1-gz.hash"; - if [ $? -ne 0 ]; then - echo "Hash mismatch with $1-gz.hash !"; - exit 1; - fi - cmp -s "$ssrc/hsums/$1.hash" "$2/hsums/$1.hash"; - if [ $? -ne 0 ]; then - echo "Hash mismatch with $1.hash !"; - exit 1; - fi - (sha512sum "$2/parts/$1.part.gz" > "/tmp/a/$1.part.gz.sum") && dd "if=/tmp/a/$1.part.gz.sum" bs=128 count=1 "of=/tmp/a/$1-gz.hash"; - cmp -s "/tmp/a/$1-gz.hash" "$2/hsums/$1-gz.hash"; - if [ $? -ne 0 ]; then - echo "Hash mismatch with $1.part.gz !"; - exit 1; - fi -fi -rm -rf /tmp/a; -echo "Finished!"; -exit 0; +./sync-btime.sh i "$1" "$2" "$3" "ikwid"; +return $?; diff --git a/backup-image/verify.sh b/backup-image/verify.sh index e91a620..517bd96 100755 --- a/backup-image/verify.sh +++ b/backup-image/verify.sh @@ -21,7 +21,12 @@ echo "Verifying..."; echo "Calculating SHA512 sums..."; ((sha512sum "$ssrc/parts/$1.part.gz" > "/tmp/a/$1.part.gz.sum") && dd "if=/tmp/a/$1.part.gz.sum" bs=128 count=1 "of=/tmp/a/$1-gz.hash" && touch "/tmp/a/1.complete") & ((sha512sum "$ssrc/tabls/$1.tbl" > "/tmp/a/$1.tbl.sum") && dd "if=/tmp/a/$1.tbl.sum" bs=128 count=1 "of=/tmp/a/$1-tbl.hash" && touch "/tmp/a/2.complete") & -until [ -f "/tmp/a/1.complete" ] && [ -f "/tmp/a/2.complete" ]; do sleep 0.1; done; +if [ -f "$ssrc/times/$1.time" ]; then + ((sha512sum "$ssrc/times/$1.time" > "/tmp/a/$1.time.sum") && dd "if=/tmp/a/$1.time.sum" bs=128 count=1 "of=/tmp/a/$1-time.hash" && touch "/tmp/a/3.complete") & +else + touch "/tmp/a/3.complete"; +fi +until [ -f "/tmp/a/1.complete" ] && [ -f "/tmp/a/2.complete" ] && [ -f "/tmp/a/3.complete" ]; do sleep 0.1; done; echo "Hashes:"; cat /tmp/a/*.sum; echo; @@ -35,6 +40,13 @@ if [ $? -ne 0 ]; then echo "Hash mismatch with $1.tbl !"; exit 1; fi +if [ -f "$ssrc/times/$1.time" ]; then + cmp -s "/tmp/a/$1-time.hash" "$ssrc/hsums/$1-time.hash"; + if [ $? -ne 0 ]; then + echo "Hash mismatch with $1.time !"; + exit 1; + fi +fi rm -rf /tmp/a; echo "Finished!"; exit 0;