bash-stuff/backup-image/backup.sh

35 lines
1.4 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;
mkdir -p /tmp/a;
dd "if=/dev/$1" status=progress conv=sync,noerror | gzip -6 -c > "/mnt/parts/$2.part.gz";
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;
echo "Hashes:";
cat /tmp/a/*.sum;
echo;
cmp -s "/tmp/a/$1.hash" "/tmp/a/$2.hash";
if [ $? -ne 0 ]; then
echo "Hashes do not match!";
exit 1;
fi
cp -f "/tmp/a/$2-gz.hash" "/mnt/hsums/$2-gz.hash";
cp -f "/tmp/a/$2.hash" "/mnt/hsums/$2.hash";
echo;
echo "Backing up partition table...";
sfdisk -d "/dev/$1" > "/mnt/tabls/$2.tbl";
(sha512sum "/mnt/tabls/$2.tbl" > "/tmp/a/$2.tbl.sum") && dd "if=/tmp/a/$2.tbl.sum" bs=128 count=1 "of=/mnt/hsums/$2-tbl.hash";
rm -rf /tmp/a;
echo "Finished!";
exit 0;