首页 > 数据库技术 > 详细

MySQL逻辑备份mysqldump

时间:2015-10-28 12:24:21      阅读:290      评论:0      收藏:0      [点我收藏+]

一个用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

 

MySQL逻辑备份mysqldump

原文:http://www.cnblogs.com/chgxtony/p/4916621.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!