...
The backups will be stored remotely.
Dockerfile
Code Block |
---|
language | java |
---|
title | Dockerfile |
---|
|
FROM mariadb:latest
RUN apk update
COPY db_backup.sh .
ENTRYPOINT [ "/bin/sh" ]
CMD [ "./db_backup.sh" ] |
Shell script
Code Block |
---|
language | java |
---|
title | db_backup.sh |
---|
|
#!/bin/sh
LOG=$(basename $0 .sh)_$(date +%F-%T).log
LOG_DIR=/c/mariadb/logs
BACKUP_DIR1=/c/mariadb/backup1
BACKUP_DIR2=/c/mariadb/backup2
MARIADB_BACKUP_EXE_DIR="/C/Program Files/MariaDB 10.5/bin"
USER=p#####
PWD=******
OK_MSG="completed OK!"
# if the backup directory doesn't exist, create it
if [ ! -d $BACKUP_DIR1 ]; then
mkdir -p $BACKUP_DIR1
fi
# if the backup directory doesn't exist, create it
if [ ! -d $BACKUP_DIR2 ]; then
mkdir -p $BACKUP_DIR2
fi
# Find the backup directory with the oldest modification date
BACKUP_DIR=`ls -td $BACKUP_DIR1/ $BACKUP_DIR2/ | tail -1 | awk -F' ' '{print $NF}' | sed 's/.$//'`
# 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_DIR"
# check if directory is empty, if not delete it's contents
if [ "$(ls -A $BACKUP_DIR)" ]; then
rm -rf $BACKUP_DIR/*
fi
# redirect output to log file
{
"$MARIADB_BACKUP_EXE_DIR"/mariabackup --backup --target-dir=$BACKUP_DIR --user=$USER --password=$PWD
} >$LOG_DIR/$LOG 2>&1
echo >&2 "Backup Complete"
# 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 |
...