How to add checksum in the log backup script? - sql-server

I have a mssql script which I want to use for daily full and log backups, I am not able to understand how to add CHECKSUM value to get this to work, can anyone help me around this:
DECLARE #name VARCHAR(50) -- database name
DECLARE #path VARCHAR(256) -- path for backup files
DECLARE #fileName VARCHAR(256) -- filename for backup
DECLARE #fileDate VARCHAR(20) -- used for file name
SET #path = 'C:\Backup\'
SELECT #fileDate = CONVERT(VARCHAR(20),GETDATE(),112)
+ '_'
+ REPLACE(CONVERT(VARCHAR(20),GETDATE(),108),':','')
DECLARE db_cursor CURSOR FOR
SELECT name
FROM master.dbo.sysdatabases
WHERE name NOT IN ('master','model','msdb','tempdb')
AND DATABASEPROPERTYEX(name, 'Recovery') IN ('FULL','BULK_LOGGED')
OPEN db_cursor
FETCH NEXT FROM db_cursor INTO #name
WHILE ##FETCH_STATUS = 0
BEGIN
SET #fileName = #path + #name + '_' + #fileDate + '.TRN'
BACKUP LOG #name TO DISK = #fileName
FETCH NEXT FROM db_cursor INTO #name
END
CLOSE db_cursor
DEALLOCATE db_cursor

Your command should look like this:
BACKUP LOG #name TO DISK = #fileName with checksum
You can find this syntax here Enable or Disable Backup Checksums During Backup or Restore (SQL Server) and here BACKUP (Transact-SQL) under
Error Management Options

I think what you're really asking for is a RESTORE VERIFYONLY which will check the state of the backup file that was created.
RESTORE VERIFYONLY FROM DISK = #fileName;

Related

MSSQL Script to export entire database

It's possible create a script (to launch from a stored procedure) that export the entire database (schema + data) into a .sql file?
I need this to make a backup from a MSSQL 2019 and I need to restore to MSSQL 2017... Using Tasks->Generate Scripts from SSMS it's possible do what I need, but I wish schedule this and the best way I think is made a script to launch from a stored procedure.
Thanks in advance!
Here is the script that will allow you to backup each database within your instance of SQL Server
DECLARE #name NVARCHAR(256) -- database name
DECLARE #path NVARCHAR(512) -- path for backup files
DECLARE #fileName NVARCHAR(512) -- filename for backup
DECLARE #fileDate NVARCHAR(40) -- used for file name
-- specify database backup directory
SET #path = 'C:\test\'
-- specify filename format
SELECT #fileDate = CONVERT(NVARCHAR(20),GETDATE(),112)
DECLARE db_cursor CURSOR READ_ONLY FOR
SELECT name
FROM master.sys.databases
WHERE name = #name
AND state = 0 -- database is online
AND is_in_standby = 0 -- database is not read only for log shipping
OPEN db_cursor
FETCH NEXT FROM db_cursor INTO #name
WHILE ##FETCH_STATUS = 0
BEGIN
SET #fileName = #path + #name + '_' + #fileDate + '.sql'
BACKUP DATABASE #name TO DISK = #fileName
FETCH NEXT FROM db_cursor INTO #name
END
CLOSE db_cursor
DEALLOCATE db_cursor
You have to change the #path to the appropriate backup directory.
NOTE: Make sure that you have read and write access to the DB

How to take all the database backup in SQL Server?

I have written a SQL cursor to take back up:
DECLARE #databaseName NVARCHAR(50), #backupFilePath NVARCHAR(50),#backupFileName
NVARCHAR(50),#dateOfFile NVARCHAR(20)
SET #backupFilePath = 'D:\Backup'
SELECT #dateOfFile = CONVERT(NVARCHAR(20),GETDATE(),112) +
REPLACE(CONVERT(NVARCHAR(20),GETDATE(),108),':','') --20170416165910
DECLARE cursorForBackup CURSOR FOR (SELECT 'Permanent_Employee')
OPEN cursorForBackup
FETCH NEXT FROM cursorForBackup INTO #databaseName
WHILE ##FETCH_STATUS = 0
BEGIN
SET #backupFileName = #backupFilePath + #databaseName + '_' + #dateOfFile + '.BAK'
BACKUP DATABASE #databaseName TO DISK = #backupFileName
FETCH NEXT FROM cursorForBackup INTO #databaseName
END
CLOSE cursorForBackup
DEALLOCATE cursorForBackup
SQL Server is connected to dev environment with valid IP and password
Here, I have specified the backupFilePath.
Though the query is running successfully but no backup file is getting generated in the path.
Can anyone help me if I am missing something
Success message:
Processed 6256 pages for database 'YumThailand_Dev', file 'YumThailand_Dev' on file 1.
Processed 2 pages for database 'YumThailand_Dev', file 'YumThailand_Dev_log' on file 1.
BACKUP DATABASE successfully processed 6258 pages in 1.129 seconds (43.304 MB/sec).
If anyone knows a better solution please suggest.

How to backup all databases on SQL Server 2008

I have been working on SQL Server 2008 R2 for 4 years and it's time to format my laptop.
I just use the default instance, which I can access using the . as server name, and then my username and password for user authentication.
Now I want to format my laptop, and it is almost impossible to backup manually all the database.
I found in the following path
C:\Program Files\Microsoft SQL Server\MSSQL10_50.SQLEXPRESS\MSSQL
all the databases that I have, for each database I found:
databasename_log.ldf
databasename.mdf
I copied these files to an external hard drive.
My question:
Are those files enough to import the database after formatting? Will they work if I installed (after formatting) SQL Server 2012 not 2008 R2?
I found the SQL from this article useful in taking backups of all databases on a server.
DECLARE #name VARCHAR(50) -- database name
DECLARE #path VARCHAR(256) -- path for backup files
DECLARE #fileName VARCHAR(256) -- filename for backup
DECLARE #fileDate VARCHAR(20) -- used for file name
-- specify database backup directory
SET #path = 'C:\Backup\'
-- specify filename format
SELECT #fileDate = CONVERT(VARCHAR(20),GETDATE(),112)
DECLARE db_cursor CURSOR FOR
SELECT name
FROM master.dbo.sysdatabases
WHERE name NOT IN ('master','model','msdb','tempdb') -- exclude these databases
OPEN db_cursor
FETCH NEXT FROM db_cursor INTO #name
WHILE ##FETCH_STATUS = 0
BEGIN
SET #fileName = #path + #name + '_' + #fileDate + '.BAK'
BACKUP DATABASE #name TO DISK = #fileName
FETCH NEXT FROM db_cursor INTO #name
END
CLOSE db_cursor
DEALLOCATE db_cursor
You can use SELECT Statement or CURSOR like this:
DECLARE #PathForBackUp VARCHAR(255)
SET #PathForBackUp = 'F:\Backup\User DB\'
SELECT 'BACKUP DATABASE [' + name + '] TO DISK = N''' + #PathForBackUp + '' + name + '.bak''
WITH NOFORMAT, NOINIT, NAME = N''' + name + '_FullBackUp'', SKIP, NOREWIND, NOUNLOAD, COMPRESSION, STATS = 5'
FROM sys.databases
WHERE database_id > 4
OR
DECLARE #DBName VARCHAR(255)
DECLARE #PathForBackUp VARCHAR(255)
DECLARE #FileName VARCHAR(255)
DECLARE #DateFile VARCHAR(255)
DECLARE #SQL NVARCHAR(2048)
SET #PathForBackUp = 'F:\Backup\User DB\'
SET #DateFile = REPLACE(REPLACE(CONVERT(VARCHAR(20),GETDATE(),120) ,' ','T'), ':','')
DECLARE BACKUPING CURSOR FOR
SELECT name
FROM master.dbo.sysdatabases WHERE dbid > 4
OPEN BACKUPING
FETCH NEXT FROM BACKUPING INTO #DBName
WHILE ##FETCH_STATUS = 0
BEGIN
SET #FileName = #PathForBackUp + #DBName + '_' + #DateFile + '.BAK'
SET #SQL = 'BACKUP DATABASE '+#DBName+ ' TO DISK = '''+#FileName+''' WITH COMPRESSION '
PRINT #SQL
EXECUTE sp_executesql #sql
FETCH NEXT FROM BACKUPING INTO #DBName
END
CLOSE BACKUPING
DEALLOCATE BACKUPING
If you want to more about these solution I wrote post about that here:
http://www.pigeonsql.com/single-post/2016/12/20/Backup-All-Users-databases-via-Select-and-Cursor
Are those files enough to import the database after formatting? Will they work if I installed (after formatting) SQL Server 2012 not 2008 R2?
You are taking file level backup of database mdf and ldf file this is bit different from T-SQL backup. As I can read you have already formatted your machine and you have not taken backup so in that case please copy the mdf and ldf file on the local drive and then attach those files to create the database.
Please note before attaching make sure you have installed Service pack(SP), if it was installed on SQL Server 2008 r2 before formatting. If not please install SP after attaching database.
To attach database you can use both TSQL and GUI. Below link would help you in attaching the data files. Before opening SSMS please right click on SSMS and select run as administrator to avoid any access denied message.
Attach a database using SSMS
Sample script for attach
CREATE DATABASE DatabaseName
ON (FILENAME = 'FilePath\FileName.mdf'), -- Main Data File .mdf
(FILENAME = 'FilePath\LogFileName.ldf'), -- Log file .ldf
(FILENAME = 'FilePath\SecondaryDataFile.ndf) -- Optional - any secondary data files
FOR ATTACH
GO

SqlServer Maintenance plan log file

Hi all I have a SQLSERVER maintanence plan that run every night, for some reason it's stopped running, when I run it manually it throws an error and states "Execution failed. See the maintenance plan and sql server agent job history for log details" Ive looked in program files/.../log and there is very little information in there
my procedure is as follows
DECLARE #name VARCHAR(50) -- database name
DECLARE #path VARCHAR(256) -- path for backup files
DECLARE #fileName VARCHAR(256) -- filename for backup
DECLARE #fileDate VARCHAR(20) -- used for file name
SET #path = 'D:\backups\'
SELECT #fileDate = CONVERT(VARCHAR(20),GETDATE(),112)
DECLARE db_cursor CURSOR FOR
SELECT name
FROM master.dbo.sysdatabases
WHERE name NOT IN ('master','model','msdb','tempdb','emhf')
OPEN db_cursor
FETCH NEXT FROM db_cursor INTO #name
WHILE ##FETCH_STATUS = 0
BEGIN
SET #fileName = #path + #name +'.BAK'
BACKUP DATABASE #name TO DISK = #fileName
is there a way to write what its doing at each and every stage to a file so I can then find out why its falling over
Not sure if this could be such a simple mistake, but you have a missing END
If you right click on the maintenance plan, and select view history, you will find more accurate error descriptions.

Saving image in database field to file on server

I am trying to save data from a table directly to the servers file system using this t-sql:
DECLARE #ID int
DECLARE #command varchar(1000)
DECLARE #Name varchar(100)
DECLARE #FileName varchar(100)
DECLARE MyCursor CURSOR FOR
SELECT Id, [FileName] FROM FilesTable
OPEN MyCursor
FETCH NEXT FROM MyCursor
INTO #ID, #Name
WHILE ##FETCH_STATUS = 0
BEGIN
SELECT #FileName = 'd:\Temp\' + convert(varchar(200), #Name)
SET #command = 'bcp "SELECT FileBytes FROM FilesTable WHERE ID = ' + convert(varchar(18), #Id) + '" queryout "' + #FileName + '" -T -n'
EXEC master..xp_cmdshell #command, no_output
FETCH NEXT FROM MyCursor
INTO #Id, #Name
END
CLOSE MyCursor
DEALLOCATE MyCursor
The table is filled with solid data. accessing this data with a C# program works.
I did configure advanced options gave permission to use xp_cmdshell
When I run this query i don't get any error but no file was written.
I also don't see anything in the logs, (maybe I am looking in the wrong location?)
What am I doing wrong? Or what can I do to checdk 'under the hood'?
Based on the comment from Patrick and the answer in fastest way to export blobs from table into individual files I was able to store the images on my fileserver.
we ended up writing a CLR function something like the one given.

Resources