Skip to content
Snippets Groups Projects

mariabackup

The snippet can be accessed without any authentication.
Authored by Kasper Dahl Stad
Edited
mariabackup.sh 1.33 KiB
#!/bin/bash

export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"

BackupDir="/backup"

if [ "$EUID" -ne 0 ]; then
    echo "Error: You MUST run this script as root!"; exit 1
fi

if [ -f "${BackupDir}/.inprogress.lock" ]; then
    echo "lockfile exits backup cannot start"; exit 1
fi

touch "${BackupDir}/.inprogress.lock"

LatestFullBackupTime=$(stat -c %Y "${BackupDir}/full/xtrabackup_checkpoints")
LatestFullBackupToDate=$(date --date="@${LatestFullBackupTime}" +'%Y%m%d')

TodayDate=$(date +'%Y%m%d')

if [[ "${LatestFullBackupToDate}" -eq "${TodayDate}" ]] && [[ -f "${BackupDir}/latest/xtrabackup_checkpoints" ]]; then
    SeqFolderNumber=01
    until [ ! -d "${BackupDir}/incr${SeqFolderNumber}" ]; do
        SeqFolderNumber=$(printf "%02d" $(( 10#${SeqFolderNumber} + 1 )))
    done
    LatestBackupDir="${BackupDir}/incr${SeqFolderNumber}"
    mariabackup --backup --user=root --target-dir="${LatestBackupDir}" --incremental-basedir="${BackupDir}/latest"
else
    LatestBackupDir="${BackupDir}/full"
    find ${BackupDir} -mindepth 1 -maxdepth 1 -type d -not -name 'lost+found' -exec rm -rf {} \;
    mariabackup --backup --user=root --target-dir=${LatestBackupDir}
fi

if [ -f "${LatestBackupDir}/xtrabackup_checkpoints" ]; then
    ln -snf ${LatestBackupDir} "${BackupDir}/latest"
fi

rm -f "${BackupDir}/.inprogress.lock"

exit 0
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment