39 lines
1.8 KiB
Bash
Executable File

#!/bin/bash
echo "Disk Backup!";
if [ $# -eq 0 ]; then
echo "Usage: backup.sh <Block Device in /dev> <Backup disk name>";
exit 2;
fi
echo "Source Disk: /dev/$1";
echo "Destination Name: $2";
echo "Press Enter to Start...";
read;
uuidTmp="/tmp/a-$(uuidgen)";
mkdir -p $uuidTmp;
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" > "$uuidTmp/$1.sum") && dd "if=$uuidTmp/$1.sum" bs=128 count=1 "of=$uuidTmp/$1.hash" && touch "$uuidTmp/1.complete") &
((sha512sum "/mnt/parts/$2.part.gz" > "$uuidTmp/$2.part.gz.sum") && dd "if=$uuidTmp/$2.part.gz.sum" bs=128 count=1 "of=$uuidTmp/$2-gz.hash" && touch "$uuidTmp/2.complete") &
((gunzip -c "/mnt/parts/$2.part.gz" | sha512sum > "$uuidTmp/$2.part.sum") && dd "if=$uuidTmp/$2.part.sum" bs=128 count=1 "of=$uuidTmp/$2.hash" && touch "$uuidTmp/3.complete") &
((sha512sum "/mnt/times/$2.time" > "$uuidTmp/$2.time.sum") && dd "if=$uuidTmp/$2.time.sum" bs=128 count=1 "of=$uuidTmp/$2-time.hash" && touch "$uuidTmp/4.complete") &
until [ -f "$uuidTmp/1.complete" ] && [ -f "$uuidTmp/2.complete" ] && [ -f "$uuidTmp/3.complete" ] && [ -f "$uuidTmp/4.complete" ]; do sleep 0.1; done;
echo "Hashes:";
cat $uuidTmp/*.sum;
echo;
cmp -s "$uuidTmp/$1.hash" "$uuidTmp/$2.hash";
if [ $? -ne 0 ]; then
echo "Hashes do not match!";
exit 1;
fi
cp -f "$uuidTmp/$2-gz.hash" "/mnt/hsums/$2-gz.hash";
cp -f "$uuidTmp/$2.hash" "/mnt/hsums/$2.hash";
cp -f "$uuidTmp/$2-time.hash" "/mnt/hsums/$2-time.hash";
echo;
echo "Backing up partition table...";
sfdisk -d "/dev/$1" > "/mnt/tabls/$2.tbl";
(sha512sum "/mnt/tabls/$2.tbl" > "$uuidTmp/$2.tbl.sum") && dd "if=$uuidTmp/$2.tbl.sum" bs=128 count=1 "of=/mnt/hsums/$2-tbl.hash";
rm -rf $uuidTmp;
echo "Finished!";
exit 0;