mirkop1988

sincro7

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