Last active
August 4, 2018 19:10
-
-
Save lihaibh/764e9d9fa2fa7f4373b5157f4c72b95b to your computer and use it in GitHub Desktop.
create snapshot of a remote linux disk to your local machine
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
try now @chikosan :)