Lệnh backup toàn bộ database trong SQL Server
DECLARE @bk_path VARCHAR(256);
DECLARE @db_name VARCHAR(50);
DECLARE @bk_file_date VARCHAR(20);
DECLARE @bk_file_name VARCHAR(256);
DECLARE @i INT = 1;
SET @bk_path = ''C:\WORKSPACE\DATABASE\BACKUP\'';
SELECT @bk_file_date = FORMAT(GETDATE(), ''yyyyMMdd'');
DECLARE @db_names TABLE (
id INT IDENTITY(1,1) PRIMARY KEY,
db_name VARCHAR(50) NOT NULL );
INSERT INTO @db_names
SELECT name
FROM master.sys.databases
WHERE name NOT IN (''master'',''model'',''msdb'',''tempdb'')
AND state = 0
AND is_in_standby = 0;
WHILE EXISTS (SELECT 1 from @db_names WHERE Id = @i)
BEGIN
SELECT @db_name = db_name from @db_names WHERE Id = @i;
SET @bk_file_name = @bk_path + @db_name + ''_'' + @bk_file_date + ''.BAK'';
BACKUP DATABASE @db_name TO DISK = @bk_file_name WITH STATS;
SET @i = @i + 1;
END
GO