Versions Compared

Key

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

...

Code Block
languagejava
titledb_backup.sh
#!/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="/Cusr/Program Files/MariaDB 10.5/bin"
USER=p#####
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=$USER$DB_USER --password=$PWD$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

...