Backup a database on a HDD with a different sector size

后端 未结 6 1050
猫巷女王i
猫巷女王i 2021-02-02 11:16

In our development environment we have long been using a particular backup and restore script for each of our products through various SQL Server versions and different environm

6条回答
  •  忘了有多久
    2021-02-02 11:47

    I ran into the same issue as the OP. On a dev machine, we had a PowerShell script that backed up databases from remote database servers and stored the backup files locally. The script overwrote the same backup files, over and over, and the script had been working fine for a couple years. Then I cloned the spinning media drive to an SSD in the dev machine. Suddenly, we were getting the same error as the OP:

    Backup-SqlDatabase : System.Data.SqlClient.SqlError: Cannot use the backup file '\DevMachine\Back-Up\Demo.bak' because it was originally formatted with sector size 4096 and is now on a device with sector size 512.

    Sure, I could delete all of the existing .bak files to fix the problem. But what if it happens, again? I wanted a command line solution that consistently worked.

    Here's our original code:

    Backup-SqlDatabase -ServerInstance "DBServer1" -Database "Demo" -BackupFile "\\DevMachine\Back-Up\Demo.bak" -BackupAction Database -CopyOnly -CompressionOption On -ConnectionTimeout 0 -Initialize -Checksum -ErrorAction Stop
    

    After some fiddling around, I changed it to the following to fix the problem:

    Backup-SqlDatabase -ServerInstance "DBServer1" -Database "Demo" -BackupFile "\\DevMachine\Back-Up\Demo.bak" -BackupAction Database -CopyOnly -CompressionOption On -ConnectionTimeout 0 -Initialize -Checksum -FormatMedia -SkipTapeHeader -ErrorAction Stop
    

    Basically, the following options were added to fix the issue:

    -FormatMedia -SkipTapeHeader
    

    Note that if you read the documentation for the Backup-SqlDatabase cmdlet, -FormatMedia is listed as only applying to tapes and not to disk backups. However, it appears to do the job of blowing away the existing backup file when backing up to disk.
    - https://docs.microsoft.com/en-us/powershell/module/sqlps/backup-sqldatabase

    I found that if I used the -FormatMedia option by itself, it generated the following error:

    Backup-SqlDatabase : The FormatMedia and SkipTapeHeader properties have conflicting settings.

    I fixed the second error by adding an additional option: -SkipTapeHeader. Clearly that's also intended for tape backups, but it worked.

提交回复
热议问题