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/267a0ac3c2704bbf1ce3d6b850d2c80f to your computer and use it in GitHub Desktop.
Save egll-tech/267a0ac3c2704bbf1ce3d6b850d2c80f to your computer and use it in GitHub Desktop.
Interactive script to backup disks in the current location. Developed for Raspberry Pi images, but it can be used with any type of disk
#!/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
while [ -z "$name" ]
do
read -p "Please name the backup: " name
done
# 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
# Generating backup
echo "Generating $name from disk $drive..."
{
sudo dd bs=4M if=$drive | gzip > $name.img.gz
} &> /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