SQL Server数据库常用操作脚本,包括备份数据库、恢复备份数据库、分离数据库、附加数据库、还原数据库、清理日志!
使用方法:
1、新建数据库脚本
在服务器上创建一个txt文件,后缀改成sql,如:备份数据库.sql
2、修改数据库表名
把下面常用脚本代码内容复制进去,修改对应数据库表名,如有些是 QPAccountsDB.bak 或 RYccountsDB.bak。
3、执行脚本
双击你的备份数据库.sql脚本在数据库工具里打开,点击执行就可以:
常用脚本:
1、备份数据库
USE master go IF EXISTS ( SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'pro_backupplain') AND OBJECTPROPERTY(id, N'IsProcedure') = 1 ) DROP PROC pro_backupplain; GO CREATE PROC pro_backupplain @backpath VARCHAR(200) AS BEGIN DECLARE @dbname AS NVARCHAR(30) , @sql AS NVARCHAR(500) , --@backpath AS VARCHAR(100), @deletetime AS VARCHAR(19) DECLARE C_dblist CURSOR FAST_FORWARD FOR SELECT Name FROM Master..SysDatabases WHERE name NOT IN ( 'master', 'tempdb', 'model', 'msdb', 'ReportServer', 'ReportServerTempDB' ) ORDER BY Name; OPEN C_dblist; FETCH NEXT FROM C_dblist INTO @dbname; --SET @backpath ='D:\dbbackup'; WHILE @@FETCH_STATUS = 0 BEGIN SET @sql = 'BACKUP DATABASE ' + @dbname + ' TO DISK = N''' + @backpath + '\' + @dbname + '.bak'''; EXEC(@sql) PRINT @sql FETCH NEXT FROM C_dblist INTO @dbname; END CLOSE C_dblist; DEALLOCATE C_dblist; SET @deletetime = CONVERT(VARCHAR(20), DATEADD(DD, -7, GETDATE()), 126); SET @sql = 'EXECUTE master.dbo.xp_delete_file 0,N''' + @backpath + ''',N''bak'',N''' + @deletetime + ''''; PRINT @sql EXEC(@sql) END go EXECUTE pro_backupplain 'D:\Game\Sql'
2、恢复数据库备份
restore filelistonly from disk='D:\Game\Sql\QPAccountsDB.bak'
3、分离数据库
sp_detach_db 'QPAccountsDB' GO sp_detach_db 'QPAgentsDB' GO sp_detach_db 'QPGameMatchDB' GO sp_detach_db 'QPGameScoreDB' GO sp_detach_db 'QPGameSportsDB' GO sp_detach_db 'QPNativeWebDB' GO sp_detach_db 'QPPlatformDB' GO sp_detach_db 'QPPlatformManagerDB' GO sp_detach_db 'QPRecordDB' GO sp_detach_db 'QPTreasureDB' GO
4、附加数据库
sp_attach_db 'QPAccountsDB','D:\Game\Data\QPAccountsDB.mdf','D:\Game\Data\QPAccountsDB_log.ldf' GO sp_attach_db 'QPAgentsDB','D:\Game\Data\QPAgentsDB.mdf','D:\Game\Data\QPAgentsDB_log.ldf' GO sp_attach_db 'QPGameMatchDB','D:\Game\Data\QPGameMatchDB.mdf','D:\Game\Data\QPGameMatchDB_log.ldf' GO sp_attach_db 'QPGameScoreDB','D:\Game\Data\QPGameScoreDB.mdf','D:\Game\Data\QPGameScoreDB_log.ldf' GO sp_attach_db 'QPGameSportsDB' ,'D:\Game\Data\QPGameSportsDB.mdf','D:\Game\Data\QPGameSportsDB_log.ldf' GO sp_attach_db 'QPNativeWebDB' ,'D:\Game\Data\QPNativeWebDB.mdf','D:\Game\Data\QPNativeWebDB_log.ldf' GO sp_attach_db 'QPPlatformDB' ,'D:\Game\Data\QPPlatformDB.mdf','D:\Game\Data\QPPlatformDB_log.ldf' GO sp_attach_db 'QPPlatformManagerDB' ,'D:\Game\Data\QPPlatformManagerDB.mdf','D:\Game\Data\QPPlatformManagerDB_log.ldf' GO sp_attach_db 'QPRecordDB' ,'D:\Game\Data\QPRecordDB.mdf','D:\Game\Data\QPRecordDB_log.ldf' GO sp_attach_db 'QPTreasureDB' ,'D:\Game\Data\QPTreasureDB.mdf','D:\Game\Data\QPTreasureDB_log.ldf' GO
5、还原数据库
restore DATABASE QPAccountsDB from disk='D:\Game\Sql\QPAccountsDB.bak' WITH FILE = 1, MOVE N'QPAccountsDB' TO N'D:\Game\Data\QPAccountsDB.mdf', --数据文件路径 MOVE N'QPAccountsDB_log' TO N'D:\Game\Data\QPAccountsDB_log.ldf', --日志文件路径 NOUNLOAD, STATS = 10 GO restore DATABASE QPAgentsDB from disk='D:\Game\Sql\QPAgentsDB.bak' WITH FILE = 1, MOVE N'QPAgentsDB' TO N'D:\Game\Data\QPAgentsDB.mdf', --数据文件路径 MOVE N'QPAgentsDB_log' TO N'D:\Game\Data\QPAgentsDB_log.ldf', --日志文件路径 NOUNLOAD, STATS = 10 GO restore DATABASE QPGameMatchDB from disk='D:\Game\Sql\QPGameMatchDB.bak' WITH FILE = 1, MOVE N'QPGameMatchDB' TO N'D:\Game\Data\QPGameMatchDB.mdf', --数据文件路径 MOVE N'QPGameMatchDB_log' TO N'D:\Game\Data\QPGameMatchDB_log.ldf', --日志文件路径 NOUNLOAD, STATS = 10 GO restore DATABASE QPGameScoreDB from disk='D:\Game\Sql\QPGameScoreDB.bak' WITH FILE = 1, MOVE N'QPGameScoreDB' TO N'D:\Game\Data\QPGameScoreDB.mdf', --数据文件路径 MOVE N'QPGameScoreDB_log' TO N'D:\Game\Data\QPGameScoreDB_log.ldf', --日志文件路径 NOUNLOAD, STATS = 10 GO restore DATABASE QPGameSportsDB from disk='D:\Game\Sql\QPGameSportsDB.bak' WITH FILE = 1, MOVE N'QPGameSportsDB' TO N'D:\Game\Data\QPGameSportsDB.mdf', --数据文件路径 MOVE N'QPGameSportsDB_log' TO N'D:\Game\Data\QPGameSportsDB_log.ldf', --日志文件路径 NOUNLOAD, STATS = 10 GO restore DATABASE QPNativeWebDB from disk='D:\Game\Sql\QPNativeWebDB.bak' WITH FILE = 1, MOVE N'QP_News' TO N'D:\Game\Data\QPNativeWebDB.mdf', --数据文件路径 MOVE N'QP_News_log' TO N'D:\Game\Data\QPNativeWebDB_log.ldf', --日志文件路径 NOUNLOAD, STATS = 10 GO restore DATABASE QPPlatformDB from disk='D:\Game\Sql\QPPlatformDB.bak' WITH FILE = 1, MOVE N'QPPlatformDB' TO N'D:\Game\Data\QPPlatformDB.mdf', --数据文件路径 MOVE N'QPPlatformDB_log' TO N'D:\Game\Data\QPPlatformDB_log.ldf', --日志文件路径 NOUNLOAD, STATS = 10 GO restore DATABASE QPPlatformManagerDB from disk='D:\Game\Sql\QPPlatformManagerDB.bak' WITH FILE = 1, MOVE N'QPPlatformManagerDB' TO N'D:\Game\Data\QPPlatformManagerDB.mdf', --数据文件路径 MOVE N'QPPlatformManagerDB_log' TO N'D:\Game\Data\QPPlatformManagerDB_log.ldf', --日志文件路径 NOUNLOAD, STATS = 10 GO restore DATABASE QPRecordDB from disk='D:\Game\Sql\QPRecordDB.bak' WITH FILE = 1, MOVE N'QPRecordDB' TO N'D:\Game\Data\QPRecordDB.mdf', --数据文件路径 MOVE N'QPRecordDB_log' TO N'D:\Game\Data\QPRecordDB_log.ldf', --日志文件路径 NOUNLOAD, STATS = 10 GO restore DATABASE QPTreasureDB from disk='D:\Game\Sql\QPTreasureDB.bak' WITH FILE = 1, MOVE N'QPTreasureDB' TO N'D:\Game\Data\QPTreasureDB.mdf', --数据文件路径 MOVE N'QPTreasureDB_log' TO N'D:\Game\Data\QPTreasureDB_log.ldf', --日志文件路径 NOUNLOAD, STATS = 10 GO
6、清理数据库日志
--清理QPAccountsDB数据库日志 use QPAccountsDB ALTER DATABASE QPAccountsDB SET RECOVERY SIMPLE GO -- Shrink the truncated log file to 1 MB. DBCC SHRINKFILE (QPAccountsDB_log, 1) GO -- Reset the database recovery model. --清理QPAgentsDB数据库日志. use QPAgentsDB ALTER DATABASE QPAgentsDB SET RECOVERY SIMPLE GO -- Shrink the truncated log file to 1 MB. DBCC SHRINKFILE (QPAgentsDB_log, 1) GO -- Reset the database recovery model. ALTER DATABASE QPAgentsDB SET RECOVERY FULL GO --清理QPGameMatchDB数据库日志. use QPGameMatchDB ALTER DATABASE QPGameMatchDB SET RECOVERY SIMPLE GO -- Shrink the truncated log file to 1 MB. DBCC SHRINKFILE (QPGameMatchDB_log, 1) GO -- Reset the database recovery model. ALTER DATABASE QPGameMatchDB SET RECOVERY FULL GO --清理QPGameScoreDB数据库日志. use QPGameScoreDB ALTER DATABASE QPGameScoreDB SET RECOVERY SIMPLE GO-- Shrink the truncated log file to 1 MB. DBCC SHRINKFILE (QPGameScoreDB_log, 1) GO -- Reset the database recovery model. ALTER DATABASE QPGameScoreDB SET RECOVERY FULL GO --清理QPGameSportsDB数据库日志. use QPGameSportsDB ALTER DATABASE QPGameSportsDB SET RECOVERY SIMPLE GO -- Shrink the truncated log file to 1 MB. DBCC SHRINKFILE (QPGameSportsDB_log, 1) GO -- Reset the database recovery model. ALTER DATABASE QPGameSportsDB SET RECOVERY FULL GO --清理QPNativeWebDB数据库日志. use QPNativeWebDB ALTER DATABASE QPNativeWebDB SET RECOVERY SIMPLE GO -- Shrink the truncated log file to 1 MB. DBCC SHRINKFILE (QP_News_log, 1) GO -- Reset the database recovery model. ALTER DATABASE QPNativeWebDB SET RECOVERY FULL GO --清理QPPlatformDB数据库日志. use QPPlatformDB ALTER DATABASE QPPlatformDB SET RECOVERY SIMPLE GO -- Shrink the truncated log file to 1 MB. DBCC SHRINKFILE (QPPlatformDB_log, 1) GO -- Reset the database recovery model. ALTER DATABASE QPPlatformDB SET RECOVERY FULL GO--清理QPPlatformManagerDB数据库日志. use QPPlatformManagerDB ALTER DATABASE QPPlatformManagerDB SET RECOVERY SIMPLE GO-- Shrink the truncated log file to 1 MB. DBCC SHRINKFILE (QPPlatformManagerDB_log, 1) GO -- Reset the database recovery model. ALTER DATABASE QPPlatformManagerDB SET RECOVERY FULL GO --清理QPTreasureDB数据库日志. use QPTreasureDB ALTER DATABASE QPTreasureDB SET RECOVERY SIMPLE GO -- Shrink the truncated log file to 1 MB. DBCC SHRINKFILE (QPTreasureDB_log, 1) GO -- Reset the database recovery model. ALTER DATABASE QPTreasureDB SET RECOVERY FULL GO select * from sys.database_files
END!!!
- 1、新建数据库脚本
- 2、修改数据库表名
- 3、执行脚本
- 1、备份数据库
- 2、恢复数据库备份
- 3、分离数据库
- 4、附加数据库
- 5、还原数据库
- 6、清理数据库日志
发表评论