Skip to content

Instantly share code, notes, and snippets.

@chikosan
Forked from lihaibh/backup-disk.sh
Created April 14, 2018 07:18
Show Gist options
  • Save chikosan/2e678695035a85b9a36f0eb2b3ef843f to your computer and use it in GitHub Desktop.
Save chikosan/2e678695035a85b9a36f0eb2b3ef843f to your computer and use it in GitHub Desktop.
create snapshot of a remote linux disk to your local machine
#/usr/bin/env sh
SSH_HOST=host.name
BACKUP_DISK="sda"
DISK_SIZE_BYTES=$(($(ssh $SSH_HOST "lsblk --all | grep \"$BACKUP_DISK\" | head -1" | while read c1; do echo $c1; done | cut -d" " -f4 | sed "s/G//g") * 1073741824))
BLOCK_SIZE=512
IMAGE_NAME="./sda.disk"
[ -f $IMAGE_NAME ] && IMAGE_CUR_SIZE=$(stat -c "%s" $IMAGE_NAME) || IMAGE_CUR_SIZE=0
WRITEN_BLOCKS=$(($IMAGE_CUR_SIZE / $BLOCK_SIZE))
echo -e "Current clone file size: \e[1m$IMAGE_CUR_SIZE\e[0m, total remote disk size: \e[1m$DISK_SIZE_BYTES\e[0m"
while [ $IMAGE_CUR_SIZE -lt $DISK_SIZE_BYTES ]; do
ssh $SSH_HOST \
"su -c \"dd if=/dev/$BACKUP_DISK skip=$WRITEN_BLOCKS bs=$BLOCK_SIZE\" | gzip -1 -9 -" \
| gunzip | dd of=$IMAGE_NAME seek=$WRITEN_BLOCKS status=progress
[ -f $IMAGE_NAME ] && IMAGE_CUR_SIZE=$(stat -c "%s" $IMAGE_NAME) || IMAGE_CUR_SIZE=0
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment