MySQL备份脚本,mysql脚本
mysqlbackup.php:
<?<span>php </span><span>//</span><span>备份mysql</span> <span>set_time_limit</span>(0<span>); date_default_timezone_set(</span>'PRC'<span>); </span><span>//</span><span>配置</span> <span>$configs</span> = <span>array</span><span>( </span>'host1'=><span>array</span><span>( </span>'localhost', 'root', 'root', <span>array</span>(), <span>//</span><span>为空备份全部数据库,否则备份这些数据库</span> 'D:/xampp/mysql/bin/mysqldump', <span>//</span><span>备份工具</span> <span>dirname</span>(<span>__FILE__</span>)."/localhost", <span>//</span><span>目录加主机名</span> 5, <span>//</span><span>删除前5天的SQL文件</span> ),<span>); </span><span>foreach</span>(<span>$configs</span> <span>as</span> <span>$config</span><span>) { </span><span>$logsfile</span> = <span>$config</span>[5].'/'.<span>date</span>('ymd').'.log'<span>; logs(</span><span>$logsfile</span>, <span>$config</span>[0]." backup\n"<span>); backup(</span><span>$config</span><span>); } </span><span>function</span> backup(<span>$config</span><span>) { </span><span>list</span>(<span>$host</span>, <span>$username</span>, <span>$password</span>, <span>$databases</span>, <span>$backuptool</span>, <span>$backupdir</span>, <span>$day</span>) = <span>$config</span><span>; </span><span>$command</span> = "<span>$backuptool</span> -u <span>$username</span> -h <span>$host</span> -p<span>$password</span> %s > %s"<span>; </span><span>$logsfile</span> = <span>$backupdir</span>.'/'.<span>date</span>('ymd').'.log'<span>; </span><span>$backfilename</span> = <span>$backupdir</span>.'/'.<span>date</span>('Ymd')."%s.sql"; <span>//</span><span>备份的SQL文件,以数据库命名</span> <span>if</span>(!<span>is_dir</span>(<span>$backupdir</span><span>)) { </span><span>mkdir</span>(<span>$backupdir</span>, 0755 , <span>true</span><span>); } </span><span>//</span><span>删除前十天的备份文件</span> get_dir_files(<span>$backupdir</span>, <span>$returnval</span><span>); </span><span>if</span>(<span>$returnval</span><span>) { </span><span>foreach</span>(<span>$returnval</span> <span>as</span> <span>$v</span><span>) { </span><span>$time</span> = <span>filemtime</span>(<span>$v</span><span>); </span><span>if</span>(<span>$time</span> < <span>strtotime</span>("-<span>$day</span> day") && (<span>pathinfo</span>(<span>$v</span>,PATHINFO_EXTENSION))=='zip'<span>) { </span><span>unlink</span>(<span>$v</span><span>); } } } </span><span>if</span>(!<span>$databases</span><span>) { </span><span>$databases</span> = getdatabases(<span>$host</span>, <span>$username</span>, <span>$password</span><span>); } </span><span>//</span><span>开始备份</span> <span>foreach</span>(<span>$databases</span> <span>as</span> <span>$database</span><span>) { </span><span>$outputfile</span> = <span>sprintf</span>(<span>$backfilename</span>, <span>$database</span><span>); </span><span>$execcommand</span> = <span>sprintf</span>(<span>$command</span>, <span>$database</span>, <span>$outputfile</span><span>); </span><span>try</span><span> { </span><span>if</span>(<span>system</span>(<span>$execcommand</span>) === <span>false</span><span>) { </span><span>throw</span> <span>new</span> <span>Exception</span>('execute backup command error!'<span>); } </span><span>//</span><span>文件过大时会压缩失败(测试的那个SQL文件4.62G,压缩失败,没创建那个压缩文件。测试2.81G可以)</span> <span>if</span>(<span>file_exists</span>(<span>$outputfile</span><span>)) { </span><span>$zip</span> = <span>new</span><span> ZipArchive(); </span><span>$filename</span> = <span>pathinfo</span>(<span>$outputfile</span>,<span>PATHINFO_FILENAME); </span><span>$zipname</span> = <span>$backupdir</span>.'/'.<span>$filename</span>.'.zip'; <span>//</span><span>zip文件的路径</span> <span>if</span>(<span>$zip</span>->open(<span>$zipname</span>, ZIPARCHIVE::OVERWRITE) === <span>true</span><span>) { </span><span>$zip</span>->addFile(<span>$outputfile</span>, <span>$filename</span>.'.'.<span>pathinfo</span>(<span>$outputfile</span>,<span>PATHINFO_EXTENSION)); </span><span>$zip</span>-><span>close(); }</span><span>else</span><span> { </span><span>throw</span> <span>new</span> <span>Exception</span>('ZipArchive open error!'<span>); } } </span><span>if</span>(!<span>file_exists</span>(<span>$zipname</span>) || (<span>filesize</span>(<span>$zipname</span>)==0<span>)) { </span><span>throw</span> <span>new</span> <span>Exception</span>('ZipArchive create error!'<span>); } </span><span>$message</span> = <span>date</span>('Y-m-d H:i:s')."<span>$database</span> backup complete!\r\n"<span>; logs(</span><span>$logsfile</span>, <span>$message</span><span>); }</span><span>catch</span>(<span>Exception</span> <span>$e</span><span>) { </span><span>$message</span> = <span>date</span>('Y-m-d H:i:s').<span>$e</span>->getLine().' '.<span>$e</span>->getMessage()."\r\n"<span>; logs(</span><span>$logsfile</span>, <span>$message</span><span>); } } } </span><span>function</span> getdatabases(<span>$host</span>, <span>$username</span>, <span>$password</span><span>) { </span><span>$databases</span> = <span>array</span><span>(); </span><span>try</span><span> { </span><span>$mysqli</span> = <span>new</span> Mysqli(<span>$host</span>, <span>$username</span>, <span>$password</span><span>); </span><span>$result</span> = <span>$mysqli</span>->query("show databases"<span>); </span><span>if</span>(<span>$result</span><span>) { </span><span>while</span>(<span>$row</span> = <span>$result</span>-><span>fetch_row()) { (</span><span>current</span>(<span>$row</span>)!='information_schema' && <span>current</span>(<span>$row</span>)!='mysql' && <span>current</span>(<span>$row</span>)!='performance_schema') && <span>$databases</span>[] = <span>current</span>(<span>$row</span><span>); } </span><span>$result</span>-><span>free_result(); </span><span>$mysqli</span>-><span>close(); }</span><span>else</span><span> { </span><span>throw</span> <span>new</span> <span>Exception</span>('No databases!'<span>); } }</span><span>catch</span>(<span>Exception</span> <span>$e</span><span>) { </span><span>$message</span> = <span>date</span>('Y-m-d H:i:s').<span>$e</span>->getLine().' '.<span>$e</span>->getMessage()."\r\n"<span>; logs(</span><span>$logsfile</span>, <span>$message</span><span>); } </span><span>return</span> <span>$databases</span><span>; } </span><span>function</span> logs(<span>$file</span>, <span>$contents</span><span>) { </span><span>$dirname</span> = <span>dirname</span>(<span>$file</span><span>); </span><span>try</span><span> { </span><span>if</span>(!<span>is_dir</span>(<span>dirname</span>(<span>$file</span>)) || !<span>is_writeable</span>(<span>dirname</span>(<span>$file</span><span>))) { </span><span>throw</span> <span>new</span> <span>Exception</span>("file is not direcotory or file can't write"<span>); } </span><span>file_put_contents</span>(<span>$file</span>, <span>$contents</span>,<span> FILE_APPEND); }</span><span>catch</span>(<span>Exception</span> <span>$e</span><span>) { </span><span>$message</span> = <span>date</span>('Y-m-d H:i:s').<span>$e</span>->getLine.' '.<span>$e</span>->getMessage()."\r\n"<span>; logs(</span><span>$logfile</span>, <span>$message</span><span>); } } </span><span>//</span><span>获取当前目录下的文件(不包含子文件夹)</span> <span>function</span> get_dir_files(<span>$currPath</span>, &<span>$returnVal</span>=<span>array</span><span>()) { </span><span>if</span>(<span>is_dir</span>(<span>$currPath</span><span>)) { </span><span>$currPath</span> = (<span>substr</span>(<span>$currPath</span>,-1,1)=='/')?<span>substr</span>(<span>$currPath</span>,0,<span>strlen</span>(<span>$currPath</span>)-1):<span>$currPath</span><span>; </span><span>if</span>(<span>$handler</span> = <span>opendir</span>(<span>$currPath</span><span>)) { </span><span>while</span>((<span>$fileName</span> = <span>readdir</span>(<span>$handler</span>)) !== <span>false</span><span>) { </span><span>if</span>(<span>$fileName</span> != '.' && <span>$fileName</span> != '..' && <span>$fileName</span>[0] != '.'<span>) { </span><span>if</span>(<span>is_file</span>(<span>$currPath</span> . '/' . <span>$fileName</span><span>)) { </span><span>$returnVal</span>[] = <span>$currPath</span> . '/' . <span>$fileName</span><span>; } } } </span><span>closedir</span>(<span>$handler</span><span>); } } } </span>?>
mysqlbackup.bat:
D:\xampp\php\php.exe -q D:\wamp\www\php_lib\basic\mysqlbackup.php
pause;
linux系统shell备份MySQL:
#!/bin/<span>sh</span><span> # </span><span>sed</span> -i <span>'</span><span>s/^M//g</span><span>'</span> /home/taskschd/backup.<span>sh</span><span> #注意:</span>^M的输入方式是 Ctrl + v ,然后Ctrl +<span> M dbs</span>=<span>(test) ROOT_DIR</span>=/home/backup/ <span>for</span> dbname <span>in</span><span> ${dbs[@]} </span><span>do</span><span> #备份数据 BACKUP_DIR</span>=$ROOT_DIR$dbname<span>'</span><span>_</span><span>'</span>$(<span>date</span> +%Y%m%<span>d).sql </span>/usr/local/mysql/bin/mysqldump --opt -uroot -pabc $dbname ><span> $BACKUP_DIR #删除三天前数据 delete_file</span>=$dbname<span>'</span><span>_</span><span>'</span>$(<span>date</span> -d <span>"</span><span>-5 day</span><span>"</span> <span>"</span><span>+%Y%m%d</span><span>"</span>)<span>'</span><span>.sql</span><span>'</span> <span>rm</span><span> $ROOT_DIR$delete_file </span><span>done</span>
另为一篇shell备份mysql脚本:http://www.cnblogs.com/luoyunshu/p/3435378.html
方法一、适合所有格式的mysql数据库,通过对数据库导出导进写个脚本定时执行:
1.导出整个数据库 mysqldump -u 用户名 -p 数据库名 > 导出的文件名 mysqldump -u wcnc -p smgp_apps_wcnc > /存放路径/wcnc.sql
2.导出一个表 mysqldump -u 用户名 -p 数据库名 表名> 导出的文件名 mysqldump -u wcnc -p smgp_apps_wcnc users> /存放路径/wcnc_users.sql
3.导出一个数据库结构 mysqldump -u wcnc -p -d --add-drop-table smgp_apps_wcnc >/存放路径/wcnc_db.sql
定义:
-d 没有数据
--add-drop-table 在每个create语句之前增加一个drop table
4.导入数据库 常用source 命令 进入mysql数据库控制台:
如mysql -u root -p mysql>use 数据库
方法二、针对mysql数据表格式为MyISAM的
假如数据文件在/var/lib/mysql
那么直接写个脚本
cp -r /var/lib/mysql /备份到的文件夹路径
隔机备份用rsync增量,或定时完整备份。
可能是你不会用吧
可以将这个脚本放进crontab,每天凌晨执行一次,自动备份
这个脚本每天最多只执行一次,而且只保留最近五天的备份在服务器上。
代码:
#!/bin/bash
#This is a ShellScript For Auto DB Backup
#Powered by aspbiz
#2004-09
#Setting
#设置数据库名,数据库登录名,密码,备份路径,日志路径,数据文件位置,以及备份方式
#默认情况下备份方式是tar,还可以是mysqldump,mysqldotcopy
#默认情况下,用root(空)登录mysql数据库,备份至/root/dbxxxxx.tgz
DBName=mysql
DBUser=root
DBPasswd=
BackupPath=/root/
LogFile=/root/db.log
DBPath=/var/lib/mysql/
#BackupMethod=mysqldump
#BackupMethod=mysqlhotcopy
#BackupMethod=tar
#Setting End
NewFile="$BackupPath"db$(date +%y%m%d).tgz
DumpFile="$BackupPath"db$(date +%y%m%d)
OldFile="$BackupPath"db$(date +%y%m%d --date='5 days ago').tgz
echo "-------------------------------------------" >> $LogFile
echo $(date +"%y-%m-%d %H:%M:%S") >> $LogFile
echo "--------------------------" >> $LogFile
#Delete Old File
if [ -f $OldFile ]
then
rm -f $OldFile >> $LogFile 2>&1
echo "[$OldFile]Delete Old File Success!" >> $LogFile
else
echo "[$OldFile]No Old Backup File!" >> $LogFile
fi
if [ -f $NewFile ]
then
echo "[$NewFile]The Backup File is exists,Can't Backup!" >> $LogFile
else
case $BackupMethod in
mysqldump)
if [ -z $DBPasswd ]
then
mysqldump -u $DBUser --opt $DBName > $DumpFile
else
mysqldump -u $DBUser -p$DBPasswd --opt $DBName > $DumpFile
f......余下全文>>

随着数据量的不断增加,数据库的性能成为了一个越来越重要的问题。数据冷热分离处理是一种有效的解决方案,它可以将热点数据和冷数据进行分离,从而提高系统的性能和效率。本文将介绍如何使用Go语言和MySQL数据库进行数据冷热分离处理。一、什么是数据冷热分离处理数据冷热分离处理是一种将热点数据和冷数据进行分类处理的方式。热点数据是指访问频率高、对性能要求高的数据,冷数

随着数据量的增加,数据库的备份变得越来越重要。而对于MySQL数据库,我们可以借助Go语言实现自动化的增量备份。本篇文章将简单介绍如何使用Go语言进行MySQL数据库的数据增量备份。一、安装Go语言环境首先,我们需要在本地安装Go语言环境。可以前往官网下载相应的安装包并进行安装。二、安装相应的库Go语言提供了许多访问MySQL数据库的第三方库,其中较为常用的

如何使用MySQL数据库进行时间序列分析?时间序列数据是指按照时间顺序排列的数据集合,它具有时间上的连续性和相关性。时间序列分析是一种重要的数据分析方法,可以用于预测未来趋势、发现周期性变化、检测异常值等。在本文中,我们将介绍如何使用MySQL数据库进行时间序列分析,并附上代码示例。创建数据表首先,我们需要创建一个数据表来存储时间序列数据。假设我们要分析的数

随着大量的数据需要存储和处理,MySQL已经成为了应用开发中最常用的关系型数据库之一。而Go语言由于其高效并发处理和简洁的语法,也越来越受到开发者的欢迎。本文就将带领读者通过Go语言实现可靠的MySQL数据库连接,让开发者能够更加高效地查询和存储数据。一、Go语言连接MySQL数据库的几种方式Go语言中连接MySQL数据库通常有3种方式,分别是:1.第三方库

近年来,Go语言越来越受到开发人员的青睐,成为开发高性能Web应用程序的首选语言之一。MySQL也作为一种流行的数据库,使用广泛。在将这两个技术结合起来使用的过程中,缓存处理是非常重要的一环。下面将介绍如何使用Go语言来处理MySQL数据库的缓存。缓存的概念在Web应用程序中,缓存是为了加快数据的访问速度而创建的一种中间层。它主要用于存储经常被请求的数据,以

随着业务的增长和数据库版本升级等因素,数据库迁移变得越来越普遍。在进行数据迁移时,选择合适的工具和语言非常重要。本文将介绍如何使用Go语言进行MySQL数据库的数据迁移。安装MySQL驱动在使用Go语言进行MySQL数据库迁移前,需要首先安装MySQL驱动。在Go语言中,有很多MySQL驱动可供选择。在本文中,我们将选择最常用的官方MySQL驱动包-"dat

MySQL创建标签表实现文章标签功能的实现步骤标签是一种常用的分类方式,它可以帮助我们更好地组织和检索文章。在许多网站和应用程序中,都会有文章标签的功能。本文将介绍如何使用MySQL创建标签表,并实现文章标签的功能。步骤1:创建标签表首先,我们需要创建一个用于存储标签的表。在MySQL中,可以使用以下命令创建标签表:CREATETABLEtags(

随着互联网和大数据时代的到来,数据处理成为了一项必备的技能。MySQL作为目前世界上最流行的关系型数据库管理系统,一直以来在数据处理领域受到了广泛的应用。MySQL具有性能高、易用性好、灵活性强等优点,但数据导入导出过程中可能会存在重复或无效数据,因此本文将介绍如何使用Go语言进行MySQL数据库的数据导入导出过滤的方法。一、环境搭建安装MySQL数据库在开


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

Dreamweaver Mac版
視覺化網頁開發工具

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

記事本++7.3.1
好用且免費的程式碼編輯器