Advertisement
KroniK907

Bash Rsync

Aug 10th, 2012
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.73 KB | None | 0 0
  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.  
  29. ls -F | grep '/$' | grep -v "^[^0-9]" | grep -v '^100' | sed -e "s/\///" | cat > kiskasys.txt
  30.  
  31. ###----------------------------------------------------###
  32. ### This will do the same as before only create a list ###
  33. ### of the system directories on the web server. The   ###
  34. ### output will be written to websys.txt               ###
  35. ###----------------------------------------------------###
  36.  
  37. ssh -p 50014 nusak@webserver 'echo I am now $USER at $HOSTNAME' #Un-comment this line to test if the SSH is working
  38.  
  39. ssh -p 50014 nusak@webserver 'cd /home/www/html/systems/; ls -F | grep '/$' | grep -v "^[^0-9]" | sed -e "s/\///" | cat > websys.txt'
  40. echo just wrote directory names to websys.txt
  41.  
  42. ###----------------------------------------------------###
  43. ### Use scp to copy the file we just created on remote ###
  44. ### system to the Systems directory                    ###
  45. ###----------------------------------------------------###
  46.  
  47. scp -P 50014 nusak@webserver:/home/www/html/systems/websys.txt /nusdata/staff/NUS/NUS/Systems/websys.txt
  48. echo Just coppied websys.txt from webserver to $HOSTNAME
  49.  
  50. ###----------------------------------------------------###
  51. ### Create two arrays, one that holds the directory    ###
  52. ### names on the kiska server and one that holds the   ###
  53. ### directory names on the web server. The variables   ###
  54. ### are kiskasys and websys respectively               ###
  55. ###----------------------------------------------------###
  56.  
  57. old_IFS=$IFS
  58. IFS=$'\n'
  59. websys=($(cat /nusdata/staff/NUS/NUS/Systems/websys.txt)) #array
  60. IFS=$old_IFS
  61.  
  62. old_IFS=$IFS
  63. IFS=$'\n'
  64. kiskasys=($(cat /nusdata/staff/NUS/NUS/Systems/kiskasys.txt)) #array
  65. IFS=$old_IFS
  66.  
  67. ###----------------------------------------------------###
  68. ### Use if statement to check to make sure that the    ###
  69. ### two directory lists have the same number of        ###
  70. ### entries. If they do not it returns an error        ###
  71. ### instead of continuing.                             ###
  72. ###----------------------------------------------------###
  73.  
  74. if [ "${#websys[@]}" != "${#kiskasys[@]}" ]
  75.   then
  76.     echo ERROR!! kiskasys.txt and websys.txt do not have an equal number of entries
  77.   else
  78.  
  79. ###----------------------------------------------------###
  80. ### For every line in the websys.txt, this will rsync  ###
  81. ### from the directory in line X of the kiskasys.txt   ###
  82. ### to the directory in line X of websys.txt           ###
  83. ###----------------------------------------------------###
  84.  
  85.     for ((i=0; i<1; i++))
  86.     do
  87.       localpath="'/nusdata/staff/NUS/NUS/Systems/${kiskasys[$i]}'"
  88.       remotepath="/home/www/html/nusalaska.com/html/systems/${websys[$i]}"
  89.       rsync -rlptnvz -s "$localpath" -e "ssh -p 50014" "nusak@webserver:$remotepath/"
  90.     done
  91. fi
  92.  
  93. exit
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement