Skip to content

Instantly share code, notes, and snippets.

@lihaibh
Last active August 4, 2018 19:10
Show Gist options
  • Save lihaibh/764e9d9fa2fa7f4373b5157f4c72b95b to your computer and use it in GitHub Desktop.
Save lihaibh/764e9d9fa2fa7f4373b5157f4c72b95b 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=`expr 1073741824 \* $(ssh $SSH_HOST "lsblk --all | grep \"$BACKUP_DISK\" | head -1" | while read c1; do echo $c1; done | cut -d" " -f4 | sed "s/G//g")`
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
@lihaibh
Copy link
Author

lihaibh commented Aug 4, 2018

try now @chikosan :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment