Blame view

copy_crashplan_id 4.31 KB
Frederik Lindenaar authored
1
2
#!/bin/bash -e
#
Frederik Lindenaar authored
3
# copy_crashplan_id  -  shell scripts to copy the connection key from a remote
Frederik Lindenaar authored
4
5
6
7
#			(e.g. headless) Crashplan instance and setup the local
#			machine's Crashplan client to connect to it using port
#			forwarding over an open SSH connection. See also https://support.code42.com/CrashPlan/4/Configuring/Using_CrashPlan_On_A_Headless_Computer
#
Frederik Lindenaar authored
8
# Version 1.1, latest version at: https://gitlab.lindenaar.net/scripts/crashplan
Frederik Lindenaar authored
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#
# Copyright (c) 2016 Frederik Lindenaar
#
# This script is free software: you can redistribute and/or modify it under the
# terms of version 3 of the GNU General Public License as published by the Free
# Software Foundation, or (at your option) any later version of the license.
#
# This script 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, visit <http://www.gnu.org/licenses/> to download it.

### Configuration ###
HOST=					# name of machine runnig crashplan
USER=					# userid to use to login to machine
					# Leave blank for current user
Frederik Lindenaar authored
27
SRC_SYSTEM=Linux			# either Linux, Darwin, osx or detect
Frederik Lindenaar authored
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
					# Autodetects if blank (ssh conn, slow!)


### Implementation ###

# default settings, no need to change
PORT=4244
ADDR=127.0.0.1

# Support function to send an error message to STDERR and exit with status 1
die() { echo "Error: $*" >&2; exit 1; }

# Support function to determine destination location of .ui_info
# parameters:   $1 - name of file (for error msg) $2 os (output of `uname -s`)
# based on https://support.code42.com/CrashPlan/4/Configuring/Using_CrashPlan_On_A_Headless_Computer#Locations_Of_.ui_info
file_location() {
    case $2 in
  	OSX|osx|[Dd]arwin)
		if [ -d "~/Library/Application Support/CrashPlan" ]; then
			echo "~/Library/Application Support/CrashPlan/.ui_info"
		else
			echo "/Library/Application Support/CrashPlan/.ui_info"
		fi
		;;
  	[Ll]inux)
		echo "/var/lib/crashplan/.ui_info"
		;;
  	*)	die "$1 os $2 is not supported"
    esac
}
Frederik Lindenaar authored
59
myname=`basename "$0" .sh`
Frederik Lindenaar authored
60
61

# determine the connection parameters based on command line and configuration
Frederik Lindenaar authored
62
63
64
65
66
67
68
69
70
71
72
73
74
if [ $# -gt 2 -o "$1" == "-h" -o "$1" == "--help" -o $# -eq 0 -a -z "$HOST" ]; then
  cat<<EOH

usage: $myname [user@]hostname [linux|osx|detect]${HOST:+"
    or just $myname to copy from $HOST"}

Copy the connection key from a remote (e.g. headless) Crashplan instance and
setup the Crashplan client on local machine to connect to it using SSH port
forwarding (enable with ssh -L $PORT:$ADDR:4243 <name of Crashplan server>)

See also https://support.code42.com/CrashPlan/4/Configuring/Using_CrashPlan_On_A_Headless_Computer

EOH
Frederik Lindenaar authored
75
76
77
78
79
80
81
82
83
84
  exit 2
elif [ $# -gt 0 ]; then
  conn=$1
  HOST=`echo $conn | cut -d\@ -f 2 -`
  SRC_SYSTEM=${2:-$SRC_SYSTEM}
else
  conn=${USER:+$USER@}$HOST
fi

# ensure the host is reachable
Frederik Lindenaar authored
85
if ping -c 1 $HOST > /dev/null 2>&1; then
Frederik Lindenaar authored
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
  echo "copying Crashplan connect key from $HOST"
else
  die "unable to reach host $HOST"
fi

# check if sudo is needed and available, prompt for password now if required
if [ -w "$dst" ]; then
	sudo=
else
	sudo=`which sudo || true`
	if [ -z "$sudo" ]; then
		die "no write access to $dst and sudo not available"
	fi
	$sudo -p "Need root access to update $dst
Please enter your password for sudo: " -v
fi

# determine the src and dst files
os=`uname -s`
if [ -z "$SRC_SYSTEM" -o "$SRC_SYSTEM" == detect ]; then
  echo autodetecting OS type for $HOST
  SRC_SYSTEM=`ssh $conn uname -s`
fi
src=`file_location source "$SRC_SYSTEM"`
dst=`file_location destination "$os"`

# use script name to generate a temp file name, ensure the temp file is removed
tmpfile=`mktemp -t "$myname"`
trap 'rm -f "$tmpfile"' 0

# retrieve the key from the host and generate the new .ui_info file
if scp -q "$conn:$src" "$tmpfile"; then
	key=`cut -d, -f2 "$tmpfile"`
	echo "$PORT,$key,$ADDR" > "$tmpfile"
else
	die "unable to copy $src from $HOST"
fi

# check if the file is different, and if so put it in place
if $sudo cmp -s "$tmpfile" "$dst"; then
        echo "no update needed, $dst up to date"
elif $sudo install -C -b -m 0644 "$tmpfile" "$dst"; then
	echo "$dst updated for $HOST"
else
	die "$dst could not be updated"
fi

echo "Connect with: ssh -L $PORT:$ADDR:4243 $conn"