ssfs rev 3

Add ssfs the client cmdline tool
author Christophe Lincoln <pankso@slitaz.org>
date Sat Jun 11 08:41:34 2011 +0200 (2011-06-11)
parents 99a6560d8128
children 0de2fba271a3
files ssfs
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/ssfs	Sat Jun 11 08:41:34 2011 +0200
     1.3 @@ -0,0 +1,134 @@
     1.4 +#!/bin/sh
     1.5 +#
     1.6 +# SliTaz Secure File Storage. Fast live sync using: SSH, Rsync, Lsyncd.
     1.7 +# Configuration file is a LUA script in user space.
     1.8 +#
     1.9 +# Copyright (C) SliTaz GNU/Linux - BSD License
    1.10 +# Author: Christophe Lincoln <pankso@slitaz.org>
    1.11 +#
    1.12 +
    1.13 +app=$(basename $0)
    1.14 +pid=$HOME/.local/var/run/$app.pid
    1.15 +logdir=$HOME/.local/var/log/$app
    1.16 +config=$HOME/.config/$app/client.lua
    1.17 +
    1.18 +# Parse cmdline options.
    1.19 +for opt in $@
    1.20 +do
    1.21 +	case "$opt" in
    1.22 +		--login=*)
    1.23 +			login=${opt#--login=} ;;
    1.24 +		--host=*)
    1.25 +			host=${opt#--host=} ;;
    1.26 +		*)
    1.27 +			continue ;;
    1.28 +	esac
    1.29 +done
    1.30 +
    1.31 +#
    1.32 +# Functions
    1.33 +#
    1.34 +
    1.35 +# Built-in usage.
    1.36 +help() {
    1.37 +	cat << EOT
    1.38 +
    1.39 +$(echo -e "\033[1m$(gettext "Usage:")\033[0m") $app [command] [--option=]
    1.40 +
    1.41 +$(echo -e "\033[1m$(gettext "Commands:")\033[0m")
    1.42 +  help          $(gettext "Display this short usage.")
    1.43 +  info          $(gettext "Display configuration settings.")
    1.44 +  setup         $(gettext "Setup client configuration and RSA key.")
    1.45 +  sync          $(gettext "Sync from server and start daemon.")
    1.46 +  stop          $(gettext "Stop monitoring ~/Sync folder.")
    1.47 +
    1.48 +$(echo -e "\033[1m$(gettext "Options:")\033[0m")
    1.49 +  --login=      $(gettext "Login name on remote host.")
    1.50 +  --host=       $(gettext "Server host name or IP.")
    1.51 +
    1.52 +EOT
    1.53 +}
    1.54 +
    1.55 +# Display config settings.
    1.56 +config_info() {
    1.57 +	[ ! -f "$config" ] && exit 0
    1.58 +	fgrep '=' $config | sed s'/^	//'	
    1.59 +}
    1.60 +
    1.61 +# Create a Lua configuration file for lsyncd.
    1.62 +gen_config() {
    1.63 +	mkdir -p $(dirname $config)
    1.64 +	cat > $config << EOT
    1.65 +-- Configuration file for SliTaz Secure Filesystem lsyncd daemon.
    1.66 +
    1.67 +sync {
    1.68 +	default.rsyncssh,
    1.69 +	source    = "$HOME/Sync",
    1.70 +	host      = "$login@$host",
    1.71 +	targetdir = "Sync/"
    1.72 +}
    1.73 +EOT
    1.74 +}
    1.75 +
    1.76 +#
    1.77 +# Commands
    1.78 +#
    1.79 +
    1.80 +case "$1" in
    1.81 +	info)
    1.82 +		size=$(du -sh $HOME/Sync | awk '{print $1}')
    1.83 +		echo "" && config_info && echo ""
    1.84 +		gettext "RSA key   :"; echo " $HOME/.ssh/id_rsa"
    1.85 +		gettext "Sync size :"; echo " $size"
    1.86 +		echo "" ;;
    1.87 +	setup)
    1.88 +		# We need a login and host name or IP.
    1.89 +		if [ -z "$login" ] || [ -z "$host" ]; then
    1.90 +			gettext -e "\nUsage:"; echo -e \
    1.91 +				" $(basename $0) setup --login=user --host=server\n" && exit 0
    1.92 +		fi
    1.93 +		gen_config
    1.94 +		mkdir -p $logdir $(dirname $pid)
    1.95 +		
    1.96 +		# Configure passwordless login via RSA key.
    1.97 +		if [ ! -f "$HOME/.ssh/id_rsa" ]; then
    1.98 +			mkdir -p $HOME/.ssh
    1.99 +			ssh-keygen -b 2048 -f $HOME/.ssh/id_rsa -P ''
   1.100 +		fi
   1.101 +		
   1.102 +		# Upload key to the server.
   1.103 +		gettext "Sending RSA secure key to:"; echo " $host"
   1.104 +		cat $HOME/.ssh/id_rsa.pub | \
   1.105 +			ssh $login@$host 'cat - >> ~/.ssh/authorized_keys' ;;
   1.106 +	sync)
   1.107 +		# Sync can be called at session startup or from cmdline to retrive
   1.108 +		# files from the server.
   1.109 +		[ ! -f "$config" ] && gettext -e "Missing config file\n" && exit 1
   1.110 +		at=$(fgrep host $config | cut -d '"' -f 2)
   1.111 +		login=${at%@*}
   1.112 +		host=${at#*@}
   1.113 +		gettext -e "\nSyncing from:"; echo " $login@$host"
   1.114 +		
   1.115 +		# First sync host with server.
   1.116 +		rsync -r -a -v -h -z -u --delete \
   1.117 +			--log-file=$logdir/rsync.log \
   1.118 +			-e "ssh -l $login" $host:~/Sync/ ~/Sync/ | \
   1.119 +				sed -e "/^$/"d -e "/^.\/$/"d
   1.120 +		
   1.121 +		# Monitor local folder if not yet running.
   1.122 +		if [ ! -f "$pid" ]; then
   1.123 +			gettext "Starting lsyncd daemon..."
   1.124 +			lsyncd -pidfile $pid -log all -logfile $logdir/lsyncd.log $config
   1.125 +		else
   1.126 +			gettext "Lcyncd daemon running PID:"; echo " $(cat $pid)"
   1.127 +		fi && echo "" ;;
   1.128 +	stop)
   1.129 +		# Kill daemon and remove pidfile.
   1.130 +		[ ! -f "$pid" ] && gettext -e "Daemon is not running\n" && exit 0 
   1.131 +		kill=$(cat $pid)
   1.132 +		gettext "Stopping"; echo "$app PID: $kill"
   1.133 +		kill $kill && rm -f $pid ;;
   1.134 +	*)
   1.135 +		help ;;
   1.136 +esac
   1.137 +exit 0