Created
August 21, 2015 09:19
-
-
Save Godefroy/0f1d18d0a00179dadcaa to your computer and use it in GitHub Desktop.
Instant backup of a MySQL database with LVM snapshot
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/bash | |
## Instant backup of a MySQL database with LVM snapshot | |
## Author: Godefroy de Compreignac (@Godefroy) | |
## License: Beerware | |
# Instructions: | |
# - MySQL Data on DB server must be an logical LVM partition | |
# - Backup server must have ssh root access to DB server | |
# - Master status is saved at the exact moment of snapshot to allow restoration of master-slave replication | |
BACKUP_DIR=/home/backup | |
BACKUP_MYSQL_DIR=$BACKUP_DIR/mysql | |
MYSQL_SERVER=db.example.com | |
MYSQL_PASSWORD=trololo | |
MYSQL_DATA_PART=/dev/vhd1/mysql-data | |
# Create backup dir | |
mkdir -p $BACKUP_MYSQL_DIR/data | |
# Lock DB, Create Snapshot, Unlock DB | |
ssh $MYSQL_SERVER "mysql -u root -p$MYSQL_PASSWORD -Bse \" | |
FLUSH TABLES WITH READ LOCK; | |
SHOW MASTER STATUS\G; | |
system lvcreate -L 5G -s -n backup $MYSQL_DATA_PART; | |
UNLOCK TABLES; | |
\"" > $BACKUP_MYSQL_DIR/master_status | |
# Mount snapshot | |
ssh $MYSQL_SERVER "mkdir -p /backup && mount -o nouuid $MYSQL_DATA_PART /backup" | |
# Backup Snapshot | |
rsync -a --partial --delete --exclude=/*.info $MYSQL_SERVER:/backup/ $BACKUP_MYSQL_DIR/data/ | |
# Remove Snapshot | |
ssh $MYSQL_SERVER "umount /backup && rm -r /backup && dmsetup remove $MYSQL_DATA_PART && lvremove -f $MYSQL_DATA_PART" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
🍺
It seems to me that there is a problem with line 30,
Correct:
/dev/vhd1/backup
Wrong:
/dev/vhd1/mysql-data