refcodes-batch.1.0.2.source-code.create-db-backup.job Maven / Gradle / Ivy
#!/bin/bash
. $SCRIPT_DIR/lib-filesystem.inc
if [[ $HELP = y ]] ; then
echo "The switch \"-j $JOB\" uses a given configuration to backup a " >&2
echo "given DB. The DB dump is archived and placed into a given folder with rotating " >&2
echo "filenames. " >&2
# echo "--------------------------------------------------------------------------------" >&2
logSeparator
fi
. $SCRIPT_DIR/validate-db-config.job
. $SCRIPT_DIR/validate-db-backup-config.job
if [[ $HELP = y ]] ; then
exit $EXIT_SUCCESS
fi
exitOnConfigOff "DB"
exitOnConfigOff "DB_BACKUP"
DAY_OF_MONTH=`date '+%d'`
ARCHIVE_NAME=db-backup-$DAY_OF_MONTH-$DB_BACKUP_BASE_NAME.sql.gz
ARCHIVE_FILE=$DB_BACKUP_DIR/$ARCHIVE_NAME
if [ ! -e "$DB_BACKUP_DIR" ] ; then
if [[ $VERBOSE = y ]] ; then
echo "Creating/configuring DB backup path \"$DB_BACKUP_DIR\" ..." >&2
fi
makePath "$DB_BACKUP_DIR" "$DB_BACKUP_FS_USER" "$DB_BACKUP_FS_GROUP" "u+rw,go-rwx"
exitOnError "Unable to create directory \"$DB_BACKUP_DIR\" !!!"
fi
if [[ $VERBOSE = y ]] ; then
echo "Dumping DB \"$DB_NAME\" to archive \"$ARCHIVE_FILE\" ..." >&2
fi
mysqldump --opt -h localhost -u$DB_USER_NAME -p$DB_USER_PASSWORD $DB_NAME | gzip > $ARCHIVE_FILE
exitOnError "Unable to dump DB \"$DB_NAME\" !!!"
if [[ $VERBOSE = y ]] ; then
echo "Applying file access rights \"$DB_BACKUP_FS_USER:$DB_BACKUP_FS_GROUP\" to \"$ARCHIVE_FILE\" ..." >&2
fi
chown $DB_BACKUP_FS_USER:$DB_BACKUP_FS_GROUP $ARCHIVE_FILE
# exitOnError "Unable to change owner to \"$DB_BACKUP_FS_USER:$DB_BACKUP_FS_GROUP\" for \"$ARCHIVE_FILE\" !!!"
warnOnError "Unable to change owner to \"$DB_BACKUP_FS_USER:$DB_BACKUP_FS_GROUP\" for \"$ARCHIVE_FILE\" !!!"
chmod u+rw,g-rwx,o-rwx $ARCHIVE_FILE
exitOnError "Unable to change access rights for \"$ARCHIVE_FILE\" !!!"
if [[ $VERBOSE = y ]] ; then
# echo "--------------------------------------------------------------------------------" >&2
logSeparator
fi
echo "Backup for DB \"$DB_NAME\" was created:" >&2
# echo "--------------------------------------------------------------------------------" >&2
logSeparator
echo "+ For DB backup archive see: \"$ARCHIVE_FILE\"" >&2
# echo "--------------------------------------------------------------------------------" >&2
logSeparator
showSuccess "Created backup of database \"$DB_NAME\" !"