mirkop1988

sincro6

Sep 16th, 2011
167
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2.  
  3. CONFIGFILE="fud.conf"
  4. FILELIST="files.list"
  5. LOGFILE="fud.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 files.list exists
  20.     if [[ ! -e $FILELIST ]]; then
  21.         printScreen $RED"Error: "$RESET"create and set $FILELIST 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 you are in the fuse group
  38.     if ! groups | grep "fuse" &> /dev/null; then
  39.         printScreen $RED"Error: "$RESET"you're not in the fuse group. Type \`addgroup $USER fuse\` as root first, then logout, login and restart this script";
  40.         exit 1
  41.     fi 
  42.  
  43.     #Check for ssh connection
  44.     if [[ $METHOD -eq 'ssh' ]]; then
  45.         # Check if sshfs is installed
  46.         if ! which sshfs &> /dev/null; then
  47.             printScreen $RED"Error: "$RESET"install sshfs first, then restart this script";
  48.             exit 1
  49.         fi
  50.         # Try to mount the remote directory with sshfs and verify
  51.         if ! sshfs $USER@$SERVER:$RPATH /mnt/fud -p $PORT -C &> /dev/null; then
  52.             printScreen $RED"Error: "$RESET"impossible to login to ssh server. Check your server configuration first, then restart the script"
  53.             exit 1
  54.         fi
  55.     fi
  56.  
  57.     # Check for ftp connection
  58.     if [[ $METHOD -eq 'ftp' ]]; then
  59.         # Check if curlftpfs is installed
  60.         if ! which curlftpfs &> /dev/null; then
  61.             printScreen $RED"Error: "$RESET"install curlftpfs first, then restart this script";
  62.             exit 1
  63.         fi
  64.         # Try to mount the remote directory with curlftpfs and verify
  65.         if ! curlftpfs $SERVER:$PORT /mnt/fud -o user=$USER:$PWD &> /dev/null; then
  66.             printScreen $RED"Error: "$RESET"impossible to login to ftp server. Check your server configuration first, then restart the script"
  67.             exit 1
  68.         fi
  69.     fi
  70. }
  71.  
  72. # Read files.list and set LPATH and LPATHOPTION variables
  73. readSources() {
  74.         # Instantiate variables
  75.         LPATH=""
  76.     LPATHOPTION=""
  77.     # Read source-files.list
  78.         while read line; do
  79.         # Check if source-files.list exists
  80.         if [[ -e "$HOME/$line" ]]; then
  81.             # Append to variables each path indicated
  82.             LPATH="$LPATH$HOME/$line ";
  83.                 LPATHOPTION=$LPATHOPTION"-path "$line" ";
  84.         else
  85.             printScreen $RED"Error: "$RESET"source $line doesn't exists. Check your $SOURCELIST first, then restart the script";
  86.             exit 1
  87.         fi
  88.         done < $FILELIST
  89.     # 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
  90.     # Example:
  91.     # LPATH:    "/home/user/dir1 /home/user/dir2"
  92.     # LPATHOPTIONS: "-path /home/user/dir1 -path /home/user/dir2"
  93. }
  94.  
  95. # Start
  96. syncDir() {
  97.     OUTPUT=$(unison $HOME /mnt/fud $LPATHOPTION -logfile $LOGFILE $boptions -batch -auto -terse -confirmbigdel="false" 2>&1)
  98.     SYNCFILES=$(echo `expr match "$OUTPUT" '.*\(([0-9]* item[s]* transferred\)'` | sed -n "s/(//g;s/item[s]* transferred//g;/[0-9]*/p")
  99.    
  100.     if [[ "$SYNCFILES" -ge "1" ]]; then
  101.         if [[ "$SYNCFILES" -eq "1" ]]; then
  102.             MESSAGE="$SYNCFILES file have been synchronized"
  103.         elif [[ "$SYNCFILES" -gt "1" ]]; then
  104.             MESSAGE="$SYNCFILES files have been synchronized"
  105.         fi
  106.  
  107.         notify-send --icon=/usr/share/icons/gnome/32x32/emblems/emblem-shared.png "$MESSAGE"
  108.     fi
  109. }
  110.  
  111. # SCRIPT STARTS HERE
  112. # Check if config file exists
  113. if [[ -e $CONFIGFILE ]]; then
  114.     # Include config file
  115.     source $CONFIGFILE
  116.     # Perform test
  117.     testStuff
  118.     # Read source-files.list
  119.     readSources
  120.     # Finally, sync dirs
  121.     syncDir
  122.     while inotifywait -r -e modify -e create -e move -e delete $LPATH &> /dev/null; do
  123.         syncDir
  124.     done
  125.     exit 0
  126. # If config file doesn't exists
  127. else
  128.     printScreen $RED"Error: "$RESET"create and set "$SOURCELIST" first, then restart the script";  
  129.     exit 1
  130. fi
RAW Paste Data