Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagejava
titleDockerfile
FROM mariadb:latest

RUN apkapt update
COPY dbmysqldump_backup.sh .

ENTRYPOINT [ "/bin/sh" ]
CMD [ "./dbmysqldump_backup.sh" ]

Shell script

Code Block
languagejava
titledb_backup.sh
#!/bin/sh

FILENAME=$(basename $0 .sh)_$(date +%F-%T)
ARCHIVE=${FILENAME}.tar.gz
LOG
ERRFILE=${FILENAME}.err
DMPFILE=${FILENAME}.logdmp
LOG_DIR=/mnt/c/mariadb/logstmp
BACKUP_WORKDIR=/mnt/c/mariadb/backuptmp
BACKUP_ARCHIVEDIR=/mnt/c/mariadb/backup_archivestmp
MARIADB_BACKUP_EXE_DIR="/usr/bin"
HOST=localhost
PORT=3355
DB_USER=p####policy
DB_PWD=*******
OK_MSG="completed OK!"HOST=host.docker.internal
PORT=3355

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"

# checkcall 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
{
backup utility
{
"$MARIADB_BACKUP_EXE_DIR"/mariabackupmysqldump --all-databases --add-drop-database --backupcompact --targetroutines --dirhost=$BACKUP_WORKDIR$HOST --user=$DB_USER --password=$DB_PWD --host=$HOST --port=$
PORT
} >$LOG_DIR/$LOG 2>&1log-error=/tmp/$ERRFILE  > /tmp/$DMPFILE
}
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 $DMPFILE $BACKUP_WORKDIR/*$ERRFILE >/dev/null 2>/dev/null

# check the log for theerrors
"completed OK!" message
grep -q "$OK_MSG" $LOG_DIR/$LOG
if [ $? -eq 0 ];if [ -s "$BACKUP_WORKDIR/$ERRFILE" ]
then
   rm $BACKUP_WORKDIR/$DMPFILE $BACKUP_WORKDIR/$ERRFILE
   exit 0
else
   rm $BACKUP_WORKDIR/$DMPFILE $BACKUP_WORKDIR/$ERRFILE
   exit 1
fi

docker build . -t mysqldump_backup

To run create directory c:\mariadb\backup

docker run -v /mnt/c/mariadb/backup:/tmp mysqldump_backup

Your backup will be available in the tar.gz file in the c:\mariadb\backup directory once docker completes

Backing Up and Restoring Databases

...