sqlserver 透過壓縮bak檔案實現從伺服器還原資料庫《資料差異數個小時》

天才卧龙發表於2024-06-05

十年河東,十年河西,莫欺少年窮

學無止境,精益求精

1、備份主伺服器資料庫並壓縮

sqlserver 透過壓縮bak檔案實現從伺服器還原資料庫《資料差異數個小時》
   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、從伺服器解壓縮並還原

sqlserver 透過壓縮bak檔案實現從伺服器還原資料庫《資料差異數個小時》
        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、幫助類

sqlserver 透過壓縮bak檔案實現從伺服器還原資料庫《資料差異數個小時》
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

相關文章