copy_crashplan_id 3.96 KB
#!/bin/bash -e
#
# copy_crashplan_id	shell scripts to copy the connection key from a remote
#			(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
#
# Version 1.0, latest version at: https://gitlab.lindenaar.net/scripts/crashplan
#
# 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
SRC_SYSTEM=Linux			# either Linux, Darwin, osx or detect 
					# 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
}


# determine the connection parameters based on command line and configuration
if [ $# -gt 2 -o $# -eq 0 -a -z "$HOST" ]; then
  echo "usage: $0 [user@]hostname [linux|osx|detect]"
  if [ -n "$HOST" ]; then
	echo "     or just $0 to copy from $HOST"
  fi
  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
if ping -o $HOST > /dev/null 2>&1; then
  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
myname=`basename "$0" .sh`
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"