Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/omegazeng/run-mariabackup
Browse files Browse the repository at this point in the history
…into omegazeng-master
  • Loading branch information
crishoj committed Mar 22, 2022
2 parents cfc8e6a + 040c773 commit c74af21
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 6 deletions.
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,15 @@ Note: have tested on Ubuntu 18.04 with MariaDB 10.3

## Create a backup user

GRANT RELOAD, PROCESS, LOCK TABLES, REPLICATION CLIENT ON *.* TO 'backup'@'localhost' identified by 'YourPassword';
FLUSH PRIVILEGES;
```sql
-- See https://mariadb.com/kb/en/mariabackup-overview/#authentication-and-privileges
CREATE USER 'backup'@'localhost' IDENTIFIED BY 'YourPassword';
-- MariaDB < 10.5:
GRANT RELOAD, PROCESS, LOCK TABLES, REPLICATION CLIENT ON *.* TO 'backup'@'localhost';
-- MariaDB >= 10.5:
GRANT RELOAD, PROCESS, LOCK TABLES, BINLOG MONITOR ON *.* TO 'backup'@'localhost';
FLUSH PRIVILEGES;
```

## Usage

Expand Down
47 changes: 47 additions & 0 deletions restoredb.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/bin/bash

# The Restore scripts for MariaDB Version 10.2 and above are slightly different
# than version 10.1 and previous. This script is designed to follow the conventions suggested on
# the official MariaDb Docs for version 10.2 and above. I have also attempted to automate
# preparing of all the incremental directories. It would take forever if you have a ton of them (eg. if you back up your DB every hour)
# Please consider the warnings below before you go and run this.
#WARNING : DO NOT RUN THIS SCRIPT Without understanding exactly what it does.
#BECAUSE Carnivorous lazer wielding baboons will come and EAT your database

# README
# What it basically does, by default is restore the latest increment. Reading the comments should guide you to figure out exactly what you want to do and restore to any point in time
# To restore to a specific point in time, comment out all lines after (and including) the WHILE loop. Then you can open the text file incremental_dirs.txt and simply
# delete all the directory paths after the point you want to restore. Then run only the remaining part of the script (DO NOT RUN the whole script again(
# Yeah I'm making it more complicated than it is - I probably need to write better instructions.
# Put this script in the same directory as your base and incr directories


for i in $(find . -name backup.stream.gz | xargs dirname) #Get all directory names of directories that contain the .gz file
do
echo $i >> folders.txt #Save all those directory names to a file
mkdir -p $i/backup #make a backup directory in all those folders
zcat $i/backup.stream.gz | mbstream -x -C $i/backup/ #Restore the database backup from the stream file (See MariaDB knowledge base for this)
done


sort folders.txt > folders_sorted.txt #If we are doing an incremental restore, then the increments have to be applied in correct order
sed 's/$/\/backup/' folders_sorted.txt > folders_backup.txt #the relevant data to be passed onto the mariadb argument is stored in the /backup folder within each directory
sed -i 's/\(.\{2\}\)//' folders_backup.txt #Remove the ./ characters at the start of each directory path - this causes the script to fail
BASE_DIR=`head -1 folders_backup.txt` #First line of the file is the full backup (Base Dir)
tail -n +2 folders_backup.txt > incremental_dirs.txt #Remaining lines are of the incremental directories

rm folders_sorted.txt folders_backup.txt folders.txt #Clean up the bloody mess


mariabackup --prepare --target-dir "$BASE_DIR" #Prepare the base_dir - read the maria docs - this sychronises somes stuff which will blow up the DB
while IFS="" read -r p || [ -n "$p" ] #iterate over the incremental_dirs file
do
mariabackup --prepare --target-dir "$BASE_DIR" --incremental-dir "$p" #Restore all incremental backups - merging it with the full backup
done < incremental_dirs.txt

sudo systemctl stop mysql #stop the db service so we don't blow things up while swapping stuff out
sudo mv /var/lib/mysql /var/lib/mysql_backup #back up the existing mysql directory. YOU MAY WANT TO CHANGE THIS, if you're using a different place to store your database
sudo mariabackup --copy-back --target-dir "$BASE_DIR" #restore the backup
sudo chown -R mysql:mysql /var/lib/mysql #set permissions right. YOU MAY WANT TO CHANGE THIS AS WELL
sudo systemctl start mysql #start back up with restored DB
rm incremental_dirs.txt #Cleaning up
16 changes: 12 additions & 4 deletions run-mariabackup.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
#!/bin/sh

# Create a backup user
# GRANT RELOAD, PROCESS, LOCK TABLES, REPLICATION CLIENT ON *.* TO 'backup'@'localhost' identified by 'YourPassword';
# CREATE USER 'backup'@'localhost' IDENTIFIED BY 'YourPassword';
# MariaDB < 10.5:
# GRANT RELOAD, PROCESS, LOCK TABLES, REPLICATION CLIENT ON *.* TO 'backup'@'localhost';
# MariaDB >= 10.5:
# GRANT RELOAD, PROCESS, LOCK TABLES, BINLOG MONITOR ON *.* TO 'backup'@'localhost';
# FLUSH PRIVILEGES;
#
# Usage:
Expand All @@ -12,10 +16,10 @@ MYSQL_USER=backup
MYSQL_HOST=localhost
MYSQL_PORT=3306
BACKCMD=mariabackup # Galera Cluster uses mariabackup instead of xtrabackup.
GZIPCMD=pigz -p8
GZIPCMD=gzip # pigz (a parallel implementation of gzip) could be used if available.
BACKDIR=/data/mysql_backup
FULLBACKUPCYCLE=604800 # Create a new full backup every X seconds
KEEP=3 # Number of additional backups cycles a backup should kept for.
KEEP=3 # Number of additional backups cycles a backup should be kept for.
LOCKDIR=/tmp/mariabackup.lock

ReleaseLockAndExitWithCode () {
Expand All @@ -40,6 +44,10 @@ GetLockOrDie () {
}

USEROPTIONS="--user=${MYSQL_USER} --password=${MYSQL_PASSWORD} --host=${MYSQL_HOST} --port=${MYSQL_PORT}"
# Arguments may include amongst others:
# --parallel=2 => Number of threads to use for parallel datafiles transfer. Default value is 1.
# --galera-info => Creates the xtrabackup_galera_info file which contains the local node state
# at the time of the backup. Option should be used when performing the backup of MariaDB Galera Cluster.
ARGS=""
BASEBACKDIR=$BACKDIR/base
INCRBACKDIR=$BACKDIR/incr
Expand Down Expand Up @@ -96,7 +104,7 @@ echo "Check completed OK"
# Find latest backup directory
LATEST=`find $BASEBACKDIR -mindepth 1 -maxdepth 1 -type d -printf "%P\n" | sort -nr | head -1`

AGE=`stat -c %Y $BASEBACKDIR/$LATEST`
AGE=`stat -c %Y $BASEBACKDIR/$LATEST/backup.stream.gz`

if [ "$LATEST" -a `expr $AGE + $FULLBACKUPCYCLE + 5` -ge $START ]
then
Expand Down

0 comments on commit c74af21

Please sign in to comment.