Restore SQL Server database with different name, but let server handle filenames -


i created backup of sql server database this:

backup database [foo] disk = '\path\to\database.bak' 

now want restore database different name on same sql server instance, while original database foo still exists. command try use:

restore database [bar] disk = '\path\to\database.bak' 

when try in ssms, bunch of error messages telling me foo.mdf , foo_log.ldf cannot overwritten because in use database foo. have read far on , other places, resolve issue must use move option of restore command , specify filesystem path!!! have hard time accepting that: mean, sql server database server, , forces me deal low-level filesystem details perform database restore?!?

is there way how can tell sql server automatically figure out names of .mdf , .ldf files based on name of database restoring to? maybe need change backup command? or maybe can use different, higher-level api "backup database" / "restore database"?

note use case guarantees database restoring not exist.

edit: interactive solutions not option, need perform restore programmatically.

as there appears no elegant solution problem, bit bullet , implemented seems work - now. although not real answer question, thought else might benefit following thoughts.


here rough outline of implementation (tested on sql server 2012):

  1. create temporary database bar
  2. get real logical , physical file names system table select * sys.master_files database_id = db_id('bar')
  3. drop temporary database bar
  4. get backup logical , physical file names backup restore filelistonly disk = '\path\to\database.bak'
  5. make sure each file in backup (obtained in step 4) have corresponding real logical/physical file names (obtained in step 2)
  6. restore database restore database [bar] disk = '\path\to\database.bak'. add 1 move statement each file in backup logical file name backup mapped real physical file name (obtained in step 2).
  7. the restored database still has wrong logical names backup, fix these alter database [bar] modify file (name = 'backup_logical_filename', newname = 'real_logical_filename'). repeat each database file.

the approach of creating temporary database obtain "real" information database files (steps 1-3) allows delegate @ least few things dbms:

  • filesystem locations correct per dba's server configuration (e.g. separate locations data files , transaction logs)
  • mapping of database name file name filesystem name (e.g. if database name contains characters not valid in file names, such ":" character)

when matching real file names , backup file names (e.g. in step 5), file types in sys.master_files , in backup not same:

  • the file types in sys.master_files "rows" , "log" (or 0 , 1 if prefer numerical file type)
  • the file types in backup "d" , "l"

Comments

Popular posts from this blog

Why can rails not find a route created by a helper? -

javascript - jquery or ashx not working -

opencv - DataType<cv::detail::deriv_type>::depth what is it used for -