add old backup expiration. improve formatting
parent
2b56a001c9
commit
a7c4c23235
|
@ -15,4 +15,3 @@ save nightly mysql database dumps
|
|||
|
||||
## To Do
|
||||
|
||||
1. Expire old logs
|
||||
|
|
49
mysqlbackup
49
mysqlbackup
|
@ -3,41 +3,60 @@
|
|||
# This script will backup all mysql databases into
|
||||
# compressed file named after date, ie: /var/backup/mysql/2016-07-13.tar.gz
|
||||
|
||||
# Setup variables used later
|
||||
|
||||
# Create date suffix with "F"ull date format
|
||||
suffix=$(date +%F)
|
||||
SUFFIX=$(date +%F)
|
||||
|
||||
# Retrieve all database names except information schemas.
|
||||
dbs=$(/usr/local/bin/mysql --defaults-extra-file=/root/.my.cnf --batch --skip-column-names -e "SHOW DATABASES;" | grep -E -v "(information|performance)_schema")
|
||||
DBS=$(/usr/local/bin/mysql \
|
||||
--defaults-extra-file=/root/.my.cnf \
|
||||
--batch \
|
||||
--skip-column-names \
|
||||
-e "SHOW DATABASES;" \
|
||||
| grep -E -v "(information|performance)_schema")
|
||||
|
||||
# Create temporary directory with "-d" option
|
||||
tmp=$(mktemp -d)
|
||||
TMP_DIR=$(mktemp -d)
|
||||
|
||||
# Set output dir here. /var/backups/ is used by system,
|
||||
# so intentionally used /var/backup/ for user backups.
|
||||
outDir="/var/backup/mysql"
|
||||
# Create output file name
|
||||
out="$outDir/$suffix.tar.gz"
|
||||
OUT_DIR="/var/backup/mysql"
|
||||
|
||||
# Create output file name
|
||||
OUT_FILE="$OUT_DIR/$SUFFIX.tar.gz"
|
||||
|
||||
# Number of days to save backups
|
||||
SAVE_DAYS=180
|
||||
|
||||
# Actual script
|
||||
|
||||
# Check if output directory exists
|
||||
if [ ! -d "$outDir" ];then
|
||||
if [ ! -d "$OUT_DIR" ];then
|
||||
# Create directory with parent ("-p" option) directories
|
||||
mkdir -p "$outDir"
|
||||
mkdir -p "$OUT_DIR"
|
||||
fi
|
||||
|
||||
# Loop through all databases
|
||||
for db in $dbs; do
|
||||
for DB in $DBS; do
|
||||
# Dump database to temporary directory with file name same as database name + sql suffix
|
||||
/usr/local/bin/mysqldump --defaults-extra-file=/root/.my.cnf --events --triggers --routines --set-gtid-purged=OFF --databases "$db" > "$tmp/$db.sql"
|
||||
/usr/local/bin/mysqldump \
|
||||
--defaults-extra-file=/root/.my.cnf \
|
||||
--events \
|
||||
--triggers \
|
||||
--routines \
|
||||
--set-gtid-purged=OFF \
|
||||
--databases "$DB" \
|
||||
> "$TMP_DIR/$DB.sql"
|
||||
done
|
||||
|
||||
# Go to tmp dir
|
||||
cd $tmp
|
||||
cd $TMP_DIR
|
||||
|
||||
# Compress all dumps with gzip, discard any output to /dev/null
|
||||
tar -zcf "$out" * > "/dev/null"
|
||||
tar -zcf "$OUT_FILE" * > "/dev/null"
|
||||
|
||||
# Cleanup
|
||||
cd "/tmp/"
|
||||
rm -rf "$tmp"
|
||||
rm -rf "$TMP_DIR"
|
||||
|
||||
# Expire old backups
|
||||
find ${OUT_DIR} -mtime +${SAVE_DAYS} -type f -delete
|
||||
|
|
Loading…
Reference in New Issue