一个用mysqldump做逻辑备份的小脚本
用于小数据量的备份,全库,按日期,按库,按表,进行分类
# date -s "2015/01/01" && bash mybackup.sh
#!/bin/bash # 使用mysqldump进行逻辑备份 # 目录结构/backup/日期 # 每隔7天删除旧的备份目录 db_backup_root="/backup" today=`date +%Y%m%d` old_backup_dir=`date -d "-7 day" +%Y%m%d` db_user="root" db_passwd="geekoo" sock=`mysql -u$db_user -p$db_passwd -e "show variables like '%sock%'" | grep -v Variable_name | awk {'print $2'}` mysql_client=`which mysql` mysqldump_client=`which mysqldump` xmysql="$mysql_client -S $sock -u$db_user -p$db_passwd" xmysqldump="$mysqldump_client -S $sock -u$db_user -p$db_passwd" if [ -d $old_backup_dir ]; then echo "delete old backup $old_backup_dir" rm -rf $old_backup_dir fi if [ ! -d $today ]; then mkdir -p $db_backup_root/$today fi $xmysql -e "show databases;" | grep -vE "(Database|_schema)" > $db_backup_root/$today/db_list while read dbname do echo $dbname if [ ! -d $db_backup_root/$today/$dbname ]; then mkdir -p $db_backup_root/$today/$dbname fi $xmysql -e "use $dbname; show tables;" | grep -v Tables_in > $db_backup_root/$today/$dbname/table_list while read tablename do echo $tablename $xmysqldump $dbname $tablename -R --events --triggers > $db_backup_root/$today/$dbname/$tablename.sql done < $db_backup_root/$today/$dbname/table_list done < $db_backup_root/$today/db_list