定时备份服务器数据库(借助windows任务计划以及mysqldump)

最近社区多了,考虑到数据的安全性,要每天备份一次数据库,以防万一;

linux目前还不是很了解,先用windows的计划任务吧;

大体思路就是 借用windows的计划任务来执行备份远程数据库到本地;

主要是搬来的哈:http://www.php-note.com/article/detail/388

首先,php程序:借用mysqldump来导出数据库,然后php操作存储以及删除旧的数据;

配置文件:db.config.php(我是备份的五个数据库,增加减少就修改$dbs的数组即可)

<?php
/**
 * 数据库配置文件
 */
$dbs = array(
    array(
        'host' => '127.0.1.1',
        'port' => 3306,
        'user' => 'root',
        'pass' => 'root',
        'name' => 'qixiang',
        'charset' => 'utf8'
    ),
    array(
        'host' => '127.0.1.1',
        'port' => 3306,
        'user' => 'root',
        'pass' => 'root',
        'name' => 'liushen',
        'charset' => 'utf8'
    ),
    array(
        'host' => '127.0.1.1',
        'port' => 3306,
        'user' => 'root',
        'pass' => 'root',
        'name' => 'newcommunity',
        'charset' => 'utf8'
    ),
    array(
        'host' => '127.0.1.1',
        'port' => 3306,
        'user' => 'root',
        'pass' => 'root',
        'name' => 'userclub',
        'charset' => 'utf8'
    ),
    array(
        'host' => '127.0.1.1',
        'port' => 3306,
        'user' => 'root',
        'pass' => 'root',
        'name' => 'ckd',
        'charset' => 'utf8'
    ),
);

$backupPath = 'D:/hcr/' . date('Ym') . '/' . date('d');
$oldPath = 'D:/hcr/' . (intval(date('Ym')) - 1);

// 注意:如果路径中有空格,需要用 双引号 引起来
$cmd = '"D:/Program Files (x86)/wamp/bin/mysql/mysql5.6.17/bin/mysqldump"';

// 全部导出
$cmd .= " -h%s -P%s -u%s -p%s %s --default_character-set=%s > %s";

// 只导出 表结构
//$cmd .= " -h%s -P%s -u%s -p%s %s --no-data --default_character-set=%s > %s";

// 只导出 数据
//$cmd .= " -h%s -P%s -u%s -p%s %s --no-create-info --default_character-set=%s > %s";

程序文件:backup.php

<?php
/**
 * 备份数据库
 */

header("Content-type: text/html; charset=utf-8");
date_default_timezone_set('PRC');
set_time_limit(0);
define('ROOT_PATH', dirname(__FILE__));


if (!@include ROOT_PATH . '/db.config.php')
{
    echo 'config error';
    write('config error');
    exit(1);
}

// 新建备份目录
new_mkdir($backupPath);
// 删除一个月前的数据
new_rmdir($oldPath);

// 批量备份数据库
foreach ($dbs as $db)
{
    $cmd2 = '';
    if (isset($db['name']))
    {
        $fileName = $backupPath . '/' . $db['name'] . '_' . date('Ymd_His') . '.sql';
        $cmd2 = sprintf($cmd, $db['host'], $db['port'], $db['user'], $db['pass'], $db['name'], $db['charset'], $fileName);
        write($cmd2);
        try
        {
            exec($cmd2);
        }
        catch (Exception $e)
        {
            // 写错误日志(省略)
            echo $e->getMessage();
        }
    }
}


/**
 * 创建文件夹
 *
 * @param string $path 文件夹路径
 * @param int $mode 访问权限
 * @param boolean $recursive 是否递归创建
 */
function new_mkdir($path = '', $mode = 0777, $recursive = true)
{
    clearstatcache();
    if (!is_dir($path))
    {
        mkdir($path, $mode, $recursive);
        chmod($path, $mode);
        $ret = @touch($path . '/index.html');
        @chmod($path . '/index.html', 0777);
        return $ret;
    }

    return true;
}

/**
 * 清空/删除 文件夹
 *
 * @param string $dirname 文件夹路径
 * @param bool $self 是否删除当前文件夹
 * @return bool
 */
function new_rmdir($dirname = '', $self = true)
{
    if (!file_exists($dirname))
    {
        return false;
    }
    if (is_file($dirname) || is_link($dirname))
    {
        return unlink($dirname);
    }
    $dir = dir($dirname);
    if ($dir)
    {
        while (false !== $entry = $dir->read())
        {
            if ($entry == '.' || $entry == '..')
            {
                continue;
            }
            new_rmdir($dirname . '/' . $entry);
        }
    }
    $dir->close();
    $self && rmdir($dirname);
}

/**
 * 写文件
 *
 * @param string $filename 文件名
 * @param string $text 要写入的文本字符串
 * @param string $openmod 文本写入模式('w':覆盖重写,'a':文本追加)
 * @return boolean
 */
function write_file($filename = '', $text = '', $openmod = 'w')
{
    if (@$fp = fopen($filename, $openmod))
    {
        flock($fp, 2);
        fwrite($fp, $text);
        fclose($fp);
        return true;
    }
    else
    {
        return false;
    }
}

/**
 * 写对象(包括 数字、字符串、数组)
 *
 * @param string $text 要写入的文本字符串
 * @param string $type 文本写入类型('w':覆盖重写,'a':文本追加)
 */
function write($text = '', $type = 'a')
{
    if (!is_dir('d:/hcr'))
    {
        return false;
    }
    $filename = 'd:/hcr/write.txt';

    $text = "\r\n++++++++++++++++++++++++++++++++++++++++++\r\n"
        . date('Y-m-d H:i:s') . "\r\n"
        . print_r($text, true);
    write_file($filename, $text, $type);
}

 

 

具体可看:http://www.php-note.com/article/detail/85

测试步骤如下:

1.所有程序>附件>系统工具>任务计划程序>右上角的‘创建任务’>任务名字

2.创建触发器:

3.新建个操作:

完成计划任务(我设置的是每天中午备份一次);

 

最终的效果如下:

posted @ 2016-03-11 17:27  pthlp  阅读(356)  评论(0编辑  收藏  举报