6
# usage: execute message cmd [arg...]
7
# Execute the given command with the given arguments and exit with an
8
# error on failure. The first argument (message) describes the command.
13
ERR=$( "$@" 2>&1 ) || {
15
echo '------------------------------------------------------------'
16
echo "Command failed: $*"
18
echo '------------------------------------------------------------'
26
echo '**************************************************************'
28
echo '**************************************************************'
31
cd ~ # Make sure we've started in $HOME
32
next_step 'Preparing to perform backup'
33
if [ -e juju-backup.tgz ]; then
34
echo Older juju backup exists, moving to juju-backup-previous
35
execute 'Removing existing backup archive' rm -rf juju-backup-previous.tgz
36
execute 'Archiving backup' mv juju-backup.tgz juju-backup-previous.tgz
38
execute 'Making backup directory' mkdir juju-backup
41
# Mongo requires that a locale is set
44
#---------------------------------------------------------------------
45
next_step 'Backing up mongo database'
46
execute 'Stopping mongo' stop juju-db
47
trap "start juju-db" 0 # ensure it starts again on failure
48
execute 'Backing up mongo' mongodump --dbpath $LIBJUJU/db
49
execute 'Backing up environ config' mongoexport \
50
--dbpath $LIBJUJU/db \
52
--collection settings \
53
--out environconfig.json
54
execute 'Starting mongo' start juju-db
57
next_step 'Copying Juju configuration'
59
# Make an archive within the main archive so that we
60
# can easily preserve file ownership and other metadata.
61
tar -cf root.tar "$@" 2>&1 | (grep -v 'Removing leading'; true)
64
# - Upstart configuration files for juju-db, machine agent, but not any unit agents.
65
# - Agent configuration directories in $LIBJUJU.
66
# (includes the config, server.pem, tools, but not any unit agents)
67
# - SSH authorized keys.
68
# - /etc/rsyslog.d/*juju* config files for the agents (ignore any unit agents)
69
# - Juju logs for machine 0 and all machines.
70
execute 'Archiving selected files' copy_files \
71
/etc/init/juju-db.conf \
72
/etc/init/jujud-machine-*.conf \
73
$LIBJUJU/agents/machine-* \
76
~/.ssh/authorized_keys \
77
/etc/rsyslog.d/*juju.conf \
78
/var/log/juju/all-machines.log \
79
/var/log/juju/machine-0.log \
81
#---------------------------------------------------------------------
82
next_step 'Creating tarball'
84
execute 'Performing tar' tar -czf juju-backup.tgz juju-backup
86
execute 'Changing ownership of backup archive to ubuntu' chown -R ubuntu.ubuntu juju-backup*
89
echo Juju backup finished.
93
# Run the backup script on the remote machine.
95
$(declare -f remote_cmd)
99
QUOTED_SCRIPT="'$(echo "$REMOTE_SCRIPT" | sed "s/'/'\"'\"'/g")'"
100
echo Connecting to machine 0
101
juju ssh 0 "sudo -n bash -c $QUOTED_SCRIPT" && {
102
# The backup has succeeded; copy backup tarball locally.
103
NOW=$(date '+%Y%m%d-%H%M')
104
FILENAME=juju-backup-$NOW.tgz
105
echo "Copying tarball to `pwd`/$FILENAME ..."
106
juju scp 0:~/juju-backup.tgz ./$FILENAME