备份文件命令SqlServer一键复制数据库脚本
發(fā)一下牢騷和主題無關(guān):
????假定有兩個Server:192.168.0.1和192.168.0.2,須要復(fù)制的數(shù)據(jù)庫名為TestDB,現(xiàn)將該庫由192.168.0.1復(fù)制到192.168.0.2,其中%source_database_backup_file_dir%為寄存192.168.0.1上TestDB庫備份文件的文件夾,%target_database_file_dir%為192.168.0.2上TestDB數(shù)據(jù)庫文件地點的文件夾,BAT本腳如下:
@echo offset source_server=192.168.0.1 set source_server_user=sa set source_server_password=saset target_server=192.168.0.2 set target_server_user=sa set target_server_password=saset database=TestDBset source_database_backup_file_dir=\\Systemname\ShareName\Path set target_database_file_dir=\\Systemname\ShareName\Pathecho Start to backup source database... osql -S"%source_server%" -U"%source_server_user%" -P"%source_server_password%" -n -h-1 -d"%database%" -Q"BACKUP DATABASE %database% TO DISK = '%source_database_backup_file_dir%\%database%.bak' WITH INIT;"echo Disconnect all existing connections of target database. osql -S"%target_server%" -U"%target_server_user%" -P"%target_server_password%" -Q"declare @i int declare cur cursor for select spid from sysprocesses where db_name(dbid)= '%database%' open cur fetch next from cur into @i while @@fetch_status=0 begin exec('kill '+@i) fetch next from cur into @i end close cur deallocate cur"echo Start to resotre target database from source database backup file... osql -S"%target_server%" -U"%target_server_user%" -P"%target_server_password%" -n -h-1 -d"Master" -Q"RESTORE DATABASE %database% FROM DISK = '%source_database_backup_file_dir%\%database%.bak' WITH REPLACE,MOVE '%database%' TO '%source_database_backup_file_dir%\%database%.mdf',MOVE '%database%_Log' TO '%source_database_backup_file_dir%\%database%_log.ldf';"echo DONE!!pause 每日一道理書,各種各樣的書。書,寄托著人類熱切的希望;書,蘊含著人類豐富的感悟。提起書,會有說不完的話語……
????本本腳要主用使BACKUP命令為原庫成生一個備份文件,然后用使RESTORE命令將備份文件復(fù)恢到目標(biāo)庫,從而現(xiàn)實數(shù)據(jù)庫復(fù)制。關(guān)于BACKUP命令請參考:http://msdn.microsoft.com/zh-cn/library/ms186865.aspx,關(guān)于RESTORE命令請參考:http://msdn.microsoft.com/zh-cn/library/ms186858.aspx
????有兩個細節(jié)說明一下:
????1. BACKUP語句中的WITH INIT用于每次備份時覆蓋原備份文件中的數(shù)據(jù)。
????2. 由于兩個Server都須要拜訪數(shù)據(jù)庫備份文件 ,寄存?zhèn)浞菸募奈募A一般要設(shè)置為"享共"文件夾。
????
文章結(jié)束給大家分享下程序員的一些笑話語錄: 手機終究會變成PC,所以ip會比wm更加暢銷,但是有一天手機強大到一定程度了就會發(fā)現(xiàn)只有wm的支持才能完美享受。就好比樹和草,草長得再高也是草,時間到了條件成熟了樹就會竄天高了。www.ishuo.cn
轉(zhuǎn)載于:https://www.cnblogs.com/xinyuyuanm/archive/2013/04/22/3036526.html
總結(jié)
以上是生活随笔為你收集整理的备份文件命令SqlServer一键复制数据库脚本的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android系统介绍
- 下一篇: mysql备份、导入、远程数据库设置