summaryrefslogtreecommitdiff
path: root/ppsnapback-install
blob: a42a84c0cc0b8f957708e7f2746f44831e18a68a (plain)
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#!/bin/bash
#
# ppsnapback-install - ppsnapback installation wizard
#
# Copyright (C) 2011 Nicolas Schodet.
#
# See end of file for documentation, copyright and license.
#
# Man page can be generated using:
#
# $ pod2man -c '' -r '' ppsnapback-install > ppsnapback-install.1
#

fatal () {
    echo $* >&2
    exit 1
}
usage () {
    fatal "Usage: $0 [options] MODULE [BACKUP_DIR]"
}
ask () {
    local question=$1
    local default=$2
    local validate=$3
    local response
    [[ -n $default ]] && question="$question [$default]"
    while true; do
	echo -n "$question " >&2
	read response
	[[ -z $response ]] && response="$default"
	[[ -z $validate ]] && break
	response=$($validate "$response") && break
    done
    echo "$response"
}
validate_yesno () {
    case "$1" in
	[yY]|[yY][eE][sS]) echo yes ;;
	[nN]|[nN][oO]) echo no ;;
	*)
	    echo "please answer yes or no" >&2
	    return 1
	    ;;
    esac
    return 0
}

# Check parameter and check configuration does not exists.
btype=push
bpath=
hostname=ASK
remote=
rppsnapback=
while getopts 't:p:h:r:R:' option; do
    case "$option" in
	t) btype="$OPTARG" ;;
	p) bpath="$OPTARG" ;;
	h) hostname="$OPTARG" ;;
	r) remote="$OPTARG" ;;
	R) rppsnapback="$OPTARG" ;;
	?) usage ;;
    esac
done
shift $((OPTIND-1))

MODULE=${1:-NONE}
BASEDIR=${2:-$HOME/backups}
MD=$BASEDIR/$MODULE

[[ $MODULE = NONE ]] && usage
[[ ! -d $BASEDIR ]] && fatal "backup directory '$BASEDIR' does not exist"
[[ -e $MD ]] && fatal "module directory for '$MODULE' exists"

# Cleanup code.
cleanup () {
    echo "cleaning up..."
    [[ -f $MD/config ]] && rm "$MD/config"
    [[ -f $MD/exclude ]] && rm "$MD/exclude"
    [[ -f $MD/$MODULE-backup-key ]] && rm "$MD/$MODULE-backup-key"
    [[ -f $MD/$MODULE-backup-key.pub ]] && rm "$MD/$MODULE-backup-key.pub"
    [[ -d $MD ]] && rmdir "$MD"
}

# Request functions.
validate_path () {
    if [[ ! -d $1 ]]; then
	echo "path does not exist" >&2
	return 1
    fi
    echo "$1"
}
need_path () {
    [[ -d $bpath ]] && return
    bpath=$(ask "Path to backup?" "$PWD/" validate_path)
}
need_hostname () {
    [[ $hostname = ASK ]] || return
    local use_hostname=$(ask "Use from= in SSH authorized_keys?" yes \
	validate_yesno)
    if [[ $use_hostname = yes ]]; then
	hostname=$(ask "Hostname?" "$(hostname -f)")
    else
	hostname=
    fi
}
validate_remote () {
    case "$1" in
	*:*)
	    echo "remote should not include path, only host" >&2
	    return 1
	    ;;
	'')
	    echo "please enter something" >&2
	    return 1
	    ;;
    esac
    echo "$1"
}
need_remote () {
    if [[ -n $remote ]]; then
	remote=$(validate_remote "$remote") || exit 1
    else
	remote=$(ask "Remote host?" '' validate_remote)
    fi
}
validate_rppsnapback () {
    case "$1" in
	/?*) echo "$1" ;;
	*)
	    echo "please enter absolute path" >&2
	    return 1
	    ;;
    esac
}
need_rppsnapback () {
    [[ -n $rppsnapback ]] && return
    rppsnapback=$(ask "Path to remote ppsnapback script?" \
	'/usr/local/bin/ppsnapback' validate_rppsnapback)
}

# Proceed with installation.
case "$btype" in
    push)
	need_path
	need_hostname
	need_remote
	need_rppsnapback
	trap cleanup EXIT
	mkdir "$MD" || fatal "cannot create module directory"
	ssh-keygen -t rsa -f "$MD/$MODULE-backup-key" \
	    -C "${hostname:+$hostname }$MODULE backup key" -N '' \
	    || fatal "cannot create backup key"
	cat > "$MD/config" <<-EOF || fatal "cannot write module configuration"
	path="$bpath"
	remote="$remote"
	privkey="$MODULE-backup-key"
	EOF
	touch "$MD/exclude"
	authorized_keys="${hostname:+from=\"$hostname\",}\
command=\"$rppsnapback $MODULE\",\
no-pty,no-port-forwarding,no-x11-forwarding,no-agent-forwarding \
$(< "$MD/$MODULE-backup-key.pub")"
	RMD="backups/$MODULE"
	echo $authorized_keys | ssh "$remote" "mkdir \"$RMD\" \
	    && touch \"$RMD/config\" \
	    && cat >> .ssh/authorized_keys" || fatal "cannot configure remote"
	;;
    *)
	fatal "only push type supported"
	;;
esac
trap - EXIT

exit 0

__END__

=head1 NAME

ppsnapback-install - ppsnapback installation wizard

=head1 SYNOPSIS

B<ppsnapback-install> [I<options>] I<module> [I<backup_dir>]

=head1 DESCRIPTION

B<ppsnapback-install> will ask for parameters to create a new B<ppsnapback>
module.  This include:

=over

=item * creation of the local configuration,

=item * creation of a SSH backup key,

=item * creation of the remote configuration,

=item * update of remote authorized_keys file.

=back

=head1 OPTIONS

Options can be used to avoid a parameter prompt.  If a parameter is missing,
B<ppsnapback-install> will ask the user to provide it.

=over

=item B<-t> I<type>

Choose configuration type.  For the moment, the only syported type is I<push>.

=item B<-p> I<path>

Path to backup.  Will default to current directory.

=item B<-h> I<hostname>

Local machine hostname.  This is used to limit SSH authorization to the
current machine address.  Use an empty string to disable.

=item B<-r> I<remote>

Remote machine or location.

=item B<-R> I<path_to_ppsnapback>

Absolute path to ppsnapback on the remote machine.

=back

=head1 RESTRICTIONS

For the moment, the only supported configuration type is I<push>, which
corresponds to the paragraph "Push Remote Backup Repository With Snapshot
Rotation" in B<ppsnapback> man page.

Remote backup directory is always $HOME/backups.

=head1 SEE ALSO

ppsnapback(1)

=head1 AUTHOR

Nicolas Schodet, http://ni.fr.eu.org/ppsnapback

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2011 Nicolas Schodet.

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 2 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, write to the Free Software Foundation, Inc., 59 Temple
Place - Suite 330, Boston, MA 02111-1307, USA.

=cut