49

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?

juergen d
  • 201,996
  • 37
  • 293
  • 362

4 Answers4

82
sqlcmd -S servername -Q "BACKUP DATABASE [DBName] TO DISK = 'C:\backup.bak' WITH INIT"
bummi
  • 27,123
  • 14
  • 62
  • 101
30

INIT does the trick. From MSDN:

INIT Specifies that all backup sets should be overwritten

Serge Belov
  • 5,633
  • 1
  • 31
  • 40
3

WITH INIT is not enough. Should be WITH INIT, SKIP these days. Docs

Explanation: INIT overwrites only if some conditions are met. SKIP instructs to ignore those conditions.

Alex from Jitbit
  • 53,710
  • 19
  • 160
  • 149
0

BACKUP DATABASE SQ_P TO DISK='D:\Data Backup\SQ_P.bak' with init;

where SQ_P is the Database Name

  • Please edit your answer! This question no need for new answer and your answer isn't minimum of policy for answer question format. please read help center – Amirhossein Oct 05 '21 at 19:09