热门标签 | HotTags
当前位置:  开发笔记 > 后端 > 正文

Shell简单全备mysql脚本案例

Shell简单全备mysql脚本案例[root@cactimysql]#catbackup.sh#!/bin/shwww.2cto.com#createdbytaiyang#2012-9-16#function:backupmysqldatabase#setlocalvariablesDBnamebackupBacku

Shell简单全备mysql脚本案例
 
[root@cacti mysql]# cat backup.sh 
#!/bin/sh
  www.2cto.com  
#created by taiyang
#2012-9-16
#function:backup mysql database
 
#set local variables
DBname=backup
BackupPath=/mysqlbak/mysqlbak/
DumpFile=${BackupPath}${DBname}`date +%y%m%d_%H%M%S`.bak
 
v_username='root'
if [ $v_username != 'root' ];then
echo "ERROR:the script must be run as mysql"
exit 1
fi
  www.2cto.com  
echo "backup database path:$DumpFile"
echo "------------------------------"
 
date
echo "****************************"
echo "backup database begin"
echo "****************************"
 
mysqldump -uroot -proot --opt $DBname > $DumpFile
 
echo "****************************" 
echo "backup database end"
echo "****************************"
date
 
#delete backups before 5 days
find $BackupPath -ctime +5 -name '*.bak' -exec rm {} \;
[root@cacti mysql]# 
  www.2cto.com  
从备份脚本当中进行还原数据库的信息;
 
[root@cacti mysqlbak]# ls
backup120928_114307.bak  backup120928_114556.bak  cacti120928_105558.bak
backup120928_114309.bak  cacti120928_105044.bak   cacti120928_110443.bak
backup120928_114310.bak  cacti120928_105155.bak   cacti120928_110448.bak
backup120928_114311.bak  cacti120928_105457.bak   cacti120928_110449.bak
[root@cacti mysqlbak]# pwd
 
/mysqlbak/mysqlbak
[root@cacti mysqlbak]#
[root@cacti ~]# mysql -hlocalhost -uroot -proot backup
p120928_114556.bak 
 
[root@cacti ~]# mysql -uroot -proot
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 860
Server version: 5.0.22-log
 
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
  www.2cto.com  
mysql> use backup
Database changed
mysql> show tables;
+------------------+
| Tables_in_backup |
+------------------+
| b                | 
| t                | 
+------------------+
2 rows in set (0.00 sec)
 
mysql> exit
Bye
 

推荐阅读
author-avatar
超级活死人
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有