数据库备份脚本

数据库备份脚本

Scroll Down
#!/bin/bash
PATH=/var/lib/mysql/bin:$PATH
DBPATH=/server/backup
MYUSER=root
MYPASS=root
SOCKET=/var/lib/mysql/mysql.sock
MYCMD="mysql -u$MYUSER -p$MYPASS -S $SOCKET"
MYDUMP="mysqldump -u$MYUSER -p$MYPASS -S $SOCKET"
  [ ! -d "$DBPATH" ] && mkdir $DBPATH
  for dbname in $MYCMD -e "show databases;"|sed '1,2d'|egrep -v "mysql|_schema|data"
  do 
       mkdir $DBPATH/${dbname}$(date +%Y-%m-%d) -p
  for table in `$MYCMD -e "show tables from $dbname;"|sed '1d'|egrep -v "trends|trends_uint|history|history*"`
  do
  $MYDUMP $dbname $table |gzip >$DBPATH/${dbname}$(date +%Y-%m-%d)/${dbname}${table}.sql.gz
  done 
  find $DBPATH -mtime +7 -type d|grep zabbix|xargs rm -rf
done