ssfs view ssfs @ rev 55

ssfs: add connect command
author Christophe Lincoln <pankso@slitaz.org>
date Sun Jun 12 20:55:51 2011 +0200 (2011-06-12)
parents 0409a8244cb8
children 5d4e3644da14
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.")
44 connect $(gettext "Connect to remote host via SSH.")
46 $(echo -e "\033[1m$(gettext "Options:")\033[0m")
47 --login= $(gettext "Login name on remote host.")
48 --host= $(gettext "Server host name or IP.")
50 EOT
51 }
53 # Display config settings.
54 config_info() {
55 [ ! -f "$config" ] && exit 0
56 fgrep '=' $config | sed s'/^ //'
57 }
59 # Create a Lua configuration file for lsyncd.
60 gen_config() {
61 mkdir -p $(dirname $config)
62 cat > $config << EOT
63 -- Configuration file for SliTaz Secure Filesystem lsyncd daemon.
65 sync {
66 default.rsyncssh,
67 source = "$HOME/Sync",
68 host = "$login@$host",
69 targetdir = "Sync/"
70 }
71 EOT
72 }
74 get_config() {
75 at=$(fgrep host $config | cut -d '"' -f 2)
76 login=${at%@*}
77 host=${at#*@}
78 }
80 #
81 # Commands
82 #
84 case "$1" in
85 info)
86 size=$(du -sh $HOME/Sync | awk '{print $1}')
87 echo "" && config_info && echo ""
88 gettext "RSA key :"; echo " $HOME/.ssh/id_rsa"
89 gettext "Sync size :"; echo " $size"
90 echo "" ;;
91 setup)
92 # We need a login and host name or IP.
93 if [ -z "$login" ] || [ -z "$host" ]; then
94 gettext -e "\nUsage:"; echo -e \
95 " $(basename $0) setup --login=user --host=server\n" && exit 0
96 fi
97 gen_config
98 mkdir -p $logdir $(dirname $pid)
100 # Configure passwordless login via RSA key.
101 if [ ! -f "$HOME/.ssh/id_rsa" ]; then
102 mkdir -p $HOME/.ssh
103 ssh-keygen -b 2048 -f $HOME/.ssh/id_rsa -P ''
104 fi
105 echo ""
107 # Upload key to the server.
108 gettext "Sending RSA secure key to:"; echo " $host"
109 gettext "Please enter you Ssfs password."
110 echo -e "\n"
111 cat $HOME/.ssh/id_rsa.pub | \
112 ssh $login@$host 'cat - >> ~/.ssh/authorized_keys' || exit 1
113 gettext "Client is setup you can now sync"; echo ;;
114 sync)
115 # Sync can be called at session startup or from cmdline to retrive
116 # files from the server.
117 [ ! -f "$config" ] && gettext -e "Missing config file\n" && exit 1
118 at=$(fgrep host $config | cut -d '"' -f 2)
119 login=${at%@*}
120 host=${at#*@}
121 gettext -e "\nSyncing from:"; echo " $login@$host"
123 # First sync host with server.
124 rsync -r -a -v -h -z -u --delete \
125 --log-file=$logdir/rsync.log \
126 -e "ssh -l $login" $host:~/Sync/ ~/Sync/ | \
127 sed -e "/^$/"d -e "/^.\/$/"d
129 # Monitor local folder if not yet running.
130 if [ ! -f "$pid" ]; then
131 gettext "Starting lsyncd daemon..."
132 lsyncd -pidfile $pid -log all -logfile $logdir/lsyncd.log $config
133 else
134 gettext "Lcyncd daemon running PID:"; echo " $(cat $pid)"
135 fi && echo "" ;;
136 stop)
137 # Kill daemon and remove pidfile.
138 [ ! -f "$pid" ] && gettext -e "Daemon is not running\n" && exit 0
139 kill=$(cat $pid)
140 gettext "Stopping"; echo " $app PID: $kill"
141 kill $kill 2>/dev/null
142 rm -f $pid ;;
143 connect)
144 # Connect user to Ssfs server via SSH.
145 get_config
146 ssh $login@$host ;;
147 *)
148 help ;;
149 esac
150 exit 0