Sometime it is easy and quick to run batch file for back up and restore database. Here is some scripts you can use

Backup mdf from current directory to Backup1 folder
********************************
@echo off

set username=sa
set password=sysadm
set database=NorthWind

if exist “User Preferences.bat” call “User Preferences.bat”

osql -U %username% -P %password% -d master -Q “alter database %database% set single_user with rollback immediate”
osql -U %username% -P %password% -d master -Q “exec sp_detach_db ‘%database%’, ‘true'”

echo Copying files…
copy %database%_data.mdf “Backup1”
copy %database%_Log.LDF “Backup1”

echo.

osql -U %username% -P %password% -d master -Q “exec sp_attach_db ‘%database%’, ‘%cd%\%database%_Data.MDF’, ‘%cd%\%database%_Log.LDF'”
osql -U %username% -P %password% -d master -Q “alter database %database% set multi_user with rollback immediate”

pause
********************************

Restore database from Backup1 folder to current directory
********************************
@echo off

set username=sa
set password=sysadm
set database=NorthWind

if exist “User Preferences.bat” call “User Preferences.bat”

osql -U %username% -P %password% -d master -Q “alter database %database% set single_user with rollback immediate”
osql -U %username% -P %password% -d master -Q “exec sp_detach_db ‘%database%’, ‘true'”

echo Copying files…
xcopy “Backup1” /y/R
echo.

osql -U %username% -P %password% -d master -Q “exec sp_attach_db ‘%database%’, ‘%cd%\%database%_Data.MDF’, ‘%cd%\%database%_Log.LDF'”
osql -U %username% -P %password% -d master -Q “alter database %database% set multi_user with rollback immediate”

pause
********************************

Advertisements