Skip to content

Instantly share code, notes, and snippets.

@lucien144
Last active April 6, 2016 08:32
Show Gist options
  • Select an option

  • Save lucien144/6c00a7327ff45966eae3 to your computer and use it in GitHub Desktop.

Select an option

Save lucien144/6c00a7327ff45966eae3 to your computer and use it in GitHub Desktop.
Server backup - MySQL & Vhosts - to S3 usign s3cmd
#! /bin/bash
# MySQL requirements
# CREATE USER 'backup'@'localhost' IDENTIFIED BY 'secret';
# GRANT SELECT, SHOW VIEW, RELOAD, REPLICATION CLIENT, EVENT, TRIGGER ON *.* TO 'backup'@'localhost';
# GRANT LOCK TABLES ON *.* TO 'backup'@'localhost';
TIMESTAMP=$(date +"%A")
DATE=$(date +"%F")
BACKUP_DIR="./mysql-backup"
MYSQL_USER="backup"
MYSQL=/usr/bin/mysql
MYSQL_PASSWORD="*****"
MYSQLDUMP=/usr/bin/mysqldump
mkdir -p "$BACKUP_DIR/$TIMESTAMP"
databases=`$MYSQL --user=$MYSQL_USER -p$MYSQL_PASSWORD -e "SHOW DATABASES;" | grep -Ev "(Database|information_schema|performance_schema)"`
for db in $databases; do
$MYSQLDUMP -v --force --opt --user=$MYSQL_USER -p$MYSQL_PASSWORD --databases $db | gzip > "$BACKUP_DIR/$TIMESTAMP/$db.gz"
done
if [ $(date +"%d") = "01" ]; then
mkdir -p "$BACKUP_DIR/$DATE"
cp -R $BACKUP_DIR/$TIMESTAMP/* "$BACKUP_DIR/$DATE/"
fi
s3cmd sync --recursive --delete-removed ./mysql-backup s3://server-backup-syd/
#!/bin/bash
s3cmd sync --recursive --exclude-from=/root/backup/vhosts-backup.exclude /var/www/vhosts/ s3://server-backup-syd/vhosts-backup/
@lucien144
Copy link
Author

Todo: consider to use automysqlbackup tool

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment