#!/bin/bash function usage { echo -e "Usage: $0 [OPTIONS]" echo "OPTIONS: " echo -e " -f path to a file with IP addresses of Linux hosts that are copied" echo -e " -v display version" echo -e " -h display help\n" echo -e "EXAMPLE: " echo "$0 -f ip.txt" } function version { echo "$0 version 1.1" echo "License GPLv3+: GNU GPL version 3 or later ." echo "This is free software: you are free to change and redistribute it." echo "There is NO WARRANTY, to the extent permitted by law." exit } function read_arguments { while getopts "f:vh" arg; do case "$arg" in f) ipfile="$OPTARG";; v) version;; h) usage exit;; esac done } function check_arguments { [ ! "$(type -p sshpass)" ] && echo "Utility 'sshpass' not found, install it" && exit 1 [ ! -f "$ipfile" ] && echo "Can't find file with list of IP addresses of Linux systems, use $0 -h" && exit 1 } function check_disk { if ( [[ "$parameter" != /dev/[hs]d[a-z] ]] && [[ "$disk" != /dev/[hs]d[a-z]/ ]] ); then echo "'$parameter' is not valid disk, enter disk name e.g /dev/sda, exiting" && exit 1 fi } function get_ssh_parameters { ssh_list_parameter=() echo -e "\nChecking SSH connection to '$ip'" for ssh_parameter in username password disk; do if [ "$ssh_parameter" == 'password' ]; then read -s -p "Enter $ssh_parameter: " parameter /dev/null; conval="$?" if [ "$conval" == 0 ]; then echo -e "SSH connection to '$ip' is working" else echo -e "Can't connect to '$ip' with username $username and password you've provided, exiting" & exit 1 fi } function make_copy_dd { for ip in $(cat $ipfile); do get_ssh_parameters echo -e "Copying '$disk' from '$ip' is started\n" echo "$password" | sshpass -p "$password" ssh -o StrictHostKeyChecking=no "$username"@"$ip" "sudo -S /bin/dd if=$disk \ status=progress | gzip -1 -" | dd of="$ip"-"$disk_short".raw.gz done echo -e "\nCopying finished!" } ############## BODY #################### read_arguments $@ check_arguments make_copy_dd # get_ssh_parameters