Advertisement
mirkop1988

sincro5

Sep 8th, 2011
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.52 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. CONFIGFILE="rsyncremote.conf"
  4. SOURCELIST="source-files.list"
  5. LOGFILE="rsyncremote.log"
  6.  
  7. #Colors
  8. RED="\e[1;31m" #Red
  9. GREEN="\e[1;32m" #Green
  10. YELLOW="\e[1;33m" #Yellow
  11. RESET="\e[0m" #Reset!
  12.  
  13. # NOTA DA RIMUOVERE: questa è una semplice funzione di stampa. Per ora non fa nulla di particolarmente straordinario, ma potrebbe essere utilizzata più in là per sopprimere agevolmente l'output nel terminale, se passata un'opzione come -silent
  14. # Print on screen
  15. printScreen() { echo -e $1; }    
  16.  
  17. # Test if software installation and server connection are ok
  18. testStuff () {
  19.     # Check if source-files.list exists
  20.     if [[ ! -e $SOURCELIST ]]; then
  21.         printScreen $RED"Error: "$RESET"create and set $SOURCELIST first, then restart the script";
  22.         exit 1
  23.     fi
  24.  
  25.     # Check if inotify is installed
  26.     if ! which inotifywait &> /dev/null; then
  27.         printScreen $RED"Error: "$RESET"install inotify-tools first, then restart this script"
  28.         exit 1
  29.     fi
  30.  
  31.     # Check if unison is installed
  32.     if ! which unison &> /dev/null; then
  33.         printScreen $RED"Error: "$RESET"install unison first, then restart this script";
  34.         exit 1
  35.     fi
  36.  
  37.     # Check if ssh is installed
  38.     if [[ $METHOD -eq 'ssh' && `! which ssh &> /dev/null` ]]; then
  39.         printScreen $RED"Error: "$RESET"install ssh first, then restart this script";
  40.         exit 1
  41.     fi
  42.  
  43.     # Check if source-files.list exists
  44.     if [[ ! -e "source-files.list" ]]; then
  45.         printScreen $RED"Error: "$RESET"create and set source-files.list first, then restart the script";
  46.         exit 1
  47.     fi
  48.  
  49.     # Check if server is available
  50.     if ! ping -c 1 $SERVER &> /dev/null; then
  51.         printScreen $RED"Error: "$RESET"server is not available. Check your configuration first, then restart the script";
  52.         exit 1
  53.     fi
  54.  
  55.     # Check if a ssh connection is available and if unison is installed on server
  56.     if [[ $METHOD -eq 'ssh' && `! ssh -p $PORT $USER@$SERVER unison -version &> /dev/null` ]]; then
  57.         printScreen $RED"Error: "$RESET"impossible to login to ssh server or unison missing on the server. Check your server configuration first, then restart the script"
  58.         exit 1
  59.     fi
  60. }
  61.  
  62. # Read source-files.list and set LPATH and LPATHOPTION variables
  63. readSources() {
  64.         # Instantiate variables
  65.         LPATH=""
  66.     LPATHOPTION=""
  67.     # Read source-files.list
  68.         while read line
  69.         do
  70.         # Check if source-files.list exists
  71.         if [[ -e "$HOME/$line" ]]; then
  72.             # Append to variables each path indicated
  73.             LPATH="$LPATH$HOME/$line ";
  74.                 LPATHOPTION=$LPATHOPTION"-path "$line" ";
  75.         else
  76.             printScreen $RED"Error: "$RESET"source $line doesn't exists. Check your $SOURCELIST first, then restart the script";
  77.             exit 1
  78.         fi
  79.         done < $SOURCELIST
  80.     # Now the LPATH variable contains all path indicated and the LPATHOPTION variable contains all path with the -path options, so it can be used as option with unison
  81.     # Example:
  82.     # LPATH:    "/home/user/dir1 /home/user/dir2"
  83.     # LPATHOPTIONS: "-path /home/user/dir1 -path /home/user/dir2"
  84. }
  85.  
  86. # Start
  87. syncDir() {
  88.     unison $HOME ssh://$USER@$SERVER:$PORT/$RPATH $LPATHOPTION -logfile $LOGFILE $boptions -batch -auto
  89. }
  90.  
  91. # SCRIPT STARTS HERE
  92. # Check if config file exists
  93. if [[ -e $CONFIGFILE ]]; then
  94.     # Include config file
  95.     source $CONFIGFILE
  96.     # Perform test
  97.     testStuff
  98.     # Read source-files.list
  99.     readSources
  100.     # Finally, sync dirs
  101.     syncDir
  102.     while inotifywait -r -e modify -e create -e delete $LPATH &> /dev/null; do
  103.         syncDir
  104.     done
  105.     exit 0
  106. # If config file doesn't exists
  107. else
  108.     printScreen $RED"Error: "$RESET"create and set "$SOURCELIST" first, then restart the script";  
  109.     exit 1
  110. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement