首页 > 数据库 >sqlserver 通过压缩bak文件实现从服务器还原数据库《数据差异数个小时》

sqlserver 通过压缩bak文件实现从服务器还原数据库《数据差异数个小时》

时间:2024-06-05 15:35:45浏览次数:23  
标签:string buffer 压缩 sqlserver rootPath Length new 服务器 bak

十年河东,十年河西,莫欺少年穷

学无止境,精益求精

1、备份主服务器数据库并压缩

   public void DbBack()
        {
            var bakname = @"ChargeDB_" + DateTime.Now.ToString("yyyyMMdd") + ".bak";
            string filepath = @"D:\dbback\" + bakname;
            if (File.Exists(filepath))
            {
                File.Delete(filepath);
            }
            string sql = @"declare @date nvarchar(10)  
set @date = CONVERT(nvarchar(10),getdate(),112) 
declare @path nvarchar(250) 
set @path = 'D:\dbback\' 
declare @db_filename nvarchar(150)  
set @db_filename = @path + 'ChargeDB_'+@date+'.bak' 
backup database ChargeDB TO DISK=@db_filename  ";
            DataRepository.ExecuteCommand(sql);
            //压缩文件 供备份服务器下载
            var dbpath = @"D:\dbback\ChargeDB_" + DateTime.Now.ToString("yyyyMMdd") + ".bak";
            var zippath = @"D:\dbback\ChargeDB_" + DateTime.Now.ToString("yyyyMMdd") + ".zip";
            if (File.Exists(dbpath))
            {
                Console.WriteLine("存在数据备份文件");
                ZipUtility zip = new ZipUtility();
                zip.ZipFile(dbpath, zippath, 2, 2048);
            }
        }
View Code

2、从服务器解压缩并还原

        static void Main(string[] args)
        {
            var zippath = @"C:\dbback";
            var zipname = @"ChargeDB_" + DateTime.Now.ToString("yyyyMMdd") + ".zip";
            var bakname = @"ChargeDB_" + DateTime.Now.ToString("yyyyMMdd") + ".bak";
            FileLoad(zipname, zippath);
            //
            ZipUtility zip = new ZipUtility();
            zip.UnZip(zippath+@"\"+ zipname, zippath);
            Console.WriteLine("UnZipOVER");
            RestoreDataBase("ChargeDB", zippath+@"\" + bakname);
            Console.WriteLine(" RestoreDataBaseOver");
            Console.Read();
        }
        /// <summary>
        /// 下载文件
        /// </summary>
        /// <param name="fileName">客户端保存的文件名</param>
        /// <param name="filePath">保存的文件夹路径</param>
        /// <returns></returns>
        public static void FileLoad(string fileName, string filePath)
        {
            //判断保存的文件夹是否存在
            if (!Directory.Exists(filePath))
            {
                //不存在则创建
                Directory.CreateDirectory(filePath);
            }
            //System.Net 中的验证和下载方法
            WebClient client = new WebClient();
            client.Credentials = CredentialCache.DefaultCredentials;
            client.DownloadFile("https://chengxiangzhineng.com/dbsite/ChargeDB_20240605.zip", filePath + "\\" + fileName);

            Console.WriteLine("下载完成");
        }

        public static void RestoreDataBase(string dataBaseName, string dataBaseBackupFile)
        {
            var CommandText = string.Format(@"USE [master]
ALTER DATABASE {0}
SET SINGLE_USER
WITH ROLLBACK IMMEDIATE;
restore database {0}  from DISK='{1}'  
WITH  REPLACE
ALTER DATABASE {0}
SET MULTI_USER;", dataBaseName, dataBaseBackupFile);
 
            DataRepository.ExecuteCommand(CommandText);
        }
View Code

3、帮助类

using ICSharpCode.SharpZipLib.Checksum;
using ICSharpCode.SharpZipLib.Zip;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace swapCommon
{
    public class ZipUtility
    {
        /// <summary>  
        /// 所有文件缓存  
        /// </summary>  
        List<string> files = new List<string>();

        /// <summary>  
        /// 所有空目录缓存  
        /// </summary>  
        List<string> paths = new List<string>();

        /// <summary>  
        /// 压缩单个文件  
        /// </summary>  
        /// <param name="fileToZip">要压缩的文件</param>  
        /// <param name="zipedFile">压缩后的文件全名</param>  
        /// <param name="compressionLevel">压缩程度,范围0-9,数值越大,压缩程序越高</param>  
        /// <param name="blockSize">分块大小</param>  
        public void ZipFile(string fileToZip, string zipedFile, int compressionLevel, int blockSize)
        {
            if (!System.IO.File.Exists(fileToZip))//如果文件没有找到,则报错  
            {
                throw new FileNotFoundException("The specified file " + fileToZip + " could not be found. Zipping aborderd");
            }

            FileStream streamToZip = new FileStream(fileToZip, FileMode.Open, FileAccess.Read);
            FileStream zipFile = File.Create(zipedFile);
            ZipOutputStream zipStream = new ZipOutputStream(zipFile);
            ZipEntry zipEntry = new ZipEntry(fileToZip);
            zipStream.PutNextEntry(zipEntry);
            zipStream.SetLevel(compressionLevel);
            byte[] buffer = new byte[blockSize];
            int size = streamToZip.Read(buffer, 0, buffer.Length);
            zipStream.Write(buffer, 0, size);

            try
            {
                while (size < streamToZip.Length)
                {
                    int sizeRead = streamToZip.Read(buffer, 0, buffer.Length);
                    zipStream.Write(buffer, 0, sizeRead);
                    size += sizeRead;
                }
            }
            catch (Exception ex)
            {
                GC.Collect();
                throw ex;
            }

            zipStream.Finish();
            zipStream.Close();
            streamToZip.Close();
            GC.Collect();
        }

        /// <summary>  
        /// 压缩目录(包括子目录及所有文件)  
        /// </summary>  
        /// <param name="rootPath">要压缩的根目录</param>  
        /// <param name="destinationPath">保存路径</param>  
        /// <param name="compressLevel">压缩程度,范围0-9,数值越大,压缩程序越高</param>  
        public void ZipFileFromDirectory(string rootPath, string destinationPath, int compressLevel)
        {
            GetAllDirectories(rootPath);

            /* while (rootPath.LastIndexOf("\\") + 1 == rootPath.Length)//检查路径是否以"\"结尾 
            { 
 
            rootPath = rootPath.Substring(0, rootPath.Length - 1);//如果是则去掉末尾的"\" 
 
            } 
            */
            //string rootMark = rootPath.Substring(0, rootPath.LastIndexOf("\\") + 1);//得到当前路径的位置,以备压缩时将所压缩内容转变成相对路径。  
            string rootMark = rootPath + "\\";//得到当前路径的位置,以备压缩时将所压缩内容转变成相对路径。  
            Crc32 crc = new Crc32();
            ZipOutputStream outPutStream = new ZipOutputStream(File.Create(destinationPath));
            outPutStream.SetLevel(compressLevel); // 0 - store only to 9 - means best compression  
            foreach (string file in files)
            {
                FileStream fileStream = File.OpenRead(file);//打开压缩文件  
                byte[] buffer = new byte[fileStream.Length];
                fileStream.Read(buffer, 0, buffer.Length);
                ZipEntry entry = new ZipEntry(file.Replace(rootMark, string.Empty));
                entry.DateTime = DateTime.Now;

                entry.Size = fileStream.Length;
                fileStream.Close();
                crc.Reset();
                crc.Update(buffer);
                entry.Crc = crc.Value;
                outPutStream.PutNextEntry(entry);
                outPutStream.Write(buffer, 0, buffer.Length);
            }

            this.files.Clear();

            foreach (string emptyPath in paths)
            {
                ZipEntry entry = new ZipEntry(emptyPath.Replace(rootMark, string.Empty) + "/");
                outPutStream.PutNextEntry(entry);
            }

            this.paths.Clear();
            outPutStream.Finish();
            outPutStream.Close();
            GC.Collect();
        }

        /// <summary>  
        /// 取得目录下所有文件及文件夹,分别存入files及paths  
        /// </summary>  
        /// <param name="rootPath">根目录</param>  
        private void GetAllDirectories(string rootPath)
        {
            string[] subPaths = Directory.GetDirectories(rootPath);//得到所有子目录  
            foreach (string path in subPaths)
            {
                GetAllDirectories(path);//对每一个字目录做与根目录相同的操作:即找到子目录并将当前目录的文件名存入List  
            }
            string[] files = Directory.GetFiles(rootPath);
            foreach (string file in files)
            {
                this.files.Add(file);//将当前目录中的所有文件全名存入文件List  
            }
            if (subPaths.Length == files.Length && files.Length == 0)//如果是空目录  
            {
                this.paths.Add(rootPath);//记录空目录  
            }
        }

        /// <summary>  
        /// 解压缩文件(压缩文件中含有子目录)  
        /// </summary>  
        /// <param name="zipfilepath">待解压缩的文件路径</param>  
        /// <param name="unzippath">解压缩到指定目录</param>  
        /// <returns>解压后的文件列表</returns>  
        public List<string> UnZip(string zipfilepath, string unzippath)
        {
            //解压出来的文件列表  
            List<string> unzipFiles = new List<string>();

            //检查输出目录是否以“\\”结尾  
            if (unzippath.EndsWith("\\") == false || unzippath.EndsWith(":\\") == false)
            {
                unzippath += "\\";
            }

            ZipInputStream s = new ZipInputStream(File.OpenRead(zipfilepath));
            ZipEntry theEntry;
            while ((theEntry = s.GetNextEntry()) != null)
            {
                string directoryName = Path.GetDirectoryName(unzippath);
                string fileName = Path.GetFileName(theEntry.Name);

                //生成解压目录【用户解压到硬盘根目录时,不需要创建】  
                if (!string.IsNullOrEmpty(directoryName))
                {
                    Directory.CreateDirectory(directoryName);
                }

                if (fileName != String.Empty)
                {
                    //如果文件的压缩后大小为0那么说明这个文件是空的,因此不需要进行读出写入  
                    if (theEntry.CompressedSize == 0)
                        continue;
                    //解压文件到指定的目录  
                    directoryName = Path.GetDirectoryName(unzippath + fileName);
                    //建立下面的目录和子目录  
                    Directory.CreateDirectory(directoryName);

                    //记录导出的文件  
                    unzipFiles.Add(unzippath + fileName);

                    FileStream streamWriter = File.Create(unzippath + fileName);

                    int size = 2048;
                    byte[] data = new byte[2048];
                    while (true)
                    {
                        size = s.Read(data, 0, data.Length);
                        if (size > 0)
                        {
                            streamWriter.Write(data, 0, size);
                        }
                        else
                        {
                            break;
                        }
                    }
                    streamWriter.Close();
                }
            }
            s.Close();
            GC.Collect();
            return unzipFiles;
        }

        public string GetZipFileExtention(string fileFullName)
        {
            int index = fileFullName.LastIndexOf(".");
            if (index <= 0)
            {
                throw new Exception("The source package file is not a compress file");
            }

            //extension string
            string ext = fileFullName.Substring(index);

            if (ext == ".rar" || ext == ".zip")
            {
                return ext;
            }
            else
            {
                throw new Exception("The source package file is not a compress file");
            }
        }
    }
}
View Code

注:当主服务器备份时,可通过某种通知机制,告诉从服务器进行下载,解压缩并还原。保持备份/还原同步

over

标签:string,buffer,压缩,sqlserver,rootPath,Length,new,服务器,bak
From: https://www.cnblogs.com/chenwolong/p/18233123

相关文章

  • Ubuntu server 24 (Linux) 安装部署smartdns 搭建智能DNS服务器
    SmartDNS是推荐本地运行的DNS服务器,SmartDNS接受本地客户端的DNS查询请求,从多个上游DNS服务器获取DNS查询结果,并将访问速度最快的结果返回给客户端,提高网络访问速度和准确性。支持指定域名IP地址,达到禁止过滤的效果。一安装smartdns1 关闭Ubuntu自带dns解析systemd-resolv......
  • 服务器数据恢复概述
    服务器数据恢复是一个复杂的过程,涉及到多种技术和方法。以下是一些关键步骤和考虑因素:确定数据丢失的原因在进行数据恢复之前,首先需要确定数据丢失的原因。这可能包括硬件故障、软件错误、病毒攻击、人为操作失误等。了解数据丢失的原因有助于选择合适的恢复方法和工具。选择......
  • 前端引入,Web服务器的本质,HTTP协议,HTML基础,常用标签介绍
    Ⅰ前端引入【一】前端/后端【1】什么是前端任何与用户直接打交道的操作界面都可以称之为前端前端可以是浏览器的界面、也可以是客户端的界面、还可以是手机的界面...比如电脑界面、手机界面、平板界面【2】什么是后端不直接与用户打交道的用于执行真正业务逻辑的代......
  • 设计程序,要求程序可以加入到一个多播组中并等待服务器发送数据包,并且程序还需要具有发
    目录题目分析代码结果题目小组实现,小组中的每位成员都需要设计程序,要求程序可以加入到一个多播组中并等待服务器发送数据包,并且程序还需要具有发送功能,如果收到数据包则把消息内容输出到终端,消息内容格式[消息来源IP消息时间]:消息内容分析1.发送端需设置套接字的广......
  • 自宿主web服务器搭建记录
    建立3个项目,分别是类库WebSite、控制台TestWebSite、win服务WinService,目标框架均为.NETFramework4.8.1。其中控制台方便开发调试,win服务作为宿主服务,类库为自定义webservice的主体代码。TestWebSite和WinService引用nuget包 Microsoft.Owin.Host.HttpListener 4.2.2,不然......
  • 服务器安装centos系统报错
    安装centos报错:dracut-initqueue:warning:dracut-initqueuetimeout-startingtimeoutscripts解决方法U盘启动报错信息 查看U盘的对应分区 重启电脑按e进入编辑模式将:vmlinuzinitrd=initrd.imginst.stage2=hd:LABEL=CentOS\x207\x20x86_64rd.live.checkquiet改......
  • UDP练习题——实现将自己加入到多播组中并等待服务器发送数据包
    设计程序,要求程序可以加入到一个多播组中并等待服务器发送,数据包,并且程序还需要具有发送功能,如果收到数据包则把消息内容输出到终端,消息内容格,式「消息来源IP消息时间1:消息内容多播地址和端口号/*************************************************************************......
  • Linux服务器磁盘清理与Inode节点清理指南
    Linux服务器磁盘清理与Inode节点清理指南在管理Linux服务器时,定期清理磁盘空间和inode节点是维护系统性能和稳定性的重要任务。磁盘空间清理可确保系统不会因为空间不足而出现问题,而inode节点清理则有助于避免系统因过多小文件而性能下降。本指南将介绍如何执行这些清理操......
  • 基于BungeeCord搭建 多服务端 Minecraft 我的世界 Bedwars服务器 教程
    本文基于BungeeCord搭建多服务我的世界起床战争服务器本文章后续会持续更新由于多世界插件EssentialsX-Core与Bedwars1058在部分指令上有冲突,于是建议使用BungeeCord(之后我简称BC)搭建多服务端minecraft服务器,将起床Bedwars服务分离出来,顺便将其他的服务比如登陆大厅等分离出......
  • 省钱攻略:阿里云99元服务器续费到2027年方法
    阿里云99元服务器新老用户均可以买!!!你没看错,老用户可以买!2核2G3M固定带宽,不限流量!!并且续费不涨价,原价99元即可续费,一直可以续费到2027年,相当于396元买4年99元服务器(买4年396元)新老用户同享云服务Q器活动入口:》》》传送门 无论是新老用户,都可以用99元下单购买,自......