mysql数据库备份实现

  1. 模拟大数据库,编写存储过程来插入数据
DELIMITER $$

DROP PROCEDURE IF EXISTS `dbname`.`test`$$
CREATE DEFINER=`root`@`localhost` PROCEDURE  `dbname`.`test`(IN t int)
BEGIN
 declare i int;
 select t;
 set i=0;
 while i< t do
  insert into users values(null, "sss","test", now());
    set i=i+1;
 end while;
END $$

DELIMITER ;

查看数据库表大小,单位是B

select DATA_LENGTH+INDEX_LENGTH as length from information_schema.tables where TABLE_NAME="users" and TABLE_SCHEMA="dbname";

备份指定数据库表

mysqldump -uuser -ppassword -t dbname tablename | gzip > tablename.sql.gz 仅备份数据
mysqldump -uuser -ppassword -d dbname tablename > tablename.sql 仅备份结构
mysqldump --opt -d dbname -uusername -ppassword > dbname.sql 备份整个数据库的结构

按日期备份数据库并清理7天前的备份文件

**此段参考文章:http://blog.sina.com.cn/s/blog_621f9b110101pfp1.html**

backupdir=/bak/mysqlbak
time=` date +%Y%m%d%H `
mysql_bin_dir/mysqldump -u user -ppassword dataname1 | gzip > $backupdir/name1$time.sql.gz
mysql_bin_dir/mysqldump -u user -ppassword dataname2 | gzip > $backupdir/name2$time.sql.gz
#
find $backupdir -name "name_*.sql.gz" -type f -mtime +7 -exec rm {} ; > /dev/null 2>&1

 

posted on 2016-05-11 15:46  蕙芷兰馨  阅读(233)  评论(0编辑  收藏  举报