#!/bin/bash

DEPNOTICE='DEPRECATED: Use "juju backups create" instead of "juju backup".'

if [ "$1" == "--help" ]; then
	echo "juju backup"
	echo ""
	echo $DEPNOTICE
	echo ""
	echo "Backup state server of a juju environment"
	exit 0
fi

if [ "$1" == "--description" ]; then
	echo $DEPNOTICE
	exit 0
fi

# TODO(ericsnow) Remove this plugin once 1.21 is no longer supported.
>&2 echo $DEPNOTICE

# Grab the first version out of juju status (should be machine 0).
VERSION=$(juju status | grep 'agent-version:' | awk '{print $2}')

# Use the new backups CLI if newer than 1.21. Earlier versions did not
# have this plugin so we don't worry about them.
if [[ ! $VERSION =~ 1.20 && ! $VERSION =~ 1.21 ]]; then
    juju backups create
    exit $?
fi

##############################
# Fall back to the old plugin.

remote_cmd() {
	LIBJUJU=/var/lib/juju

	# usage: execute message cmd [arg...]
	# Execute the given command with the given arguments and exit with an
	# error on failure. The first argument (message) describes the command.
	execute() {
		MSG=$1
		shift
		echo -n $MSG.....
		ERR=$( "$@" 2>&1 ) || {
			echo FAILED
			echo '------------------------------------------------------------'
			echo "Command failed: $*"
			echo "Error: $ERR"
			echo '------------------------------------------------------------'
			exit 1
		}
		echo SUCCESS
	}

	next_step() {
		echo
		echo '**************************************************************'
		echo $1
		echo '**************************************************************'
	}

	cd ~			# Make sure we've started in $HOME
	next_step 'Preparing to perform backup'
	if [ -e juju-backup.tgz ]; then
		echo Older juju backup exists, moving to juju-backup-previous
		execute 'Removing existing backup archive' rm -rf juju-backup-previous.tgz
		execute 'Archiving backup' mv juju-backup.tgz juju-backup-previous.tgz
	fi
	execute 'Making backup directory' mkdir juju-backup
	cd juju-backup

	# Mongo requires that a locale is set
	export LC_ALL=C

	# Prefer jujud-mongodb binaries if available
	export MONGODUMP=mongodump
	if [ -f /usr/lib/juju/bin/mongodump ]; then
		export MONGODUMP=/usr/lib/juju/bin/mongodump;
	fi

	export MONGOEXPORT=mongoexport
	if [ -f /usr/lib/juju/bin/mongoexport ]; then
		export MONGOEXPORT=/usr/lib/juju/bin/mongoexport;
	fi


	#---------------------------------------------------------------------
	next_step 'Backing up mongo database'
	execute 'Stopping mongo' stop juju-db
	trap "start juju-db" 0		# ensure it starts again on failure
	execute 'Backing up mongo' $MONGODUMP --journal --dbpath $LIBJUJU/db
	execute 'Backing up environ config' $MONGOEXPORT \
		--dbpath $LIBJUJU/db \
		--db juju \
		--collection settings \
		--out environconfig.json
	execute 'Starting mongo' start juju-db
	trap - 0

	next_step 'Copying Juju configuration'
	copy_files() {
		# Make an archive within the main archive so that we
		# can easily preserve file ownership and other metadata.
		tar -cf root.tar "$@" 2>&1 | (grep -v 'Removing leading'; true)
	}
	# Make copies of:
	#   - Upstart configuration files for juju-db, machine agent, but not any unit agents.
	#   - Agent configuration directories in $LIBJUJU.
	#   (includes the config, server.pem, tools, but not any unit agents)
	#   - SSH authorized keys.
	#   - /etc/rsyslog.d/*juju* config files for the agents (ignore any unit agents)
	#  - Juju logs for machine 0 and all machines.
	execute 'Archiving selected files' copy_files \
		/etc/init/juju-db.conf \
		/etc/init/jujud-machine-*.conf \
		$LIBJUJU/agents/machine-* \
		$LIBJUJU/tools \
		$LIBJUJU/server.pem \
		$LIBJUJU/system-identity \
		$LIBJUJU/nonce.txt \
		$LIBJUJU/shared-secret \
		~/.ssh/authorized_keys \
		/etc/rsyslog.d/*juju.conf \
		/var/log/juju/all-machines.log \
		/var/log/juju/machine-0.log \

	#---------------------------------------------------------------------
	next_step 'Creating tarball'
	cd ..
	execute 'Performing tar' tar -czf juju-backup.tgz juju-backup
	rm -r juju-backup
	execute 'Changing ownership of backup archive to ubuntu' chown -R ubuntu.ubuntu juju-backup*

	echo
	echo Juju backup finished.
	echo
}

# Run the backup script on the remote machine.
REMOTE_SCRIPT="
	$(declare -f remote_cmd)
	remote_cmd
"

QUOTED_SCRIPT="'$(echo "$REMOTE_SCRIPT" | sed "s/'/'\"'\"'/g")'"
echo Connecting to machine 0
juju ssh 0 "sudo -n bash -c $QUOTED_SCRIPT" && {
	# The backup has succeeded; copy backup tarball locally.
	NOW=$(date '+%Y%m%d-%H%M')
	FILENAME=juju-backup-$NOW.tgz
	echo "Copying tarball to `pwd`/$FILENAME ..."
	juju scp 0:~/juju-backup.tgz ./$FILENAME
}
