数据库的备份与还原

#include "buckrecodb.h"
#include <QFile>
#include <QFileDialog>
#include <QMessageBox>
#include <QDebug>
#include <QDir>
#include <QDateTime>
BackupAndRecover::BackupAndRecover()
{

}
//备份数据库
bool BackupAndRecover::backupDB(const QString& databaseName,QString buckupPath)
{
    bool result = false;

    QSqlDatabase database = getCurrentDB(databaseName);
    QSqlQuery query(database);

    //开启cmdShell
    QString startcmdshell = QString("exec sp_configure 'show advanced options',1 "
                                    "reconfigure "
                                    "exec sp_configure 'xp_cmdshell',1 "
                                    "reconfigure "
                                    "exec sp_configure 'show advanced options',0 "
                                    "reconfigure ");
    if(!query.exec(startcmdshell))
    {

    }

    //覆盖已存在信息,并将被覆盖文件存至历史备份文件夹
    QString filePath = QString("%1\\%2.bak").arg(buckupPath).arg(databaseName);
    QFile backupFile(filePath);
    if(backupFile.exists())
    {
        int result = QMessageBox::information(this,QStringLiteral("提示"),QStringLiteral("已存在%1,是否覆盖?").arg(filePath),QMessageBox::Ok|QMessageBox::Cancel);
        if(result == QMessageBox::Ok)
        {
            bool next = false;
            QDir historyBKDir;
            QDateTime currentDateTime;
            currentDateTime = QDateTime::currentDateTime();
            QDate currentDate = currentDateTime.date();
            QTime currentTime = currentDateTime.time();
            QString backupTime = QString("[%1_%2_%3 %4_%5_%6]%7")
                    .arg(currentDate.year())
                    .arg(QString("%1%2").arg((currentDate.month()<9)?"0":"").arg(currentDate.month()))
                    .arg(QString("%1%2").arg((currentDate.day()<9)?"0":"").arg(currentDate.day()))
                    .arg(QString("%1%2").arg((currentTime.hour()<9)?"0":"").arg(currentTime.hour()))
                    .arg(QString("%1%2").arg((currentTime.minute()<9)?"0":"").arg(currentTime.minute()))
                    .arg(QString("%1%2").arg((currentTime.second()<9)?"0":"").arg(currentTime.second()))
                    .arg(databaseName);
            historyBKDir.setPath(QStringLiteral("%1\\%2\\%3").arg(buckupPath).arg(QStringLiteral("历史备份")).arg(backupTime));

            //创建历史备份目录
            if(!historyBKDir.exists())
            {
                next = historyBKDir.mkpath(historyBKDir.path());
            }else
            {
                next = true;
            }

            //复制待覆盖历史备份文件
            if(true == next)
            {
                next = QFile::copy(filePath,QStringLiteral("%1\\%2.bak").arg(historyBKDir.path()).arg(databaseName));
            }else
            {
                qDebug()<<__FILE__<<__LINE__<<"can not create history backup path";
                return false;
            }

            //删除当前待覆盖备份文件
            if(true == next)
            {
                backupFile.remove();
            }else
            {
                qDebug()<<__FILE__<<__LINE__<<"can not copy .bak file";
                return false;
            }
        }else
        {
            return false;
        }
    }

    QString newFilePath = QString("exec xp_cmdshell 'mkdir %1'").arg(buckupPath);
    qDebug()<<__FILE__<<__LINE__<<"newFilePath = "<<newFilePath;
    if(!query.exec(newFilePath))
    {

    }
//        _BackupAndRecover.closeCmdshell(databaseName);
    QString sqlstr = QString("use %1 backup database %2 to disk = '%3\\%4.bak'").arg(databaseName).arg(databaseName).arg(buckupPath).arg(databaseName);

    qDebug()<<__FILE__<<__LINE__<<"sqlstr = "<<sqlstr;

    if(query.exec(sqlstr))
    {
        result = true;
        //QMessageBox::information(this,QStringLiteral("成功"), QStringLiteral("成功备份 [%1] 至 [%2]").arg(databaseName).arg(buckupPath));
    }else
    {
        //qDebug()<<__FILE__<<__LINE__<<query.lastError();
        //QMessageBox::information(this,QStringLiteral("失败"), QStringLiteral("备份失败,路径不存在"));
    }

    return result;
}

//还原数据库
bool BackupAndRecover::recoverDB(const QString& databaseName,QString recoverPath)
{
    bool result = false;
    QSqlDatabase database = getCurrentDB(databaseName);
    QSqlQuery query(database);

    //开启cmdShell
    QString startcmdshell = QString("exec sp_configure 'show advanced options',1 "
                                    "reconfigure "
                                    "exec sp_configure 'xp_cmdshell',1 "
                                    "reconfigure "
                                    "exec sp_configure 'show advanced options',0 "
                                    "reconfigure ");
    if(!query.exec(startcmdshell))
    {
    }

    QString sqlstr = QString("use master ALTER DATABASE %1 SET offline WITH ROLLBACK IMMEDIATE "
                             "RESTORE DATABASE %2 FROM DISK = '%3\\%4.bak' WITH REPLACE "
                             "ALTER DATABASE %5 SET online").arg(databaseName).arg(databaseName).arg(recoverPath).arg(databaseName).arg(databaseName);

    qDebug()<<__FILE__<<__LINE__<<"sqlstr = "<<sqlstr;

    if(query.exec(sqlstr))
    {
        result = true;
        //QMessageBox::information(this,QStringLiteral("成功"), QStringLiteral("成功还原 [%1] 从 [%2]").arg(databaseName).arg(recoverPath));
    }else
    {
        //qDebug()<<__FILE__<<__LINE__<<query.lastError();
        //QMessageBox::information(this,QStringLiteral("失败"), QStringLiteral("还原失败,未找到 [%1.bak]").arg(databaseName));
    }

    return result;
}

QSqlDatabase BackupAndRecover::getCurrentDB(const QString& databaseName)
{
    QSqlDatabase database ;
    //获取当前数据库

    return database;
}

头文件

#ifndef BUCKRECODB_H
#define BUCKRECODB_H
#include <QTreeWidgetItem>
#include <QTreeWidget>
#include <QStackedWidget>
#include <QProgressBar>
#include <QSettings>
#include <QSqlQuery>
class BackupAndRecover
{
public:
    BackupAndRecover();
    bool backupDB(const QString& databaseName, QString buckupPath);
    bool recoverDB(const QString& databaseName, QString recoverPath);
    QSqlDatabase getCurrentDB(const QString& databaseName);
};

#endif // BUCKRECODB_H

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值