- POLICY-3000Getting issue details... STATUS
Recover from corruption of policy database. [Possibly a bug in mariadb: MDEV-23119]
High Level Design
A Kubernetes cron will run a docker container once every 24 hours.
The docker container will call a shell script to do the backup.
The backups will be stored remotely.
Dockerfile
FROM mariadb:latest RUN apk update COPY db_backup.sh . ENTRYPOINT [ "/bin/sh" ] CMD [ "./db_backup.sh" ]
Shell script
#!/bin/sh FILENAME=$(basename $0 .sh)_$(date +%F-%T) ARCHIVE=${FILENAME}.tar.gz LOG=${FILENAME}.log LOG_DIR=/mnt/c/mariadb/logs BACKUP_WORKDIR=/mnt/c/mariadb/backup BACKUP_ARCHIVEDIR=/mnt/c/mariadb/backup_archives MARIADB_BACKUP_EXE_DIR="/usr/bin" HOST=localhost PORT=3355 DB_USER=p#### DB_PWD=****** OK_MSG="completed OK!" cd / # if the backup directory doesn't exist, create it if [ ! -d $BACKUP_WORKDIR ]; then mkdir -p $BACKUP_WORKDIR fi # if the archive directory doesn't exist, create it if [ ! -d $BACKUP_ARCHIVEDIR ]; then mkdir -p $BACKUP_ARCHIVEDIR fi # if the log directory doesn't exist, create it if [ ! -d $LOG_DIR ]; then mkdir -p $LOG_DIR fi echo "Backup Started" echo "Backing up to $BACKUP_WORKDIR" # check if directory is empty, if not delete it's contents if [ "$(ls -A $BACKUP_WORKDIR)" ]; then rm -rf $BACKUP_WORKDIR/* fi # redirect output to log file { "$MARIADB_BACKUP_EXE_DIR"/mariabackup --backup --target-dir=$BACKUP_WORKDIR --user=$DB_USER --password=$DB_PWD --host=$HOST --port=$ PORT } >$LOG_DIR/$LOG 2>&1 echo >&2 "Backup Complete" # create a tar archive from the backup directory tar cvzf $BACKUP_ARCHIVEDIR/$ARCHIVE $BACKUP_WORKDIR >/dev/null 2>/dev/null # remove he backup directory rm -rf $BACKUP_WORKDIR/* >/dev/null 2>/dev/null # check the log for the "completed OK!" message grep -q "$OK_MSG" $LOG_DIR/$LOG if [ $? -eq 0 ]; then exit 0 else exit 1 fi
Links
Backing Up and Restoring Databases
Openstack MariaDB database backup and restore
mariabackup is an open source tool for doing back ups on MariaDB.
Incremental Backup and Restore with Mariabackup
Back up databases using Kubernetes CronJobs
Replication as a Backup Solution
Notes
We may need to create a user specifically for doing backups:
create user backup identified by '******';
GRANT RELOAD, PROCESS, LOCK TABLES, REPLICATION CLIENT, REPLICATION SLAVE ON *.* TO 'backup'@'%';
mariabackup isn't designed to work remotely. When testing it got stuck in a loop when trying to read the logs.
mysqldump: This can be used to backup mariadb running in a docker container :
docker exec <mariadb_container_name> mysqldump [--user <db username>] [--password= <db password>] <db name> > /<backup path>/db.dump
It can also be run inside a container:
mysqldump --all-databases --add-drop-database --compact --routines --host=<hostname> --port=<port> --user=<db user> --password=<db password> --log-error=/tmp/<filename>.err > /tmp/<dmp file name>.dmp