ssfs view ssfs @ rev 73

Prepa for i18n
author Christophe Lincoln <pankso@slitaz.org>
date Mon Jun 13 20:57:43 2011 +0200 (2011-06-13)
parents 2143ab1630b5
children 00d35dfe62e4
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 # Internationalization
16 . /usr/bin/gettext.sh
17 TEXTDOMAIN='ssfs'
18 export TEXTDOMAIN
20 # Parse cmdline options.
21 for opt in $@
22 do
23 case "$opt" in
24 --login=*)
25 login=${opt#--login=} ;;
26 --host=*)
27 host=${opt#--host=} ;;
28 *)
29 continue ;;
30 esac
31 done
33 #
34 # Functions
35 #
37 # Built-in usage.
38 help() {
39 cat << EOT
41 $(echo -e "\033[1m$(gettext "Usage:")\033[0m") $app [command] [--option=]
43 $(echo -e "\033[1m$(gettext "Commands:")\033[0m")
44 help $(gettext "Display this short help usage.")
45 info $(gettext "Display configuration settings.")
46 setup $(gettext "Setup client configuration and RSA key.")
47 sync $(gettext "Sync from server and start daemon.")
48 stop $(gettext "Stop monitoring ~/Sync folder.")
49 login $(gettext "Connect to remote host via SSH.")
51 $(echo -e "\033[1m$(gettext "Options:")\033[0m")
52 --login= $(gettext "Login name on remote host.")
53 --host= $(gettext "Server host name or IP.")
55 EOT
56 }
58 # Display config settings.
59 config_info() {
60 [ ! -f "$config" ] && exit 0
61 fgrep '=' $config | sed s'/^ //'
62 }
64 # Create a Lua configuration file for lsyncd.
65 gen_config() {
66 mkdir -p $(dirname $config)
67 cat > $config << EOT
68 -- Configuration file for SliTaz Secure File Storage lsyncd daemon.
70 sync {
71 default.rsyncssh,
72 source = "$HOME/Sync",
73 host = "$login@$host",
74 targetdir = "Sync/"
75 }
76 EOT
77 }
79 get_config() {
80 at=$(fgrep host $config | cut -d '"' -f 2)
81 login=${at%@*}
82 host=${at#*@}
83 }
85 #
86 # Commands
87 #
89 case "$1" in
90 info)
91 size=$(du -sh $HOME/Sync | awk '{print $1}')
92 echo "" && config_info && echo ""
93 gettext "RSA key :"; echo " $HOME/.ssh/id_rsa"
94 gettext "Sync size :"; echo " $size"
95 echo "" ;;
96 setup)
97 # We need a login and host name or IP.
98 if [ -z "$login" ] || [ -z "$host" ]; then
99 gettext -e "\nUsage:"; echo -e \
100 " $(basename $0) setup --login=user --host=server\n" && exit 0
101 fi
102 gen_config
103 mkdir -p $logdir $(dirname $pid)
105 # Configure passwordless login via RSA key.
106 if [ ! -f "$HOME/.ssh/id_rsa" ]; then
107 mkdir -p $HOME/.ssh
108 ssh-keygen -b 2048 -f $HOME/.ssh/id_rsa -P ''
109 fi
110 echo ""
112 # Upload key to the server.
113 gettext "Sending RSA secure key to:"; echo " $host"
114 gettext "Please enter your Ssfs password."
115 echo -e "\n"
116 cat $HOME/.ssh/id_rsa.pub | \
117 ssh $login@$host 'cat - >> ~/.ssh/authorized_keys' || exit 1
118 gettext "Client is setup you can now sync"; echo ;;
119 sync)
120 # Sync can be called at session startup or from cmdline to retrive
121 # files from the server.
122 [ ! -f "$config" ] && gettext -e "Missing config file\n" && exit 1
123 at=$(fgrep host $config | cut -d '"' -f 2)
124 login=${at%@*}
125 host=${at#*@}
126 gettext -e "\nSyncing from:"; echo " $login@$host"
128 # First sync host with server.
129 rsync -r -a -v -h -z -u --delete \
130 --log-file=$logdir/rsync.log \
131 -e "ssh -l $login" $host:~/Sync/ ~/Sync/ | \
132 sed -e "/^$/"d -e "/^.\/$/"d
134 # Monitor local folder if not yet running.
135 killall lsyncd 2>/dev/null
136 gettext "Starting lsyncd daemon..."
137 lsyncd -pidfile $pid -log all -logfile $logdir/lsyncd.log $config
138 echo "" ;;
139 stop)
140 # Kill daemon and remove pidfile.
141 [ ! -s "$pid" ] && gettext -e "Daemon is not running\n" && exit 0
142 kill=$(cat $pid)
143 gettext "Stopping"; echo " $app PID: $kill"
144 kill $kill 2>/dev/null
145 rm -f $pid ;;
146 login)
147 # Connect user to Ssfs server via SSH.
148 get_config
149 ssh $login@$host ;;
150 *)
151 help ;;
152 esac
153 exit 0