Skip to content

Commit

Permalink
backup-mysql.sh: formatting fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ckujau committed Oct 21, 2022
1 parent 46d52e2 commit 37f98cd
Showing 1 changed file with 32 additions and 32 deletions.
64 changes: 32 additions & 32 deletions backup-mysql.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#
# We once had a one-liner to backup all our databases:
#
# $ mysqldump -AcfFl --flush-privileges | pbzip2 -c > backup.sql.bz2
# $ mysqldump -AcfFl --flush-privileges | pbzip2 -c > backup.sql.bz2
#
# However, most of the databases do not change much or do not change at all
# over the day, yet the resulting .bz2 had to be generated every day. The
Expand All @@ -29,17 +29,17 @@ fi

# Use 'gdate' if available, 'date' otherwise
if [ "$(which gdate)" ]; then
DATE=gdate
DATE="gdate"
else
DATE=date
DATE="date"
fi

$DATE
# We have checks too :-)
if [ "$2" = "-c" ]; then
for f in "$DIR"/*.bz2; do
printf "Processing $f ($(stat -c 'scale=0; %s / 1024' "$f" | bc -l)KB)..."
grep -q $(bzip2 -dc "$f" | openssl $HASH) $(echo "$f" | sed "s/.bz2$/.$HASH/")
for f in "${DIR}"/*.bz2; do
printf '..%s..' "Processing ${f} ($(stat -c 'scale=0; %s / 1024' "${f}" | bc -l)KB)..."
grep -q "$(bzip2 -dc "$f" | openssl ${HASH}) $(echo "${f}" | sed "s/.bz2$/.${HASH}/")"
if [ $? = 0 ]; then
echo "checksum OK"
else
Expand All @@ -51,7 +51,7 @@ fi

BEGIN=$($DATE +%s)
for db in $(mysql --batch --skip-column-names -e 'show databases' | sort); do
printf "Backing up "$db"...."
printf '..%s..' "Backing up ${db}...."
# - Use multiple-row INSERT syntax that include several VALUES lists
# - Continue even if an SQL error occurs during a table dump
# - Flush the MySQL server log files before starting the dump
Expand All @@ -61,7 +61,7 @@ for db in $(mysql --batch --skip-column-names -e 'show databases' | sort); do
# - Using --skip-dump-date (added in v5.0.52) so that the dump won't change unnecessarily.
# - Included stored routines
# - Include triggers for each dumped table
case "$db" in
case "${db}" in
performance_schema|information_schema)
# Access denied for user 'root'@'localhost' to database 'information_schema' when using LOCK TABLES
# > mysqldump incorrectly tries to LOCK TABLES on the information_schema database.
Expand All @@ -80,48 +80,48 @@ for db in $(mysql --batch --skip-column-names -e 'show databases' | sort); do
;;
esac

if [ -n "$DEBUG" ]; then
$DEBUG mysqldump "$OPTIONS" "$db" egrep -v -- '^-- Dump completed on' "$DIR"/DB_"$db".sql.new
if [ -n "${DEBUG}" ]; then
${DEBUG} mysqldump "${OPTIONS}" "${db}" grep -Ev -- '^-- Dump completed on' "${DIR}/DB_${db}.sql.new"
else
$DEBUG mysqldump "$OPTIONS" "$db" | egrep -v -- '^-- Dump completed on' > "$DIR"/DB_"$db".sql.new
${DEBUG} mysqldump "${OPTIONS}" "${db}" | grep -Ev -- '^-- Dump completed on' > "${DIR}/DB_${db}.sql.new"
fi

# We're comparing checksum rather than the whole dump, so that we
# can compress them afterwards and still be able to compare tomorrow's dump.
# - If a checksum file is present, create a new one and compare them
# - If no checksum file is present, create one
if [ -f "$DIR"/DB_"$db".sql.$HASH ]; then
if [ -n "$DEBUG" ]; then
$DEBUG openssl $HASH "$DIR"/DB_"$db".sql.new sed 's/\.new$//' "$DIR"/DB_"$db".sql.new.$HASH
if [ -f "${DIR}/DB_${db}.sql.${HASH}" ]; then
if [ -n "${DEBUG}" ]; then
${DEBUG} openssl ${HASH} "${DIR}/DB_${db}.sql.new" sed 's/\.new$//' "${DIR}/DB_${db}.sql.new.${HASH}"
else
$DEBUG openssl $HASH "$DIR"/DB_"$db".sql.new | sed 's/\.new$//' > "$DIR"/DB_"$db".sql.new.$HASH
${DEBUG} openssl ${HASH} "${DIR}/DB_${db}.sql.new" | sed 's/\.new$//' > "${DIR}/DB_${db}.sql.new.${HASH}"
fi
H_OLD=$(awk '{print $NF}' "$DIR"/DB_"$db".sql.$HASH 2>/dev/null)
H_NEW=$(awk '{print $NF}' "$DIR"/DB_"$db".sql.new.$HASH 2>/dev/null)
H_OLD=$(awk '{print $NF}' "${DIR}/DB_${db}.sql.${HASH}" 2>/dev/null)
H_NEW=$(awk '{print $NF}' "${DIR}/DB_${db}.sql.new.${HASH}" 2>/dev/null)

# - If they are equal, delete our new one, otherwise update the old one
if [ "$H_OLD" = "$H_NEW" ]; then
echo "database $db has not changed, nothing to do."
$DEBUG rm "$DIR"/DB_"$db".sql.new "$DIR"/DB_"$db".sql.new.$HASH
echo "database ${db} has not changed, nothing to do."
${DEBUG} rm "${DIR}/DB_${db}.sql.new" "${DIR}/DB_${db}.sql.new.${HASH}"
else
echo "database $db changed!"
$DEBUG mv "$DIR"/DB_"$db".sql.new.$HASH "$DIR"/DB_"$db".sql.$HASH
$DEBUG mv "$DIR"/DB_"$db".sql.new "$DIR"/DB_"$db".sql
$DEBUG pbzip2 -f "$DIR"/DB_"$db".sql
echo "database ${db} changed!"
${DEBUG} mv "${DIR}/DB_${db}.sql.new.${HASH}" "${DIR}/DB_${db}.sql.${HASH}"
${DEBUG} mv "${DIR}/DB_${db}.sql.new" "${DIR}/DB_${db}.sql"
${DEBUG} pbzip2 -f "${DIR}/DB_${db}.sql"
fi
else
# - We have nothing to compare
echo "database $db must be new?"
$DEBUG mv "$DIR"/DB_"$db".sql.new "$DIR"/DB_"$db".sql
if [ -n "$DEBUG" ]; then
$DEBUG openssl $HASH "$DIR"/DB_"$db".sql "$DIR"/DB_"$db".sql.$HASH
echo "database ${db} must be new?"
${DEBUG} mv "${DIR}/DB_${db}.sql.new" "${DIR}/DB_${db}.sql"
if [ -n "${DEBUG}" ]; then
${DEBUG} openssl ${HASH} "${DIR}/DB_${db}.sql" "${DIR}/DB_${db}.sql.${HASH}"
else
$DEBUG openssl $HASH "$DIR"/DB_"$db".sql > "$DIR"/DB_"$db".sql.$HASH
${DEBUG} openssl ${HASH} "${DIR}/DB_${db}.sql" > "${DIR}/DB_${db}.sql.${HASH}"
fi
$DEBUG pbzip2 -f "$DIR"/DB_"$db".sql
${DEBUG} pbzip2 -f "${DIR}/DB_${db}.sql"
fi
$DEBUG
${DEBUG}
done
END=$($DATE +%s)
echo "$0 finished after $(echo "$END" - "$BEGIN" | bc) seconds."
END=$(${DATE} +%s)
echo "$0 finished after $(echo "${END}" - "${BEGIN}" | bc) seconds."
echo

0 comments on commit 37f98cd

Please sign in to comment.