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 |
Hi
Thanks a lot(:
i have a problem running it
/home/scripts/backup-disk.sh: line 4: * 1073741824: syntax error: operand expected (error token is "* 1073741824")
Current clone file size: 0, total remote disk size:
/home/scripts/backup-disk.sh: line 12: [: 0: unary operator expected
do you know how to fix it?
Thank shai
try now @chikosan :)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Before running this script, you need to update your ssh config file in order to add the host name with the right credentials.
Also the user running the clone command must be a super user.
By the way you need to run it from a shell support terminal like gitbash or cygwin if you are a windows user.
Example
consider i have a server located at 80.20.180.37
i want to backup my entire server disk to a local file: "vps.backup"
I didnt want to mess up with passwords in my script, i believe authentication with ssh keys is far more secured.
so in order to authenticate with ssh keys you need to create a pair of public and private key.
this command will create 2 files in your home directory under the folder ".ssh": vps and vps.pub.
"vps" is your private key and "vps.pub" is your public key.
login to your server using putty.exe or other ways you logged in so far.
Inside your server run the commands:
an editor will be opened for updating the file: authorized_keys.
paste the content of your public key in: vps.pub file you created, to the end of that file and save the file.
What it is doing is letting your server knows that you can also log in with the matched private key you created, the "vps" file.
Now you can log out from your server.
It is tab & space sensitive so make sure you copy and paste it right.
update variables in the script:
change variable SSH_HOST value to: example.host
change variable IMAGE_NAME value to: "./vps.backup"
run the script
It will use your private key to authenticate (which is 2048-bit length).