php中实现mysql数据库备份与linux自动定时备份代码
文章介绍了二种数据库备案的代码,一种是我们php写的常用的数据库备份类,另一种是为linux朋友提供的一个自动定时备份mysql数据库的代码,有需要的同学可以参考一下。
把下面php代码保存成backdata.class.php文件
代码如下 | 复制代码 |
/* public function __construct($mysql_link){ ?> |
我们再创建一个新的文件与backdata.class.php保存在同一目录
使用方法:
代码如下 | 复制代码 |
require_once("backdata.class.php"); $link = @mysql_connect("localhost","数据库名","密码") or die ('Could not connect to server.'); mysql_query("use cms",$link); mysql_query("set names utf8",$link); $dbbck=new backupData($link);//实例化它,只要一个链接标识就行了 //备份数据时,如想备份一个数据库中的所有表,你可这样写: $dbbck->backupTables("cms","./",array('*')); //备份数据时,如想备份一个数据库中的仅一个表时,你可这样写: $dbbck->backupTables("cms","./",array('user')); //备份数据时,如想备份一个数据库中的多个表时,你可这样写: $dbbck->backupTables("cms","./",array('user','acl','informatoin')); //注解:$dbbck->backupTables("参1","参2",array());中, |
参1为:数据库名,
参2为:要存放备份数据的位置(即目录地址)
第三个为:你要保存那些表
下面为linux中的自动定时备份的代码
参考了网上的很多教程,外加自己的测试,以下脚本经测试可用。
代码如下 | 复制代码 |
#!/bin/bash #配置参数 #判断备份文件存储目录是否存在,否则创建该目录 #开始备份之前,将备份信息头写入日记文件 #切换至备份目录 #备份失败后向网站管理者发送邮件提醒,需要mailutils或者类似终端下发送邮件工具的支持 |
使用:
将以上代码保存到: /usr/sbin/DataBackup (文件名随意,只要不跟系统原有的命令同名即可;代码可以放到任何地方,放在sbin目录下只是为了方便执行,sbin目录下的文件/目录可在终端直接调 用,类似于windows下PATH变量指定的目录)
为脚本添加可执行权限: sudo chmod +x /usr/sbin/DataBackup
执行脚本: sudo DataBackup
如果需要定时执行备份命令的,只需将下面这段代码放到crontab 文件(sudo vim /etc/crontab)中去就可以了:
01 3 * * * root /usr/sbin/DataBackup #它代表着将于每天3点执行DataBackup脚本
小注意一下下:
linux 下的shell脚本定义变量的格式为: key=value ,注意他们两者之间的” = “前后不能出现空格,否则系统无法确认该变量。
用红色标注的那行,第一个类似单引号的字符”`”其实不是单引号,它的输入键在键盘ESC键下方。
这个脚本只适合用于一些小站点的备份,因为它是对数据库进行全部备份而不是增量备份,不适合大容量的数据库备份。
相对于两种方法定时备份算是最好的方法了,这样可以节省时间让机器在最少人访问时自动备案,而php备份类需要人为操作,当然在windows下也可以利用计划任务来实现了。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Mac version
God-level code editing software (SublimeText3)

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool
