This script will sync two directories between two servers. Basically we are using rsync features to run this program. Script will interactively request to input below details.
1) Remote server name or ip
2) Remote system user name
3) Local directory to sync
4) Remote directory to sync to
5) Check user need to copy timestamp and preserve permissions
Script Sync Directories
#Tell with shell to use #!/bin/bash #Ask user to enter remote server ip or name while [[ -z ${RS} ]] do echo -n "Remote syncing server ip/name: " #Save input into variable with name RS read RS done #Ask user to enter remote server user while [[ -z ${RU} ]] do echo -n "Remote syncing server user: " #Save input into variable with name RU read RU done #Ask user if copy timestamp and preserve permissions, valid input is Yes or No, will ask until valid input while [[ "$TS" != "Yes" && "$TS" != "No" ]] do echo -n "Copying with timestamp and preserve permissions : Yes/No " #Save input into variable with name TS read TS done #If user need to copy timestamp and preserve permissions we need to use key -p additionally. if [ "$TS" = "Yes" ] then TS=" -p " else #If no need to copy timestamp and preserve permissions, we don't need to use any additional key TS="" fi #Ask user to enter local directory to sync, also check if directory exist while [[ -z ${LD} || !( -d ${LD}) ]] do echo -n "Syncing local directory location: " #Save input into variable with name LD read LD done #Ask user to enter remote directory while [[ -z ${RD} ]] do echo -n "Remote Syncing directory location: " #Save input into variable with name RD read RD done echo -n "Syncing ..." #Run sync with presented parameters rsync -r $TS ${LD} ${RU}@${RS}:${RD} --progress
Script Output
#./sync.sh Remote syncing server ip/name: node246.linoxide.com Remote syncing server user: linuser Copying with timestamp and preserve permissions : Yes/No Yes Syncing local directory location: /home/linoxide/backup/database01 Remote Syncing directory location: /storage/backup/ Syncing … sending incremental file list CSR/file 1,120 100% 410.16kB/s 0:00:00 (xfr#1, to-chk=8/10) CSR/file1 1,110 100% 1.06MB/s 0:00:00 (xfr#2, to-chk=7/10) CSR/file2 1,675 100% 1.60MB/s 0:00:00 (xfr#3, to-chk=6/10) CSR/file3 2,479 100% 2.36MB/s 0:00:00 (xfr#4, to-chk=5/10) CSR/201406/file4 1,110 100% 1.06MB/s 0:00:00 (xfr#5, to-chk=3/10) CSR/201406/file5 1,679 100% 1.60MB/s 0:00:00 (xfr#6, to-chk=2/10) CSR/201406/file6 1,115 100% 1.06MB/s 0:00:00 (xfr#7, to-chk=1/10)