#!/bin/sh
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
MYSQL_BACKUP_PATH=/backup/mysql
BACKUP_DATE=`date "+%Y-%m-%d"`
set mask 077
cd $MYSQL_BACKUP_PATH/$PORT
find . -mtime +21 -delete
# 3306
PORT=3306
cd $MYSQL_BACKUP_PATH/$PORT
mkdir -p $BACKUP_DATE
cd $BACKUP_DATE
for MYSQL_DB in $(mysql -u backup -pSECRET -S /tmp/mysql.sock$PORT -Bse 'show databases' | grep -v '_schema'); do \
mkdir -p $MYSQL_DB
echo '===>' $MYSQL_DB;
mysqldump -u backup -pSECRET -S /tmp/mysql.sock$PORT $MYSQL_DB --no-data --triggers --routines --events > $MYSQL_DB/_structur
e.sql
for MYSQL_TABLE in $(mysql -u backup -pSECRET -S /tmp/mysql.sock$PORT -Bse 'show tables' $MYSQL_DB); do \
echo $MYSQL_TABLE;
mysqldump -u backup -pSECRET -S /tmp/mysql.sock$PORT $MYSQL_DB $MYSQL_TABLE --opt --lock-all-tables --default-character-set=cp1251 > $MYSQL_DB/$MYSQL_TABLE.sql
done
echo 'Archiving '$MYSQL_DB;
tar czf $MYSQL_DB.tgz $MYSQL_DB
rm -rf $MYSQL_DB
done
rsync -avR --password-file=/etc/rsync.key /backup/mysql rsync@where_you_want::backup_folder