Last active
May 14, 2022 03:42
-
-
Save dimitris-k/8d1fa37eeb8e1423c483c8155b62bc46 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
function __msg_error() { | |
printf 'ERROR from line: %s %s\n' "${BASH_LINENO[$(( ${#BASH_LINENO[@]} - 2 ))]}" "$*" | |
} | |
# Do not run if an instance is already running | |
# ------------------------------------------------------------------------------------- | |
RFLAG="/tmp/___RUNNING_FLAG___$( basename "${BASH_SOURCE[0]}" )" | |
mkdir "$RFLAG" 2>/dev/null || { __msg_error "There is an already running instance of this program"; exit 1; } | |
# some functions | |
# ------------------------------------------------------------------------------------- | |
on_exit() { | |
__msg_error "Abnormal Exit" | |
rmdir "$RFLAG" 2>/dev/null | |
} | |
trap on_exit EXIT # Enable emergency handler | |
clean_exit () { | |
rmdir "$RFLAG" 2>/dev/null | |
trap - EXIT # Disable emergency handler | |
exit 0 | |
} | |
LOG_FILE="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd -P )/LOG__$( basename "${BASH_SOURCE[0]}" )" | |
get_date () { | |
local day_str | |
day_str=$(date +'%A') | |
day_str=$(printf "%s%0$((10 - ${#day_str}))d" "$day_str" 0) | |
echo -n "${day_str//0/_}$(date "+%d-%m-%Y__%H.%M.%S")" | |
} | |
date=$(get_date) | |
HOST="my_dbserver" | |
DB="my_database" | |
USER="my_database_admin" | |
PASSWD="a_simple_passwd" | |
# exec and redirect stdout to log file | |
# ------------------------------------------------------------------------------------- | |
touch "$LOG_FILE" | |
exec >>"$LOG_FILE" 2>&1 | |
printf '\n\nExporting '%s' database on %s ... ' "$DB" "$date" | |
BACKUP_DIR=/srv/mysql_exports | |
export TZ=Europe/Athens | |
umask 077 | |
keep=5 | |
mkdir -p "$BACKUP_DIR" || { __msg_error "Could not create directory $BACKUP_DIR. Exiting"; exit 1; } | |
cd "$BACKUP_DIR" 2>/dev/null || { __msg_error "Could not change directory to $BACKUP_DIR. Exiting"; exit 1; } | |
ignored_tables=( | |
'--ignore-table=my_database.table1' | |
'--ignore-table=my_database.table2' | |
'--ignore-table=my_database.table3' | |
'--ignore-table=my_database.table4' | |
'--ignore-table=my_database.table5' | |
'--ignore-table=my_database.table6' | |
'--ignore-table=my_database.log' | |
'--ignore-table=my_database.log_archive' | |
) | |
/usr/bin/mysqldump \ | |
--host="$HOST" \ | |
--user="$USER" --password="$PASSWD" \ | |
--opt --allow-keywords \ | |
--add-drop-database \ | |
--single-transaction \ | |
"${ignored_tables[@]}" \ | |
"$DB" > "${HOST}__${DB}__export__${date}.sql" | |
# echo -n "Compressing Exported Data ... " | |
# gzip --quiet "${HOST}__${DB}__export__${date}.sql" | |
delete_old_exports() { | |
# Delete all dumps older than $keep days, or the 1st of any month | |
# ------------------------------------------------------------------------------------- | |
# accepts two parameters: | |
# keep: how many exports to keep (assuming every day an export is performed) | |
# ext: the extension of the files to inspect | |
(( $# != 2 )) && { __msg_error "inadequate parameters passed to function ${FUNCNAME[0]}"; exit 1; } | |
[[ ${1+isset} = isset ]] || { __msg_error "keep parameter not passed to function ${FUNCNAME[0]}"; exit 1; } | |
[[ ${2+isset} = isset ]] || { __msg_error ".ext parameter not passed to function ${FUNCNAME[0]}"; exit 1; } | |
[ ! -z "${1##*[!0-9]*}" ] || { __msg_error "keep parameter passed to ${FUNCNAME[0]}() is not a possitive integer"; exit 1; } | |
keep=$(( "$1" - 1 )); ext="$2" | |
printf '\nDeleting old exports matching *.%s...\n' "$ext" | |
find . -name '*.'"$ext" -type f -mtime +${keep} -print0 | \ | |
while read -r -d '' old_export; do | |
# day is %d | |
day=$(date +%d --date "@$(stat --format '%Z' "$old_export")") | |
if [[ $day != "01" ]]; then | |
echo " deleting" "$old_export" | |
rm -f "$old_export" | |
fi | |
done | |
echo 'Deletions completed' | |
} | |
delete_old_exports "$keep" "sql" | |
printf '\nDone\n---\n' | |
# delete the semaphore directory and exit | |
# ------------------------------------------------------------------------------------- | |
clean_exit | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment