Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Override file while backup database

I want to back up a database using this code

sqlcmd -S servername -Q "BACKUP DATABASE [DBName] TO DISK = 'C:\backup.bak'"

It works. But if the backup file already exists, the data gets appended to the file instead of replacing the file. Every time I call BACKUP DATABASE the file gets bigger.

Is there an option for BACKUP DATABASE to force a replace?

like image 572
juergen d Avatar asked Dec 04 '12 18:12

juergen d


People also ask

Which backup option will help you to overwrite an existing backup file with the new backup file if the backups are using the same backup name?

WITH REPLACE” option allows overwriting an existing database while doing a database restore process.

Does taking a backup lock the database?

No. Backup operations do not take locks on user objects. Backups do cause a really heavy read load on the I/O subsystem so it might look like the workload is being blocked, but it isn't really. It's just being slowed down.

What is Noinit in SQL Server?

INIT: INIT is used to create a new backup set; NOINIT is used for appending the backup to the existing backup set. The NOINIT parameter is used mostly when you backup the database to a tape device.


2 Answers

sqlcmd -S servername -Q "BACKUP DATABASE [DBName] TO DISK = 'C:\backup.bak' WITH INIT" 
like image 58
bummi Avatar answered Sep 20 '22 03:09

bummi


INIT does the trick. From MSDN:

INIT Specifies that all backup sets should be overwritten

like image 27
Serge Belov Avatar answered Sep 22 '22 03:09

Serge Belov