Skip to content

Instantly share code, notes, and snippets.

@egll-tech
Last active August 12, 2021 23:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save egll-tech/bc3599ebf4741679cb99d74be7f36fce to your computer and use it in GitHub Desktop.
Save egll-tech/bc3599ebf4741679cb99d74be7f36fce to your computer and use it in GitHub Desktop.
Interactive script to select compressed image and disk to restore it
#!/bin/bash
# Check sudo rights
if [[ "$EUID" = 0 ]]; then
echo "Already root"
else
sudo -k # make sure to ask for password on next sudo
if sudo false; then
echo "Wrong password"
exit 1
fi
fi
# Get filename
name=$1
if [ -z "$name" ]
then
echo "Please select the file to restore:"
select name in $(ls *.img.gz)
do
if [ -n "$name" ]
then
break
fi
done
fi
echo
# Get drive location
drive=$2
if [ -z "$drive" ]
then
echo "Please select the drive:"
readarray -t drives < <(lsblk -I 8,179 -d -o NAME -n)
select drive in ${drives[*]}
do
if [ -n "$drive" ]
then
drive="/dev/${drive}"
break
fi
done
fi
# Restoring backup
echo "Generating $name from disk $drive..."
{
gunzip --stdout $name | sudo dd bs=4M of=$drive
} &> /dev/null &
PID=$!
i=1
sp="/-\|"
echo -n ' '
while [ -d /proc/$PID ]
do
printf "\b${sp:i++%${#sp}:1}"
done
echo
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment