Last active
February 8, 2016 00:23
-
-
Save ilyaevseev/33c01fdb697e57fe7ce8 to your computer and use it in GitHub Desktop.
/etc/cron.daily/Dump-All-Databases for Debian/Ubuntu/MySQL/Postgres.
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/sh | |
TSTAMP="$(LANG=C date +%Y.%m.%d_%H%M%S)" | |
DIR="/var/lib/mysqldump" | |
LOG="$DIR/log-$TSTAMP.log" | |
DUMP="$DIR/dump_all_databases.sql" | |
Fail() { logger "$0: $@"; echo "$@" | mail -s "mysqldump failed" admins; exit 1; } | |
mkdir -p "$DIR" || Fail "Mkdir $DIR failed." | |
mysqldump --defaults-file="/etc/mysql/debian.cnf" -A --opt --skip-lock-tables --skip-extended-insert --events \ | |
> "$DUMP" 2>"$LOG" | |
find "$DIR/" -type "f" -name "*.log" -mtime "+7" -delete | |
test -s "$DUMP" || Fail "Dump empty, see $LOG for details." | |
test -s "$LOG" && Fail "Log not empty, see $LOG for details." |
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/sh | |
TSTAMP="$(date +%Y-%m-%d_%H%M)" | |
DIR="/var/lib/pgsql-dump" | |
DUMP="$DIR/$TSTAMP.psql" | |
LOG="$DIR/$TSTAMP.log" | |
Fail() { echo "$@" | mail -s 'Pgsql-dumpall failed' admins; exit 1; } | |
mkdir -p "$DIR" || Fail "Cannot create $DIR" | |
sudo -u postgres pg_dumpall > "$DUMP" 2>"$LOG" || Fail "pg_dumpall exited with non-zero, check $LOG" | |
test -s "$LOG" && Fail "$LOG is not empty after pg_dumpall." | |
test -s "$DUMP" || Fail "$DUMP is empty." | |
bzip2 -9 "$DUMP" >"$LOG" 2>&1 || Fail "bzip2 exited with non-zero, check $LOG" | |
test -s "$LOG" && Fail "$LOG is not empty after bzip2." | |
find "$DIR/" -type f -mtime +2 -not -name '*.log' -delete | |
find "$DIR/" -type f -mtime +9 -name '*.log' -delete |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment