Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- CONFIGFILE="rsyncremote.conf"
- SOURCELIST="source-files.list"
- LOGFILE="rsyncremote.log"
- #Colors
- RED="\e[1;31m" #Red
- GREEN="\e[1;32m" #Green
- YELLOW="\e[1;33m" #Yellow
- RESET="\e[0m" #Reset!
- # 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
- # Print on screen
- printScreen() { echo -e $1; }
- # Test if software installation and server connection are ok
- testStuff () {
- # Check if source-files.list exists
- if [[ ! -e $SOURCELIST ]]; then
- printScreen $RED"Error: "$RESET"create and set $SOURCELIST first, then restart the script";
- exit 1
- fi
- # Check if inotify is installed
- if ! which inotifywait &> /dev/null; then
- printScreen $RED"Error: "$RESET"install inotify-tools first, then restart this script"
- exit 1
- fi
- # Check if unison is installed
- if ! which unison &> /dev/null; then
- printScreen $RED"Error: "$RESET"install unison first, then restart this script";
- exit 1
- fi
- # Check if ssh is installed
- if [[ $METHOD -eq 'ssh' && `! which ssh &> /dev/null` ]]; then
- printScreen $RED"Error: "$RESET"install ssh first, then restart this script";
- exit 1
- fi
- # Check if source-files.list exists
- if [[ ! -e "source-files.list" ]]; then
- printScreen $RED"Error: "$RESET"create and set source-files.list first, then restart the script";
- exit 1
- fi
- # Check if server is available
- if ! ping -c 1 $SERVER &> /dev/null; then
- printScreen $RED"Error: "$RESET"server is not available. Check your configuration first, then restart the script";
- exit 1
- fi
- # Check if a ssh connection is available and if unison is installed on server
- if [[ $METHOD -eq 'ssh' && `! ssh -p $PORT $USER@$SERVER unison -version &> /dev/null` ]]; then
- 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"
- exit 1
- fi
- }
- # Read source-files.list and set LPATH and LPATHOPTION variables
- readSources() {
- # Instantiate variables
- LPATH=""
- LPATHOPTION=""
- # Read source-files.list
- while read line
- do
- # Check if source-files.list exists
- if [[ -e "$HOME/$line" ]]; then
- # Append to variables each path indicated
- LPATH="$LPATH$HOME/$line ";
- LPATHOPTION=$LPATHOPTION"-path "$line" ";
- else
- printScreen $RED"Error: "$RESET"source $line doesn't exists. Check your $SOURCELIST first, then restart the script";
- exit 1
- fi
- done < $SOURCELIST
- # 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
- # Example:
- # LPATH: "/home/user/dir1 /home/user/dir2"
- # LPATHOPTIONS: "-path /home/user/dir1 -path /home/user/dir2"
- }
- # Start
- syncDir() {
- unison $HOME ssh://$USER@$SERVER:$PORT/$RPATH $LPATHOPTION -logfile $LOGFILE $boptions -batch -auto
- }
- # SCRIPT STARTS HERE
- # Check if config file exists
- if [[ -e $CONFIGFILE ]]; then
- # Include config file
- source $CONFIGFILE
- # Perform test
- testStuff
- # Read source-files.list
- readSources
- # Finally, sync dirs
- syncDir
- while inotifywait -r -e modify -e create -e delete $LPATH &> /dev/null; do
- syncDir
- done
- exit 0
- # If config file doesn't exists
- else
- printScreen $RED"Error: "$RESET"create and set "$SOURCELIST" first, then restart the script";
- exit 1
- fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement