-
Notifications
You must be signed in to change notification settings - Fork 10
/
rhocs-oneshot-backup.sh
executable file
·101 lines (90 loc) · 3.86 KB
/
rhocs-oneshot-backup.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/bin/bash
##
## Script to back up to a mount point destination
## existing persistent volumes in OpenShift from Gluster/RHOCS
##
## Environment variables .ini file should be provided as argument
if [ $1 -a -f $1 ]
then
source $1
else
echo "Error: You have to provide a ini file with environment variables as a parameter
"
echo "Example: `basename $0` myvars.ini"
exit 1
fi
## Temporary file to put the list of Gluster volumes to backup
VOLLIST=/root/vollist-`date +%Y%m%d-%H:%M`.txt
## Provides Logging of this script in the dir specified below:
LOG="${LOGDIR}/`basename $0`-`date +%Y%m%d-%H:%M`.log"
exec &>> $LOG
BASTIONHOST=localhost
## Source and destination of backups
PARENTMOUNT=$PARENTDIR/backup-`date +%Y%m%d-%H%M` ## Parent directory for all volumes mounting
/usr/bin/mkdir $PARENTMOUNT
TARGET=$DESTINATION/backup-`date +%Y%m%d-%H%M` ## Daily/Hourly destination of backups
## Get list of Persistent Volumes in Gluster with Heketi till now and
## store them in file $VOLLIST , exclude gluster-block and heketidb
/usr/bin/heketi-cli volume list --server $HEKETI_CLI_SERVER --user $USERHEKETI --secret $SECRETHEKETI | /usr/bin/grep -v block | /usr/bin/grep -v heketidbstorage | /usr/bin/cut -d":" -f4 > $VOLLIST
## If converged mode, get name of gluster pod
if [ "$RHOCSMODE" = "converged" ]
then
## Get inside OCP cluster to get access of Gluster pod
/usr/bin/ssh $BASTIONHOST "/usr/bin/oc login $OCADDRESS -u $OCUSER -p $OCPASS"
POD=`/usr/bin/ssh $BASTIONHOST "/usr/bin/oc get pods --all-namespaces -o wide" | /usr/bin/grep glusterfs | /usr/bin/grep $GLUSTERSERVER | /usr/bin/tr -s " " | /usr/bin/cut -f2 -d" "`
echo $POD
/usr/bin/ssh $BASTIONHOST "/usr/bin/oc project $OCPROJECT"
fi
## For each volume in $VOLLIST create mount directory, mount it,
## and copy to destination (and umount)
/usr/bin/mkdir $TARGET
for VOLNAME in `/usr/bin/cat $VOLLIST`
do
## Create an specific name for snapshot with controlled timestamp
SNAPNAME=${VOLNAME}-snap-`date +%Y%m%d-%H%M`
## Create a dir to mount Gluster Snapshot based on Snapshot name
/usr/bin/mkdir -p $PARENTMOUNT/$SNAPNAME
## Create a one-off snapshot of Gluster volume to be used for backup.
## Depending on deployment mode: set of actions to interact with
## Gluster commands is different:
if [ "$RHOCSMODE" = "converged" ]
then
## Create snpashot and activate for VOLNAME
/usr/bin/ssh $BASTIONHOST "/usr/bin/oc rsh $POD /usr/sbin/gluster snapshot create $SNAPNAME $VOLNAME no-timestamp"
/usr/bin/ssh $BASTIONHOST "/usr/bin/oc rsh $POD /usr/sbin/gluster snapshot activate $SNAPNAME"
elif [ "$RHOCSMODE" = "independent" ]
then
## Create snpashot and activate for VOLNAME
/usr/bin/ssh $GLUSTERSERVER "/usr/sbin/gluster snapshot create $SNAPNAME $VOLNAME no-timestamp"
/usr/bin/ssh $GLUSTERSERVER "/usr/sbin/gluster snapshot activate $SNAPNAME"
else
echo "Error: parameter RHOCSMODE not set to converged or independent"
exit 1
fi
/usr/bin/mount -t glusterfs $GLUSTERSERVER:/snaps/$SNAPNAME/$VOLNAME $PARENTMOUNT/$SNAPNAME
echo "Backing up $SNAPNAME ..."
/usr/bin/mkdir -p $TARGET
/usr/bin/tar czf $TARGET/$SNAPNAME.tar.gz $PARENTMOUNT/$SNAPNAME/
/usr/bin/umount $PARENTMOUNT/$SNAPNAME
## Depending on deployment mode: set of actions to interact with
## Gluster commands is different:
if [ "$RHOCSMODE" = "converged" ]
then
## Delete Snapshot
/usr/bin/ssh $BASTIONHOST "/usr/bin/oc rsh $POD /usr/sbin/gluster snapshot delete $SNAPNAME"<<EOF
y
EOF
fi
if [ "$RHOCSMODE" = "independent" ]
then
## Delete Snapshot
/usr/bin/ssh $GLUSTERSERVER "/usr/sbin/gluster snapshot delete $SNAPNAME"<<EOF
y
EOF
fi
echo "Dismounting and deleting $SNAPNAME ..."
done
## Backing up Heketi DB
echo "Backing up Heketi DB..."
/usr/bin/heketi-cli db dump --server $HEKETI_CLI_SERVER --user $USERHEKETI --secret $SECRETHEKETI > $TARGET/heketidb-`date +%Y%m%d-%H%M`.json
exit 0