summaryrefslogtreecommitdiff
path: root/docker/mariadb/backup.sh
blob: 30796fa4ecb84f976c10f4a9ccd044e392813f48 (plain)
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
#!/usr/bin/env bash

set -eu
set -o pipefail

# Execute a full backup
backup_full() {
    echo "Taking a full backup"
    LAST_FULL_DATE=$(date +%d-%m-%Y)
    mariabackup \
        --defaults-file=/etc/mysql/my.cnf \
        --backup \
        --stream=xbstream \
        --history=$LAST_FULL_DATE | gzip > \
        $BACKUP_DIR/mysqlbackup-$(date +%d-%m-%Y-%s).qp.xbc.xbs.gz
    echo $LAST_FULL_DATE > $BACKUP_DIR/last_full_date
}

# Execute an incremental backup
backup_incremental() {
    echo "Taking an incremental backup"
    if [ -r $BACKUP_DIR/last_full_date ]; then
        LAST_FULL_DATE=$(cat $BACKUP_DIR/last_full_date)
    fi
    if [ -z $LAST_FULL_DATE ]; then
        LAST_FULL_DATE=$(date +%d-%m-%Y)
    fi
    mariabackup \
        --defaults-file=/etc/mysql/my.cnf \
        --backup \
        --stream=xbstream \
        --incremental-history-name=$LAST_FULL_DATE \
        --history=$LAST_FULL_DATE | gzip > \
        $BACKUP_DIR/incremental-$(date +%H)-mysqlbackup-$(date +%d-%m-%Y-%s).qp.xbc.xbs.gz
}

BACKUP_DIR=/backup/
cd $BACKUP_DIR

if [ -n $BACKUP_TYPE ]; then
    case $BACKUP_TYPE in
        "full")
        backup_full
        ;;
        "incremental")
        backup_incremental
        ;;
        *)
        echo "Only full or incremental options are supported."
        exit 1
        ;;
    esac
else
    echo "You need to specify either full or incremental backup options."
    exit 1
fi