# http://linoxide.com/linux-shell-script/shell-script-backup-files-directories-rsync/ #=== Script 1 === #!/bin/bash #We will save path to backup file in variable backupf='/tmp/bckup.txt' #Next line just prints message echo "Shell Script Backup Your Files / Directories Using rsync" #next line check if entered value is not null, and if null it will reask user to enter Destination Server while [ x$desthost = "x" ]; do #next line prints what userd should enter, and stores entered value to variable with name desthost read -p "Destination backup Server : " desthost #next line finishes while loop done #next line check if entered value is not null, and if null it will reask user to enter Destination Path while [ x$destpath = "x" ]; do #next line prints what userd should enter, and stores entered value to variable with name destpath read -p "Destination Folder : " destpath #next line finishes while loop done #Next line will start reading backup file line by line for line in `cat $backupf` #and on each line will execute next do #print message that file/dir will be copied echo "Copying $line ... " #copy via rsync file/dir to destination rsync -ar "$line" "$desthost":"$destpath" #this line just print done echo "DONE" #end of reading backup file done # === Script 2 === #!/bin/bash #We will save path to backup file in variable backupf='/tmp/bckup.txt' #Next line just prints message echo "Shell Script Backup Your Files / Directories Using rsync" #next line check if entered value is not null, and if null it will reask user to enter Destination Server while [ x$desthost = "x" ]; do #next line prints what userd should enter, and stores entered value to variable with name desthost read -p "Destination backup Server : " desthost #next line finishes while loop done #next line check if entered value is not null, and if null it will reask user to enter Destination Path while [ x$destpath = "x" ]; do #next line prints what userd should enter, and stores entered value to variable with name destpath read -p "Destination Folder : " destpath #next line finishes while loop done #next line check if entered value is not null, and if null it will reask user to enter password while [ x$password = "x" ]; do #next line prints what userd should enter, and stores entered value to variable with name password. #To hide password we are using -s key read -sp "Password : " password #next line finishes while loop done #Next line will start reading backup file line by line for line in `cat $backupf` #and on each line will execute next do #print message that file/dir will be copied echo "Copying $line ... " #we will use expect tool to enter password inside script /usr/bin/expect << EOD #next line set timeout to -1, recommended to use set timeout -1 #copy via rsync file/dir to destination, using part of expect ? spawn command spawn rsync -ar ${line} ${desthost}:${destpath} #as result of previous command we expect ?password? promtp expect "*?assword:*" #next command enters password from script send "${password}\r" #next command tells that we expect end of file (everything finished on remote server) expect eof #end of expect pard EOD #this line just print done echo "DONE" #end of reading backup file done