Created
December 20, 2021 15:24
-
-
Save douggreen/2f0017a0ac1f90e2bca3d2554f88249d to your computer and use it in GitHub Desktop.
Snapshot a mariadb database for quick restore
This file contains 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 | |
if [ $# -ne 2 ]; then | |
echo "usage: $0 [database] [snapshot-name]" | |
exit 1 | |
fi | |
DIR=~/databases/backup | |
DB=$1 | |
SNAPSHOT=$2 | |
DEST="$DIR/$DB/$SNAPSHOT" | |
rm -fr "$DEST" | |
mkdir -p "$DEST" | |
/opt/homebrew/bin/mariabackup --rsync --backup --databases=$1 --target-dir="$DEST" | |
PRE="$DIR/$DB/$SNAPSHOT-pre.sql" | |
POST="$DIR/$DB/$SNAPSHOT-post.sql" | |
> $PRE | |
> $POST | |
echo "/* DROP, CREATE, and USE added by $0 */" >> $PRE | |
echo "DROP DATABASE $DB; CREATE DATABASE $DB; USE $DB;" >> $PRE | |
echo >> $PRE | |
mysqldump -d $DB >> $PRE | |
echo >> $PRE | |
echo "/* Discard tablespaces added by $0 */" >> $PRE | |
echo "/* Import tablespaces added by $0 */" >> $POST | |
echo "USE $DB;" >> $POST | |
for table in `mysql -A -E -e "select table_name FROM information_schema.tables where table_schema = '$DB';" | grep table_name | sed -e 's/table_name://g'`; do | |
echo "ALTER TABLE $table DISCARD TABLESPACE;" >> $PRE | |
echo "ALTER TABLE $table IMPORT TABLESPACE;" >> $POST | |
done | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment