Advertisement
dantpro

bkp-rsync.sh

May 20th, 2015
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.28 KB | None | 0 0
  1. # http://linoxide.com/linux-shell-script/shell-script-backup-files-directories-rsync/
  2.  
  3. #=== Script 1 ===
  4. #!/bin/bash
  5.  
  6.  
  7. #We will save path to backup file in variable
  8. backupf='/tmp/bckup.txt'
  9.  
  10. #Next line just prints message
  11. echo "Shell Script Backup Your Files / Directories Using rsync"
  12.  
  13. #next line check if entered value is not null, and if null it will reask user to enter Destination Server
  14. while [ x$desthost = "x" ]; do
  15.  
  16. #next line prints what userd should enter, and stores entered value to variable with name desthost
  17. read -p "Destination backup Server : " desthost
  18.  
  19. #next line finishes while loop
  20. done
  21.  
  22. #next line check if entered value is not null, and if null it will reask user to enter Destination Path
  23. while [ x$destpath = "x" ]; do
  24.  
  25. #next line prints what userd should enter, and stores entered value to variable with name destpath
  26. read -p "Destination Folder : " destpath
  27.  
  28. #next line finishes while loop
  29. done
  30.  
  31. #Next line will start reading backup file line by line
  32. for line in `cat $backupf`
  33.  
  34. #and on each line will execute next
  35. do
  36.  
  37. #print message that file/dir will be copied
  38. echo "Copying $line ... "
  39. #copy via rsync file/dir to destination
  40.  
  41. rsync -ar "$line" "$desthost":"$destpath"
  42.  
  43. #this line just print done
  44. echo "DONE"
  45.  
  46. #end of reading backup file
  47. done
  48.  
  49. # === Script 2 ===
  50. #!/bin/bash
  51.  
  52. #We will save path to backup file in variable
  53. backupf='/tmp/bckup.txt'
  54.  
  55. #Next line just prints message
  56. echo "Shell Script Backup Your Files / Directories Using rsync"
  57.  
  58. #next line check if entered value is not null, and if null it will reask user to enter Destination Server
  59. while [ x$desthost = "x" ]; do
  60.  
  61. #next line prints what userd should enter, and stores entered value to variable with name desthost
  62. read -p "Destination backup Server : " desthost
  63.  
  64. #next line finishes while loop
  65. done
  66.  
  67. #next line check if entered value is not null, and if null it will reask user to enter Destination Path
  68. while [ x$destpath = "x" ]; do
  69.  
  70. #next line prints what userd should enter, and stores entered value to variable with name destpath
  71. read -p "Destination Folder : " destpath
  72.  
  73. #next line finishes while loop
  74. done
  75.  
  76. #next line check if entered value is not null, and if null it will reask user to enter password
  77. while [ x$password = "x" ]; do
  78. #next line prints what userd should enter, and stores entered value to variable with name password. #To hide password we are using -s key
  79. read -sp "Password : " password
  80. #next line finishes while loop
  81. done
  82.  
  83. #Next line will start reading backup file line by line
  84. for line in `cat $backupf`
  85.  
  86. #and on each line will execute next
  87. do
  88.  
  89. #print message that file/dir will be copied
  90. echo "Copying $line ... "
  91. #we will use expect tool to enter password inside script
  92. /usr/bin/expect << EOD
  93. #next line set timeout to -1, recommended to use
  94. set timeout -1
  95. #copy via rsync file/dir to destination, using part of expect — spawn command
  96.  
  97. spawn rsync -ar ${line} ${desthost}:${destpath}
  98. #as result of previous command we expect “password” promtp
  99. expect "*?assword:*"
  100. #next command enters password from script
  101. send "${password}\r"
  102. #next command tells that we expect end of file (everything finished on remote server)
  103. expect eof
  104. #end of expect pard
  105. EOD
  106. #this line just print done
  107. echo "DONE"
  108.  
  109. #end of reading backup file
  110. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement