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?
WITH REPLACE” option allows overwriting an existing database while doing a database restore process.
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.
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.
sqlcmd -S servername -Q "BACKUP DATABASE [DBName] TO DISK = 'C:\backup.bak' WITH INIT"
INIT
does the trick. From MSDN:
INIT Specifies that all backup sets should be overwritten
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With