scripts/shell/mydbdump.sh
2024-03-20 11:28:46 -05:00

42 lines
903 B
Bash
Executable file

#!/usr/bin/env bash
#
# SIMPLE MySQL backup: dumps a complete database, keeping 7 days worth
# of backups in rotation. run nightly.
#
# Ensure you have a privileged login cookied for the user who will
# run this script. E.g.:
#
# /root/.my.cnf:
# [client]
# user=root
# password=p@ssw0rd
#
# SPDX-License-Identifier: MIT
MYD_BDIR=/opt/backups/database
MYD_DUMP=/usr/bin/mysqldump
MYD_BZ2=/usr/bin/bzip2
# get the day of week, 1 = Monday
DOW=$(date "+%u")
# we overwrite the old weekday's backup
MYD_BLOG="$MYD_BDIR/db-backup.log.${DOW}"
MYD_BFILE="$MYD_BDIR/db-backup.sql.${DOW}"
# the real export procedure
$MYD_DUMP -A --opt 1>"$MYD_BFILE" 2>"$MYD_BLOG"
# erase old bz2 files
if [ -f "${MYD_BFILE}.bz2" ]; then
rm -f "${MYD_BFILE}.bz2"
fi
if [ -f "${MYD_BLOG}.bz2" ]; then
rm -f "${MYD_BLOG}.bz2"
fi
# compress our files to save some space
$MYD_BZ2 "$MYD_BFILE"
$MYD_BZ2 "$MYD_BLOG"
exit 0