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 Apr 6, 2018

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"

  1. create a key pair:
    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.
ssh-keygen -t rsa -q -P "" -f ~/.ssh/vps

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.

  1. update your server to login with your private key:
    login to your server using putty.exe or other ways you logged in so far.
    Inside your server run the commands:
cd ~/.ssh;
vi authorized_keys

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.

  1. update config file in ~/ssh/config in your local machine and add these lines:
Host example.host
	Hostname		80.20.180.37
	User 			root
	Port			22 # Its the default port for ssh connection, it might be something else if you changed it
	IdentityFile	~/.ssh/vps # The path to your private key

It is tab & space sensitive so make sure you copy and paste it right.

  1. update variables in the script:
    change variable SSH_HOST value to: example.host
    change variable IMAGE_NAME value to: "./vps.backup"

  2. run the script

  • By the way you can now log in your server without password and safer by running:
ssh example.host

It will use your private key to authenticate (which is 2048-bit length).

@chikosan
Copy link

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

@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