Skip to content

Instantly share code, notes, and snippets.

@nacholibre
Last active June 27, 2016 09:05
Show Gist options
  • Save nacholibre/cf42fb70b9b43e9995b577e4b3dc1fa0 to your computer and use it in GitHub Desktop.
Save nacholibre/cf42fb70b9b43e9995b577e4b3dc1fa0 to your computer and use it in GitHub Desktop.
Shell script for server backup (sites and mysql databases) with i/o throttling.
#!/bin/bash
CURRENT_DATE=$(date +'%Y-%m-%d')
MYSQL_USER="mysql_user"
MYSQL=/usr/bin/mysql
MYSQL_PASSWORD="mysql_user_pass"
MYSQLDUMP=/usr/bin/mysqldump
BACKUP_DIR=/DATA/backup/$CURRENT_DATE
SITES_DIR=/www
BACKUP_SPEED="1m" #per sec, for archiving the SITES_DIR, this is done by `pv`
mkdir -p $BACKUP_DIR/mysql
mkdir -p $BACKUP_DIR/www
for directory in $SITES_DIR/*; do
[ -d "${directory}" ] || continue # if not a directory, skip
dirname="$(basename "${directory}")"
#h - follow symlinks
tar -ch $SITES_DIR/$dirname | pv -L $BACKUP_SPEED > $BACKUP_DIR/www/$dirname.tar
done
databases=`$MYSQL --user=$MYSQL_USER -p$MYSQL_PASSWORD -e "SHOW DATABASES;" | grep -Ev "(Database|information_schema|performance_schema)"`
for db in $databases; do
$MYSQLDUMP --force --opt --user=$MYSQL_USER -p$MYSQL_PASSWORD --databases $db | gzip > "$BACKUP_DIR/mysql/$db.gz"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment