bash-stuff/backup-image/sync.sh

57 lines
1.8 KiB
Bash
Executable File

#!/bin/bash
echo "Sync Backup!";
if [ $# -eq 0 ]; then
echo "Usage: sync.sh <Backup disk name> <Sync target disk root path> [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";
echo "Press Enter to Start...";
read;
mkdir -p /tmp/a;
echo "Syncing...";
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 "$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-tbl.hash" "$2/hsums/$1-tbl.hash";
if [ $? -ne 0 ]; then
echo "Hash mismatch with $1-tbl.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" && touch "/tmp/a/1.complete") &
((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" && touch "/tmp/a/2.complete") &
until [ -f "/tmp/a/1.complete" ] && [ -f "/tmp/a/2.complete" ]; do sleep 0.1; done;
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
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
rm -rf /tmp/a;
echo "Finished!";
exit 0;