-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmount_shared.sh
More file actions
executable file
·153 lines (136 loc) · 5.25 KB
/
mount_shared.sh
File metadata and controls
executable file
·153 lines (136 loc) · 5.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/bin/bash -ue
# Mount/unmount remote sshfs file system (files.freecode.no / Shared )
# Copyright (C) 2011 Christian Bryn <cb@freecode.no>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
function do_unmount {
# unmount based on 'environment'
if [ "${is_mounted}" == "true" ]; then
if ( ! fusermount -u "${local_path}" ); then
while :; do
choice=$( zenity --title "FC Shared Mounter" --text "Unmounting $local_path failed!\nYou may have open files - close your open documents and try again.\n" --list --radiolist --column "" --column "Choice" TRUE "Retry" FALSE "Force unmount" FALSE "Cancel" )
case $choice in
"Retry")
fusermount -u "${local_path}" || continue
;;
"Force unmount")
fusermount -z -u "${local_path}" || { zenity --error --text "Even forced unmount failed - backing out..."; break; }
;;
"Cancel")
break
;;
esac
done
fi
fi
}
# TODO: z_err, z_info
function p_err {
# print errors
# params: <string>
local string="${@}"
if [ "${logging}" == "true" ]; then
printf "[ error ] %s - %s\n" "$(date)" "${string}" >> ${logfile}
else
printf "${b:-}${red:-}[ error ]${t_reset:-} %s - %s\n" "$(date)" "${string}"
fi
}
function p_info {
# print info
# params: <string>
local string="${@}"
if [ "${logging}" == "true" ]; then
printf "[ info ] %s - %s\n" "$(date)" "${string}" >> ${logfile}
else
printf "${b:-}${yellow:-}[ info ]${t_reset:-} %s - %s\n" "$(date)" "${string}"
fi
}
# fancy terminal stuff
if [ -t 1 ]; then
exec 3>&2 2>/dev/null
b=$( tput bold ) || true
red=$( tput setf 4 ) || true
green=$( tput setf 2 ) || true
yellow=$( tput setf 6 ) || true
t_reset=$( tput sgr0 ) || true
exec 2>&3; exec 3>&-
fi
# options
remote_server="files.freecode.no"
mount_options=""
username=$USER
local_path=~/Shared
remote_path="/shared/"
logging="false"
while getopts hu:m:Ur:s: o
do
case $o in
h)
print_usage
exit 0
;;
u)
username=$OPTARG
;;
m)
local_path=$OPTARG
;;
U)
# TODO: User feedback.
do_unmount
exit
;;
r)
remote_path=$OPTARG
;;
s)
remote_server=$OPTARG
;;
esac
done
shift $(($OPTIND-1))
which sshfs>/dev/null || zenity --info --title "sshfs missing" --text "SSHFS is not installed! Install or ask for help :)"
# TODO: check network connectivity
#if ( ping -q
# TODO: make is_mounted a function
if ( mount | grep -q "${remote_server}:${remote_path}" );
then
is_mounted="true"
else
is_mounted="false"
fi
if [ "${is_mounted}" == "true" ]; then
zenity --question --title "FC Shared" --text "FCNOOS Shared area is already mounted" --ok-label "Keep mounted" --cancel-label "Unmount" || { p_info "Unmounting ${remote_server}:${remote_path}"; do_unmount; }
exit $?
elif [ "${is_mounted}" == "false" ]; then
zenity --question --cancel-label "Abort" --text "Mount FCNOOS Shared as user ${username}?" || { p_info "User aborted, exiting..."; exit 0; }
else
zenity --warning --text "Stete of mount not known. This should not happen. Call for help."
exit 1
fi
if [ ! -d "${local_path}" ]; then
p_info "Creating local mount point ${local_path}"
mkdir "${local_path}" || { zenity --error --text "Could not mount Shared!\nCould not create local directory ${local_path}"; exit 1; }
fi
if ( ! sshfs ${sshfs_opts:-} ${username}@${remote_server}:${remote_path} ${local_path}/ ); then
zenity --error --text "Mounting of ${remote_server}:${remote_path} at ${local_path} failed!\nChecking for local files..."
files=$( find ${local_path}/ | wc -l )
if [[ "${files}" -gt 1 ]]; then
local_backup_path=~/Shared-local-$( date "+%Y-%m-%d_%H.%M" )
zenity --question --text "Found local files - moving them to ${local_backup_path}" || { p_info "User aborted moving of files, exiting."; exit 0; }
mkdir "${local_backup_path}" || { zenity --error --text "Could not create local directory ${local_backup_path}"; exit 1; }
mv ${local_path}/* ${local_backup_path}/ || { zenity --error --text "Could not move files from ${local_path} to ${local_backup_path}"; exit 1; }
fi
sshfs ${sshfs_opts:-} ${username}@${remote_server}:${remote_path} ${local_path}/ || { zenity --error --text "Still no go at mounting Shared - call for help!"; exit 1; }
fi