首页 > 代码库 > mysql备份脚本

mysql备份脚本

mysql备份脚本
1. 最简单的
#!/bin/bash
d=`date +%w`
/usr/bin/mysqldump -umylinux -pxxxxx mylinux >/data/mysqlbak/$d.sql

2. 稍微复杂的
#! /bin/bash

STORE_NAME=`date +%m%d`
STORE_NAME2=`date +%w`
STORE_DIR=‘/backup/mysqlbak‘
REMOTE_DIR=‘ypl-web:/backup‘
BACKUP_DIR=‘/data/mysql/‘
exec 1>/var/log/mysqlbak.log 2>&1
echo mysqlbak start `date`

/etc/init.d/mysqld stop
sleep 2
killall -9 mysqld; sleep 2
killall -9 mysqld
/usr/bin/rsync -azu $BACKUP_DIR $STORE_DIR/$STORE_NAME2
/etc/init.d/mysqld start

for db in ypl 1yplcc ypl913 ecshop wordpress
do
/usr/local/mysql/bin/mysqldump -uroot -p‘xxx‘ $db >$STORE_DIR/$db-$STORE_NAME.sql
echo mysqlbak end `date`

cd $STORE_DIR
for db in ypl 1yplcc ypl913 ecshop wordpress
do
gzip -f $db-$STORE_NAME.sql
done
find $STORE_DIR/*.sql.gz -mtime +6 |xargs rm -f

echo remote bak start `date`
/usr/bin/rsync -azu --delete $STORE_DIR $REMOTE_DIR/
echo remote bak end `date`

3. innodb引擎,超大数据库备份
#!/bin/bash
## mysql backup daily 3306 and 3307.
exec 2> /BACKUP/mysqlbackup/bak.err

innb="/usr/bin/innobackupex"
opt="--user=backup \
--password=bakBAK001 \
--defaults-file=/etc/my.cnf "
bakdir="/BACKUP/mysqlbackup/whole"

bak() {
$innb $opt --socket=$1 $bakdir/$2
}

bak /tmp/mysql1.sock 3306 >/tmp/3306.bak
bak /tmp/mysql2.sock 3307 >/tmp/3307.bak

find /BACKUP/mysqlbackup/whole/ -type f -mtime +7 |xargs rm -f

mysql备份脚本