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=/c/mariadb/logs
BACKUP_WORKDIR=/c/mariadb/backup
BACKUP_ARCHIVEDIR=/c/mariadb/backup_archives
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_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 --password=$PWD
} >$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 bckupbackup 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

...