Skip to main content

Posts

Showing posts from May 17, 2011

Backup and Restore SQL Server Database from the Command Line

The most important part of a SQL Server maintenance plan is backing up your databases regularly. To backup a database, you cannot simply copy the respective MDF and LDF files of the database as SQL Server has a lock on these. Instead, you need to create a true backup file through SQL Server. While this can be done by developing a Maintenance Plan inside of SQL Management Studio, the free Express editions of SQL Server do not offer this interface. To work around this, you can easily backup your databases by running the command below while logged in as a Windows Administrator: SqlCmd -E -S Server_Name –Q “BACKUP DATABASE [Name_of_Database] TO DISK=’X:PathofBackupLocation[Name_of_Database].bak’” Example: Default SQL Server instance: SqlCmd -E -S MSSQLServer –Q “BACKUP DATABASE [Bookdb] TO DISK=’E:\Bookdb.bak’” Named SQL Server instance: SqlCmd -E -S MyOwnInstance –Q “BACKUP DATABASE [Bookdb] TO DISK=’E:\Bookdb.bak’” The above create a fully restorable backup copy of “Bookdb” to th