Pastebin launched a little side project called HostCabi.net, check it out ;-)Don't like ads? PRO users don't see any ads ;-)
Guest

Rsync

By: a guest on Aug 10th, 2012  |  syntax: Bash  |  size: 3.75 KB  |  hits: 62  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #!/bin/bash
  2.  
  3. ###----------------------------------------------------###
  4. ### If there is already a websys.txt then remove it    ###
  5. ###----------------------------------------------------###
  6.  
  7. if [ -e /nusdata/staff/NUS/NUS/Systems/websys.txt ]
  8.   then
  9.     echo doing some cleanup
  10.     rm /nusdata/staff/NUS/NUS/Systems/websys.txt
  11. fi
  12.  
  13. ###----------------------------------------------------###
  14. ### Change directory so that this script can be placed ###
  15. ### anywhere on the computer and it will still work    ###
  16. ###----------------------------------------------------###
  17.  
  18. cd /nusdata/staff/NUS/NUS/Systems/
  19.  
  20. ###----------------------------------------------------###
  21. ### This will output the names of the directories for  ###
  22. ### each system. We will use this to create a variable ###
  23. ### in an array for each system directory. The output  ###
  24. ### will be put in kiskasys.txt to be used later       ###
  25. ###----------------------------------------------------###
  26.  
  27. echo writing directory names to kiskasys.txt
  28. ls -F | grep '/$' | grep -v "^[^0-9]" | grep -v '^100' | sed -e "s/\///" | sed -e s/^/\'/ | sed -e s/$/\'/ | cat > kiskasys.txt
  29.  
  30. ###----------------------------------------------------###
  31. ### This will do the same as before only create a list ###
  32. ### of the system directories on the web server. The   ###
  33. ### output will be written to websys.txt               ###
  34. ###----------------------------------------------------###
  35.  
  36. ssh -p 50014 nusak@webserver 'echo I am now $USER at $HOSTNAME' #Un-comment this line to test if the SSH is working
  37.  
  38. ssh -p 50014 nusak@webserver 'cd /home/www/html/systems/; ls -F | grep '/$' | grep -v "^[^0-9]" | sed -e "s/\///" | cat > websys.txt'
  39. echo just wrote directory names to websys.txt
  40.  
  41. ###----------------------------------------------------###
  42. ### Use scp to copy the file we just created on remote ###
  43. ### system to the Systems directory                    ###
  44. ###----------------------------------------------------###
  45.  
  46. scp -P 50014 nusak@webserver:/home/www/html/systems/websys.txt /nusdata/staff/NUS/NUS/Systems/websys.txt
  47. echo Just coppied websys.txt from webserver to $HOSTNAME
  48.  
  49. ###----------------------------------------------------###
  50. ### Create two arrays, one that holds the directory    ###
  51. ### names on the kiska server and one that holds the   ###
  52. ### directory names on the web server. The variables   ###
  53. ### are kiskasys and websys respectively               ###
  54. ###----------------------------------------------------###
  55.  
  56. old_IFS=$IFS
  57. IFS=$'\n'
  58. websys=($(cat /nusdata/staff/NUS/NUS/Systems/websys.txt)) #array
  59. IFS=$old_IFS
  60.  
  61. old_IFS=$IFS
  62. IFS=$'\n'
  63. kiskasys=($(cat /nusdata/staff/NUS/NUS/Systems/kiskasys.txt)) #array
  64. IFS=$old_IFS
  65.  
  66. ###----------------------------------------------------###
  67. ### Use if statement to check to make sure that the    ###
  68. ### two directory lists have the same number of        ###
  69. ### entries. If they do not it returns an error        ###
  70. ### instead of continuing.                             ###
  71. ###----------------------------------------------------###
  72.  
  73. if [ "${#websys[@]}" != "${#kiskasys[@]}" ]
  74.   then
  75.     echo ERROR!! kiskasys.txt and websys.txt do not have an equal number of entries
  76.   else
  77.  
  78. ###----------------------------------------------------###
  79. ### For every line in the websys.txt, this will rsync  ###
  80. ### from the directory in line X of the kiskasys.txt   ###
  81. ### to the directory in line X of websys.txt           ###
  82. ###----------------------------------------------------###
  83.  
  84.     for ((i=0; i<${#websys[@]}; i++))
  85.     do
  86.       localpath="/nusdata/staff/NUS/NUS/Systems/"${kiskasys[$i]}"/"
  87.       remotepath="/home/www/html/systems/${websys[$i]}"
  88.       rsync -avz -s -e "$localpath" "ssh -p 50014" "nusak@webserver:$remotepath/"
  89.     done
  90. fi
  91.  
  92. exit