ssfs view ssfs @ rev 49

ssfs-box: small improvment
author Christophe Lincoln <pankso@slitaz.org>
date Sun Jun 12 19:16:41 2011 +0200 (2011-06-12)
parents 766306b0ea7b
children 0409a8244cb8
line source
1 #!/bin/sh
2 #
3 # SliTaz Secure File Storage. Fast live sync using: SSH, Rsync, Lsyncd.
4 # Configuration file is a LUA script in user space.
5 #
6 # Copyright (C) SliTaz GNU/Linux - BSD License
7 # Author: Christophe Lincoln <pankso@slitaz.org>
8 #
10 app=$(basename $0)
11 pid=$HOME/.local/var/run/$app.pid
12 logdir=$HOME/.local/var/log/$app
13 config=$HOME/.config/$app/client.lua
15 # Parse cmdline options.
16 for opt in $@
17 do
18 case "$opt" in
19 --login=*)
20 login=${opt#--login=} ;;
21 --host=*)
22 host=${opt#--host=} ;;
23 *)
24 continue ;;
25 esac
26 done
28 #
29 # Functions
30 #
32 # Built-in usage.
33 help() {
34 cat << EOT
36 $(echo -e "\033[1m$(gettext "Usage:")\033[0m") $app [command] [--option=]
38 $(echo -e "\033[1m$(gettext "Commands:")\033[0m")
39 help $(gettext "Display this short usage.")
40 info $(gettext "Display configuration settings.")
41 setup $(gettext "Setup client configuration and RSA key.")
42 sync $(gettext "Sync from server and start daemon.")
43 stop $(gettext "Stop monitoring ~/Sync folder.")
45 $(echo -e "\033[1m$(gettext "Options:")\033[0m")
46 --login= $(gettext "Login name on remote host.")
47 --host= $(gettext "Server host name or IP.")
49 EOT
50 }
52 # Display config settings.
53 config_info() {
54 [ ! -f "$config" ] && exit 0
55 fgrep '=' $config | sed s'/^ //'
56 }
58 # Create a Lua configuration file for lsyncd.
59 gen_config() {
60 mkdir -p $(dirname $config)
61 cat > $config << EOT
62 -- Configuration file for SliTaz Secure Filesystem lsyncd daemon.
64 sync {
65 default.rsyncssh,
66 source = "$HOME/Sync",
67 host = "$login@$host",
68 targetdir = "Sync/"
69 }
70 EOT
71 }
73 #
74 # Commands
75 #
77 case "$1" in
78 info)
79 size=$(du -sh $HOME/Sync | awk '{print $1}')
80 echo "" && config_info && echo ""
81 gettext "RSA key :"; echo " $HOME/.ssh/id_rsa"
82 gettext "Sync size :"; echo " $size"
83 echo "" ;;
84 setup)
85 # We need a login and host name or IP.
86 if [ -z "$login" ] || [ -z "$host" ]; then
87 gettext -e "\nUsage:"; echo -e \
88 " $(basename $0) setup --login=user --host=server\n" && exit 0
89 fi
90 gen_config
91 mkdir -p $logdir $(dirname $pid)
93 # Configure passwordless login via RSA key.
94 if [ ! -f "$HOME/.ssh/id_rsa" ]; then
95 mkdir -p $HOME/.ssh
96 ssh-keygen -b 2048 -f $HOME/.ssh/id_rsa -P ''
97 fi
99 # Upload key to the server.
100 gettext "Sending RSA secure key to:"; echo " $host"
101 cat $HOME/.ssh/id_rsa.pub | \
102 ssh $login@$host 'cat - >> ~/.ssh/authorized_keys' || exit 1
103 gettext "Client is setup you can now sync"; echo ;;
104 sync)
105 # Sync can be called at session startup or from cmdline to retrive
106 # files from the server.
107 [ ! -f "$config" ] && gettext -e "Missing config file\n" && exit 1
108 at=$(fgrep host $config | cut -d '"' -f 2)
109 login=${at%@*}
110 host=${at#*@}
111 gettext -e "\nSyncing from:"; echo " $login@$host"
113 # First sync host with server.
114 rsync -r -a -v -h -z -u --delete \
115 --log-file=$logdir/rsync.log \
116 -e "ssh -l $login" $host:~/Sync/ ~/Sync/ | \
117 sed -e "/^$/"d -e "/^.\/$/"d
119 # Monitor local folder if not yet running.
120 if [ ! -f "$pid" ]; then
121 gettext "Starting lsyncd daemon..."
122 lsyncd -pidfile $pid -log all -logfile $logdir/lsyncd.log $config
123 else
124 gettext "Lcyncd daemon running PID:"; echo " $(cat $pid)"
125 fi && echo "" ;;
126 stop)
127 # Kill daemon and remove pidfile.
128 [ ! -f "$pid" ] && gettext -e "Daemon is not running\n" && exit 0
129 kill=$(cat $pid)
130 gettext "Stopping"; echo " $app PID: $kill"
131 kill $kill 2>/dev/null
132 rm -f $pid ;;
133 *)
134 help ;;
135 esac
136 exit 0