From 837d75d00339a7bd38bbca8c5fdddaf39af1b283 Mon Sep 17 00:00:00 2001 From: Captain ALM Date: Fri, 30 Aug 2024 11:15:29 +0100 Subject: [PATCH] Add more backup management scripts. --- backup-image/copy.sh | 51 +++++++++++++++++++++++++++++++++++++++++ backup-image/move.sh | 25 ++++++++++++++++++++ backup-image/remove.sh | 24 +++++++++++++++++++ backup-image/restore.sh | 31 +++++++++++++++++++++++++ backup-image/sync.sh | 28 +++++++++++++--------- backup-image/verify.sh | 38 ++++++++++++++++++++++++++++++ 6 files changed, 186 insertions(+), 11 deletions(-) create mode 100755 backup-image/copy.sh create mode 100755 backup-image/move.sh create mode 100755 backup-image/remove.sh create mode 100755 backup-image/restore.sh create mode 100755 backup-image/verify.sh diff --git a/backup-image/copy.sh b/backup-image/copy.sh new file mode 100755 index 0000000..5b962be --- /dev/null +++ b/backup-image/copy.sh @@ -0,0 +1,51 @@ +#!/bin/bash +echo "Copy Backup!"; +if [ $# -eq 0 ]; then + echo "Usage: copy.sh [Storage disk root path]"; + exit 2; +fi +ssrc=""; +if [ -z $3 ]; then + ssrc="/mnt"; +else + ssrc="$3"; +fi +echo "Source Backup disk name: $1"; +echo "Target Backup disk name: $2"; +echo "Sync storage disk root path: $ssrc"; +echo "Press Enter to Start..."; +read; +echo "Copying..."; +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..."; +cmp -s "$ssrc/hsums/$1-gz.hash" "$ssrc/hsums/$2-gz.hash"; +if [ $? -ne 0 ]; then + echo "Byte mismatch with $1-gz.hash !"; + exit 1; +fi +cmp -s "$ssrc/hsums/$1-tbl.hash" "$ssrc/hsums/$2-tbl.hash"; +if [ $? -ne 0 ]; then + echo "Byte mismatch with $1-tbl.hash !"; + exit 1; +fi +cmp -s "$ssrc/hsums/$1.hash" "$ssrc/hsums/$2.hash"; +if [ $? -ne 0 ]; then + echo "Byte mismatch with $1.hash !"; + exit 1; +fi +cmp -s "$ssrc/parts/$1.part.gz" "$ssrc/parts/$2.part.gz"; +if [ $? -ne 0 ]; then + echo "Byte mismatch with $1.part.gz !"; + exit 1; +fi +cmp -s "$ssrc/tabls/$1.tbl" "$ssrc/tabls/$2.tbl"; +if [ $? -ne 0 ]; then + echo "Byte mismatch with $1.tbl !"; + exit 1; +fi +echo "Finished!"; +exit 0; diff --git a/backup-image/move.sh b/backup-image/move.sh new file mode 100755 index 0000000..c29623c --- /dev/null +++ b/backup-image/move.sh @@ -0,0 +1,25 @@ +#!/bin/bash +echo "Move Backup!"; +if [ $# -eq 0 ]; then + echo "Usage: move.sh [Storage disk root path]"; + exit 2; +fi +ssrc=""; +if [ -z $3 ]; then + ssrc="/mnt"; +else + ssrc="$3"; +fi +echo "Source Backup disk name: $1"; +echo "Target Backup disk name: $2"; +echo "Sync storage disk root path: $ssrc"; +echo "Press Enter to Start..."; +read; +echo "Moving..."; +mv -f "$ssrc/hsums/$1-gz.hash" "$ssrc/hsums/$2-gz.hash"; +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"; +echo "Finished!"; +exit 0; diff --git a/backup-image/remove.sh b/backup-image/remove.sh new file mode 100755 index 0000000..acce11b --- /dev/null +++ b/backup-image/remove.sh @@ -0,0 +1,24 @@ +#!/bin/bash +echo "Remove Backup!"; +if [ $# -eq 0 ]; then + echo "Usage: remove.sh [Storage disk root path]"; + exit 2; +fi +ssrc=""; +if [ -z $2 ]; then + ssrc="/mnt"; +else + ssrc="$2"; +fi +echo "Source Backup disk name: $1"; +echo "Sync storage disk root path: $ssrc"; +echo "Press Enter to Start..."; +read; +echo "Deleting..."; +rm -f "$ssrc/hsums/$1-gz.hash"; +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"; +echo "Finished!"; +exit 0; diff --git a/backup-image/restore.sh b/backup-image/restore.sh new file mode 100755 index 0000000..32b6d14 --- /dev/null +++ b/backup-image/restore.sh @@ -0,0 +1,31 @@ +#!/bin/bash +echo "Disk Restore!"; +if [ $# -eq 0 ]; then + echo "Usage: restore.sh "; + exit 2; +fi +echo "Destination Disk: /dev/$2"; +echo "Source Backup Name: $1"; +echo "Press Enter to Start..."; +read; +mkdir -p /tmp/a; +gunzip -c "/mnt/parts/$1.part.gz" | dd "of=/dev/$2" status=progress conv=sync,noerror; +echo "Calculating SHA512 sums..."; +(sha512sum "/dev/$2" > "/tmp/a/$2.sum") && dd "if=/tmp/a/$2.sum" bs=128 count=1 "of=/tmp/a/$2.hash"; +cmp -s "/tmp/a/$2.hash" "/mnt/hsums/$1.hash"; +if [ $? -ne 0 ]; then + echo "Hashes do not match!"; + exit 1; +fi +(sha512sum "/mnt/tabls/$1.tbl" > "/tmp/a/$1.tbl.sum") && dd "if=/tmp/a/$1.tbl.sum" bs=128 count=1 "of=/mnt/hsums/$1-tbl.hash"; +cmp -s "/tmp/a/$1-tbl.hash" "/mnt/hsums/$1-tbl.hash"; +if [ $? -ne 0 ]; then + echo "Partition hashes do not match!"; + exit 1; +fi +echo; +echo "Restoring up partition table..."; +sfdisk -d "/dev/$2" < "/mnt/tabls/$1.tbl"; +rm -rf /tmp/a; +echo "Finished!"; +exit 0; diff --git a/backup-image/sync.sh b/backup-image/sync.sh index b92dd8d..ee5d076 100755 --- a/backup-image/sync.sh +++ b/backup-image/sync.sh @@ -1,33 +1,39 @@ #!/bin/bash echo "Sync Backup!"; if [ $# -eq 0 ]; then - echo "Usage: sync.sh "; + 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 name: $2"; -echo "Sync source disk name: $3"; +echo "Sync target disk root path: $2"; +echo "Sync source disk root path: $ssrc"; echo "Press Enter to Start..."; read; mkdir -p /tmp/a; echo "Syncing..."; -cp -f "$3/hsums/$1-gz.hash" "$2/hsums/$1-gz.hash"; -cp -f "$3/hsums/$1-tbl.hash" "$2/hsums/$1-tbl.hash"; -cp -f "$3/hsums/$1.hash" "$2/hsums/$1.hash"; -cp -f "$3/parts/$1.part.gz" "$2/parts/$1.part.gz"; -cp -f "$3/tabls/$1.tbl" "$2/tabls/$1.tbl"; +cp -f "$ssrc/hsums/$1-gz.hash" "$2/hsums/$1-gz.hash"; +cp -f "$ssrc/hsums/$1-tbl.hash" "$2/hsums/$1-tbl.hash"; +cp -f "$ssrc/hsums/$1.hash" "$2/hsums/$1.hash"; +cp -f "$ssrc/parts/$1.part.gz" "$2/parts/$1.part.gz"; +cp -f "$ssrc/tabls/$1.tbl" "$2/tabls/$1.tbl"; echo "Verifying..."; -cmp -s "$3/hsums/$1-gz.hash" "$2/hsums/$1-gz.hash"; +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 "$3/hsums/$1-tbl.hash" "$2/hsums/$1-tbl.hash"; +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 -cmp -s "$3/hsums/$1.hash" "$2/hsums/$1.hash"; +cmp -s "$ssrc/hsums/$1.hash" "$2/hsums/$1.hash"; if [ $? -ne 0 ]; then echo "Hash mismatch with $1.hash !"; exit 1; diff --git a/backup-image/verify.sh b/backup-image/verify.sh new file mode 100755 index 0000000..077e0ae --- /dev/null +++ b/backup-image/verify.sh @@ -0,0 +1,38 @@ +#!/bin/bash +echo "Verify Backup!"; +if [ $# -eq 0 ]; then + echo "Usage: verify.sh [Storage disk root path]"; + exit 2; +fi +ssrc=""; +if [ -z $2 ]; then + ssrc="/mnt"; +else + ssrc="$2"; +fi +echo "Source Backup disk name: $1"; +echo "Sync storage disk root path: $ssrc"; +echo "Press Enter to Start..."; +read; +mkdir -p /tmp/a; +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=/mnt/hsums/$1-tbl.hash" && touch "/tmp/a/2.complete") & +until [ -f "/tmp/a/1.complete" ] && [ -f "/tmp/a/2.complete" ]; do sleep 0.1; done; +echo "Hashes:"; +cat /tmp/a/*.sum; +echo; +cmp -s "/tmp/a/$1-gz.hash" "$ssrc/hsums/$1-gz.hash"; +if [ $? -ne 0 ]; then + echo "Hash mismatch with $1.part.gz !"; + exit 1; +fi +cmp -s "/tmp/a/$1-tbl.hash" "$ssrc/hsums/$1-tbl.hash"; +if [ $? -ne 0 ]; then + echo "Hash mismatch with $1.tbl !"; + exit 1; +fi +rm -rf /tmp/a; +echo "Finished!"; +exit 0;