Skip to content
This repository has been archived by the owner on Feb 10, 2022. It is now read-only.

Commit

Permalink
Merge pull request #10 from cloudfoundry-incubator/PR-backup-and-rest…
Browse files Browse the repository at this point in the history
…ore-#160017872

Refactor bbr scripts to work for multi-master
  • Loading branch information
Urvashi Reddy authored Oct 19, 2018
2 parents 291d74f + afff3af commit 60ce483
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 22 deletions.
3 changes: 3 additions & 0 deletions jobs/bbr-etcd/spec
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ name: bbr-etcd
templates:
backup.erb: bin/bbr/backup
restore.erb: bin/bbr/restore
metadata.sh.erb: bin/bbr/metadata
pre-restore-lock.sh.erb: bin/bbr/pre-restore-lock
post-restore-unlock.sh.erb: bin/bbr/post-restore-unlock

packages: []

Expand Down
8 changes: 6 additions & 2 deletions jobs/bbr-etcd/templates/backup.erb
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
#!/bin/bash
#!/usr/bin/env bash

set -euo pipefail

source /var/vcap/jobs/etcd/bin/utils.sh

BBR_ARTIFACT_FILE_PATH="${BBR_ARTIFACT_DIRECTORY}/snapshotdb"

export ETCDCTL_API=3


<% if spec.bootstrap %>
echo "Taking etcd snapshot"

/var/vcap/packages/etcd/bin/etcdctl \
--cacert /var/vcap/jobs/etcd/config/etcdctl-ca.crt \
--cert /var/vcap/jobs/etcd/config/etcdctl.crt \
--key /var/vcap/jobs/etcd/config/etcdctl.key \
--endpoints "${etcd_endpoint_address}" \
snapshot save "${BBR_ARTIFACT_FILE_PATH}"
<% end %>
8 changes: 8 additions & 0 deletions jobs/bbr-etcd/templates/metadata.sh.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env bash

echo "---
restore_name: cfcr-etcd-snapshot
<% if spec.bootstrap %>
backup_name: cfcr-etcd-snapshot
<% end %>
"
36 changes: 36 additions & 0 deletions jobs/bbr-etcd/templates/post-restore-unlock.sh.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/usr/bin/env bash

set -euo pipefail

echo "Starting etcd"

TIMEOUT=60
if timeout "$TIMEOUT" /bin/bash <<EOF
#!/bin/bash
until /var/vcap/bosh/bin/monit start etcd; do
echo "starting etcd"
sleep 5
done
EOF
then
echo "monit has started etcd"
else
echo "monit was unable to start etcd after $TIMEOUT seconds"
exit 1
fi

if timeout "$TIMEOUT" /bin/bash <<EOF
#!/bin/bash
until /var/vcap/bosh/bin/monit summary | grep etcd | grep "running"; do
echo "waiting for etcd daemon to start"
sleep 5
done
EOF
then
echo "etcd daemon has started"
else
echo "etcd daemon was unable to start after $TIMEOUT seconds"
exit 1
fi
26 changes: 26 additions & 0 deletions jobs/bbr-etcd/templates/pre-restore-lock.sh.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/usr/bin/env bash

set -euo pipefail

echo "Stopping etcd"
/var/vcap/bosh/bin/monit stop etcd

TIMEOUT=60

if timeout "$TIMEOUT" /bin/bash <<EOF
#!/bin/bash
until /var/vcap/bosh/bin/monit summary | grep etcd | grep "not monitored" ; do
echo "waiting for etcd to stop..."
sleep 5
done
EOF
then
echo "Etcd stopped"
else
echo "Timed out stopping etcd after $TIMEOUT seconds"
exit 1
fi

echo "Deleting old etcd data"
rm -rf /var/vcap/store/etcd
23 changes: 3 additions & 20 deletions jobs/bbr-etcd/templates/restore.erb
Original file line number Diff line number Diff line change
@@ -1,36 +1,19 @@
#!/bin/bash
#!/usr/bin/env bash

set -euo pipefail

source /var/vcap/jobs/etcd/bin/utils.sh

exec 1>&2

export BBR_ARTIFACT_FILE_PATH="${BBR_ARTIFACT_DIRECTORY}/snapshotdb"
export ETCDCTL_API=3

/var/vcap/bosh/bin/monit unmonitor etcd
/var/vcap/jobs/bpm/bin/bpm stop etcd

while /var/vcap/jobs/bpm/bin/bpm pid etcd; do
echo "Waiting for etcd to exit"
sleep 1
done

echo "Deleting old etcd data"
rm -rf /var/vcap/store/etcd

echo "Restoring etcd data"

/var/vcap/packages/etcd/bin/etcdctl \
snapshot restore ${BBR_ARTIFACT_FILE_PATH} \
--name="<%= spec.id %>" \
--initial-cluster="${etcd_peers}" \
--initial-advertise-peer-urls="${etcd_peer_address}" \
--data-dir="/var/vcap/store/etcd"

chown -R vcap:vcap /var/vcap/store/etcd

echo "Starting etcd"
/var/vcap/jobs/bpm/bin/bpm start etcd
/var/vcap/bosh/bin/monit summary
/var/vcap/bosh/bin/monit monitor etcd
chown -R vcap:vcap /var/vcap/store/etcd

0 comments on commit 60ce483

Please sign in to comment.