gitlabbackup.sh 1.61 KB
Newer Older
Dave Lane's avatar
Dave Lane committed
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
#!/bin/bash
#
# Author: dave@oerfoundation.org
# License: GPL v3 (or latest)
#
# the purpose of this script is to backup up the current docker-compose-deployed Gitlab instance into 
# a directory on the host, suitable for including in the host's incremental file backups.
#
DC=`which docker-compose`
DIR=/home/docker/git.oeru.org
CONTAINER=web
COMMAND='gitlab-rake gitlab:backup:create'
LOG=/var/log/gitlabbackup.log
DATE=`date`
DATESTR=`date +"%Y_%m_%d"`
TESTSTR='Creating backup archive:'
BUDIR=$DIR/backups
BUPAT="*_gitlab_backup.tar"
TMP=/tmp
#
# run the actual command
CWD=`pwd`
cd $DIR
# first check that the container's running
if [[ `$DC ps | grep -c "$CONTAINER"` == 1 ]] ; then 	
    # backup, using this command
    OUT=`$DC exec -T $CONTAINER $COMMAND`
    # check the output for "Creating backup archive:"
    RESULT=`echo "$OUT" | grep "$TESTSTR" -`
    #echo "Result = $RESULT"
    # get the name of the backup:
    BU=`echo $RESULT | sed "s/^$TESTSTR \(.*\.tar\).*$/\1/g"`
    #echo "Backup file: $BU"
    if [[ ! -z $BU ]] ; then 
        echo "created backup: $BU in $BUDIR"
	echo "protecting latest backup" 
	echo "mv $BUDIR/$BU $TMP"
	#echo `ls -l $BUDIR`
	mv $BUDIR/$BU $TMP
	#echo `ls -l $TMP/$BU`
	if [[ -f $TMP/$BU ]] ; then 
    	    echo "remove other backups in $BUDIR" 
	    echo "removing $BUDIR/$BUPAT"
            rm $BUDIR/$BUPAT
	    echo "releasing latest backup"
	    mv $TMP/$BU $BUDIR
	else 
	    echo "failed to protect backup, not removing other backups in $BUDIR"
	fi
    else 
        echo "failed to create a backup!"
    fi	
else
    echo "Container \"$CONTAINER\" does not appear to be running."
fi
cd $CWD