1

在数据库备份项目的最后,我遇到了一个问题,我放入的 Deflate 压缩似乎找不到我保存备份的路径。由于默认备份位置(此处使用)位于网络驱动器上,我还需要做些什么来确保 Deflate 找到该路径吗?截至目前,我得到 System.IO.DirectoryNotFoundException: 'Could not find a part of the path。该工具的目的是能够放入您要访问的任何服务器,获取可用数据库的列表,然后选择您要备份的数据库。

我以前在本地遇到过这个问题,但我所要做的就是为 SQLserver 提供对该文件夹的适当权限。

using (SqlConnection newConn = new SqlConnection(connString))
        using (SqlCommand sqlCmd = new SqlCommand(query, newConn))
        {
            newConn.Open();
            value = sqlCmd.ExecuteScalar();
            canCompress = !(value == null || Convert.ToInt32(value) == 0);

            //----------------------------------
            //SQL Commands to run backup process
            //----------------------------------
            Interface.WriteLine("Creating backup");               
            if (canCompress)
            {
                sqlCmd.CommandText = "BACKUP DATABASE [" + connBuilder.InitialCatalog + "] "
                                    + "TO DISK = '" + backupFile + "' "
                                    + "WITH COPY_ONLY, COMPRESSION, NOFORMAT, NOINIT, "
                                    + "NAME = '" + backupName + "', "
                                    + "SKIP, REWIND, NOUNLOAD, STATS = 10";
                sqlCmd.ExecuteNonQuery();
            }
            else
            {
                sqlCmd.CommandText = "BACKUP DATABASE [" + connBuilder.InitialCatalog + "] "
                                    + "TO DISK = '" + backupFile + "' "
                                    + "WITH COPY_ONLY, NOFORMAT, NOINIT, "
                                    + "NAME = '" + backupName + "', "
                                    + "SKIP, REWIND, NOUNLOAD, STATS = 10";
                sqlCmd.ExecuteNonQuery();
            }

            //----------------------------------
            //Grab Backup File
            //----------------------------------
            query = "SELECT physical_device_name "
                    + "FROM msdb.dbo.backupset b "
                    + "JOIN msdb.dbo.backupmediafamily m ON b.media_set_id = m.media_set_id "
                    + "WHERE database_name = '" + connBuilder.InitialCatalog + "' "
                    + "ORDER BY backup_finish_date DESC ";

            using (SqlConnection connection = new SqlConnection(connString))
            using (SqlCommand cmd = new SqlCommand(query, connection))
            {
                connection.Open();
                value = cmd.ExecuteScalar();
                if (value != null)
                    backupFile = (string)value;
                else
                    throw new Exception("Unable to find backup file.");
            }

            //Set which files should be uploaded.
            if (canCompress)
            {
                fileToUpload = backupFile;
            }
            else
            {
                fileToUpload = Deflate.CompressFile(backupFile); //Point of error message
                File.Delete(backupFile);
            }

            return fileToUpload;
        }


static class Deflate
{
        public static string CompressFile(string sourcePath, string destPath = null)
        {
            if (destPath == null)
                destPath = Path.Combine(Path.GetDirectoryName(sourcePath), Path.GetFileNameWithoutExtension(sourcePath) + ".cmp");

            using (FileStream originalFileStream = File.OpenRead(sourcePath))
            using (FileStream compressedFileStream = File.Create(destPath))
            using (DeflateStream compressionStream = new 
    DeflateStream(compressedFileStream, CompressionMode.Compress))
     {
          originalFileStream.CopyTo(compressionStream);
          compressedFileStream.Flush();
      }

            FileInfo sourceInfo = new FileInfo(sourcePath); //Remove the .bak extension on compression?
            FileInfo destInfo = new FileInfo(destPath); //Remove the .bak extension on compression?
            Console.WriteLine("Compressed {0} from {1} to {2} bytes.", Path.GetFileName(sourcePath), sourcePath.Length, destInfo.Length);
            return destPath;
        }
 }
4

0 回答 0