~andrewjbeach/juju-ci-tools/make-local-patcher

« back to all changes in this revision

Viewing changes to juju-backup

  • Committer: Aaron Bentley
  • Date: 2014-02-28 16:40:22 UTC
  • mto: This revision was merged to the branch mainline in revision 257.
  • Revision ID: aaron.bentley@canonical.com-20140228164022-kfip2tphn9m9invi
Add juju-backup script.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/bin/bash
 
2
 
 
3
remote_cmd() {  
 
4
        LIBJUJU=/var/lib/juju
 
5
 
 
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.
 
9
        execute() {
 
10
                MSG=$1
 
11
                shift
 
12
                echo -n $MSG.....
 
13
                ERR=$( "$@" 2>&1 ) || {
 
14
                        echo FAILED
 
15
                        echo '------------------------------------------------------------'
 
16
                        echo "Command failed: $*"
 
17
                        echo "Error: $ERR"
 
18
                        echo '------------------------------------------------------------'
 
19
                        exit 1
 
20
                }
 
21
                echo SUCCESS
 
22
        }
 
23
        
 
24
        next_step() {
 
25
                echo
 
26
                echo '**************************************************************'
 
27
                echo $1
 
28
                echo '**************************************************************'
 
29
        }
 
30
 
 
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
 
37
        fi
 
38
        execute 'Making backup directory' mkdir juju-backup
 
39
        cd juju-backup
 
40
 
 
41
        # Mongo requires that a locale is set
 
42
        export LC_ALL=C
 
43
 
 
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 \
 
51
                --db juju \
 
52
                --collection settings \
 
53
                --out environconfig.json
 
54
        execute 'Starting mongo' start juju-db
 
55
        trap - 0
 
56
 
 
57
        next_step 'Copying Juju configuration'
 
58
        copy_files() {
 
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)
 
62
        }
 
63
        # Make copies of:
 
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-* \
 
74
                $LIBJUJU/tools \
 
75
                $LIBJUJU/server.pem \
 
76
                ~/.ssh/authorized_keys \
 
77
                /etc/rsyslog.d/*juju.conf \
 
78
                /var/log/juju/all-machines.log \
 
79
                /var/log/juju/machine-0.log \
 
80
 
 
81
        #---------------------------------------------------------------------
 
82
        next_step 'Creating tarball'
 
83
        cd ..
 
84
        execute 'Performing tar' tar -czf juju-backup.tgz juju-backup
 
85
        rm -r juju-backup
 
86
        execute 'Changing ownership of backup archive to ubuntu' chown -R ubuntu.ubuntu juju-backup*
 
87
        
 
88
        echo
 
89
        echo Juju backup finished.
 
90
        echo
 
91
}
 
92
 
 
93
# Run the backup script on the remote machine.
 
94
REMOTE_SCRIPT="
 
95
        $(declare -f remote_cmd)
 
96
        remote_cmd
 
97
"
 
98
 
 
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
 
107
}