backup database task in SSIS - sql-server

I am new to SSIS ,and I am trying to make a package that automatically creates a backup of the Prod server and restores it on Dev server. For that I am using a backup database task in control flow . I am trying to store the name of the backup file that is created into a variable and then pass the same to the restore task . But I am not able to understand the format in which this filename is created . It is like
\Configurations_backup_2015_07_29_133104_1546397.bak'.
Can anyone help me with how to fetch this name and pass it directly to restore task.
Thanks

Try executing below query to get the backup name created by the task. The same can be stored into a variable and passed on for restore.
select top 1
bs.database_name,
bmf.physical_device_name
from
msdb.dbo.backupset as bs
inner join
msdb.dbo.backupmediafamily as bmf
on bmf.media_set_id = bs.media_set_id
where
bs.database_name = 'master' --mention your database name
and bs.type = 'D'
order by
bs.backup_start_date desc

Related

Find DB Restore History along with machine hostname/ip address

I want to find out from which laptop or desktop a given database was restored during a given time period. Currently every one in my team is using the same SQL Server user id, so I have to find from which machine the database restore was triggered.
That way I can figure out who actually did the database restore.
I have already found the below query which gives the details of restore history. But this doesn't give the ipaddress or hostname of the machine from which restore was done.
SELECT
[rs].[destination_database_name],
[rs].[restore_date],
[bs].[backup_start_date],
[bs].[backup_finish_date],
[bs].[database_name] as [source_database_name],
[bmf].[physical_device_name] as [backup_file_used_for_restore]
FROM msdb..restorehistory rs
INNER JOIN msdb..backupset bs ON [rs].[backup_set_id] = [bs].
[backup_set_id]
INNER JOIN msdb..backupmediafamily bmf ON [bs].[media_set_id] = [bmf].
[media_set_id]
ORDER BY [rs].[restore_date] DESC
After you get the restore datetime from the query you put, you can go to Application event viewer and check the computer name
Example:

SQL Server 2012 - Finding out which transaction logs have been applied to a NORECOVER db

I have a copy of an offsite production database used for reporting which is running on SQL Server 2012. I want to start updating it hourly with transaction logs from that offsite production database.
No big deal, restore a full backup (w/ NORECOVERY) to get things started and apply the transaction logs (w/ NORECOVERY) as they come in.
However, in the event of a problem with the restore (or with getting the log files) I could end up with several transaction log files, some of which have been applied and others that have not. When that happens, how do I figure out which file to start with in my TSQL script?
I tried looking in the restore history table like this:
select distinct
h.destination_database_name,
h.restore_date,
s.server_name,
m.physical_device_name as backup_device,
f.physical_name
from
msdb..restorehistory h
inner join
msdb..backupfile f on h.backup_set_id = f.backup_set_id
inner join
msdb..backupset s on f.backup_set_id = s.backup_set_id
inner join
msdb..backupmediafamily m on s.media_set_id = m.media_set_id
where
h.destination_database_name = 'mydb'
and h.restore_date > (GETDATE() -0.5)
order by
h.restore_date
But checking restorehistory is no good because the NORECOVERY flag means no records have been added in that table. So is there another way to check this, via T-SQL, that works for a NORECOVERY database?
Assuming this is a rare manual operation, the simplest way to is scan the errorlog.
SQL Server's built-in log shipping (and some third party implementations) have tables, views and user interfaces that make this simpler.

Executing SSRS Reports From SSIS

I need to execute a SSRS reports from SSIS on periodic schedule.
Saw a solution here :
https://www.mssqltips.com/sqlservertip/3475/execute-a-sql-server-reporting-services-report-from-integration-services-package/
But is there any other option in SSIS without using Script Task ? I don't quite understand the script and concern there could be some support issue for me.
Database : SQL Server 2008R2 Standard Edition
Any ideas ? Thanks very much ...
SSIS controlling the running of an SSRS in SQL Agent.
This assumes that the SSIS job will have updated a control record or written some other identifiable record to a database.
1. Create a subscription for the report.
2. Run this SQL to get the GUID of the report
SELECT c.Name AS ReportName
, rs.ScheduleID AS JOB_NAME
, s.[Description]
, s.LastStatus
, s.LastRunTime
FROM
ReportServer..[Catalog] c
JOIN ReportServer..Subscriptions s ON c.ItemID = s.Report_OID
JOIN ReportServer..ReportSchedule rs ON c.ItemID = rs.ReportID
AND rs.SubscriptionID = s.SubscriptionID<br>
3. Create a SQL Agent job.
a. Step 1. A SQL statement to look for data in a table containing a flagged record where the Advanced setting is "on failure end job reporting success"
IF NOT exists ( select top 1 * from mytable where mykey = 'x'
and mycondition = 'y') RAISERROR ('No Records Found',16,1)
b. Step 2
USE msdb
EXEC sp_start_job #job_name = ‘1X2C91X5-8B86-4CDA-9G1B-112C4F6E450A'<br>
Replacing the GUID with the one returned from your GUID query.
One thing to note though ... once the report subscription has been executed then as far as SQL Agent is concerned then that step is complete, even though the report has not necessarily finished running. I once had a clean up job after the Exec step which effectively deleted some of my data before the report reached it!
You can create a subscription for the report that is never scheduled to run.
If you have the Subscription ID, you can fire the report subscription using a simple SQL Task in SSIS.
You can get the Subscription ID from the Report Server database. It is in the Subscriptions table.
Use this query to help locate the subscription:
SELECT Catalog.Path
,Catalog.Name
,SubscriptionID
,Subscriptions.Description
FROM Catalog
INNER JOIN Subscriptions
ON Catalog.ItemID = Subscriptions.Report_OID
In SSIS, you can use this statement, inside of a SQL Task, to fire the subscription:
EXEC reportserver.dbo.AddEvent #EventType='TimedSubscription',#EventData= [Your Subscription ID]
Hope this helps.

Move SQL Database Files MDF & LDF to New Location

Environment:
SQL Server 2008 R2
Database not receiving active transactions.
In a production environment I need to move the MDF & LDF files to new drives. Since I have a Window of time to stop active transactions, I thought I could just take a backup of the database and then restore it while configuring the file groups to new location.
I figured this is much better than detaching and reattaching the database with new file name.
Since I am a novice, wanted to check with the experts here. Any suggestion/advise much appreciated.
Here's a simple example. It assumes your database has a single .mdf (data) file and a single .ldf (log) file. I will use the [model] database as an example.
--First, make note of the current location of the db files.
--Copy and paste the physical_names somewhere. Trust me, if you forget
--where the files were originally, this will save you some heartache.
SELECT d.name, f.name, f.physical_name
FROM master.sys.master_files f
JOIN master.sys.databases d
ON d.database_id = f.database_id
WHERE d.name = 'model' --Replace with the name of your db.
--Now set the new file paths.
--You can run the ALTER DATABASE statements while the db is online.
--Run once for the mdf/data file.
ALTER DATABASE [model] --Replace with the name of your db.
MODIFY FILE
(
NAME = 'modeldev', --this is the "logical" file name.
FILENAME = 'D:\SqlData\model.mdf' --Replace with the new path\filename.
)
--Run once for the ldf/data file.
ALTER DATABASE [model] --Replace with the name of your db.
MODIFY FILE
(
NAME = 'modellog',
FILENAME = 'D:\SqlData\modellog.ldf' --Replace with the new path\filename.
)
--When business rules allow, take the db OFFLINE.
ALTER DATABASE [model] --Replace with the name of your db.
SET OFFLINE
--Move the physical db files to the new location on disk.
--Bring the db back ONLINE to complete the task.
ALTER DATABASE [model] --Replace with the name of your db.
SET ONLINE

Where is database .bak file saved from SQL Server Management Studio?

I was trying to create a backup for my SQL Server Database using SQL Server Management Studio 2008 Express. I have created the backup but it is getting saved at some path which I am not able to find. I am saving it on my local HD and I checked in Program Files>Microsoft SQL Server>MSSQL 1.0>MSSQL>DATA> but its not there.
What's the default save path for this DB .bak?
Should be in
Program Files>Microsoft SQL Server>MSSQL 1.0>MSSQL>BACKUP>
In my case it is
C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\Backup
If you use the gui or T-SQL you can specify where you want it
T-SQL example
BACKUP DATABASE [YourDB] TO DISK = N'SomePath\YourDB.bak'
WITH NOFORMAT, NOINIT, NAME = N'YourDB Full Database Backup',
SKIP, NOREWIND, NOUNLOAD, STATS = 10
GO
With T-SQL you can also get the location of the backup, see here Getting the physical device name and backup time for a SQL Server database
SELECT physical_device_name,
backup_start_date,
backup_finish_date,
backup_size/1024.0 AS BackupSizeKB
FROM msdb.dbo.backupset b
JOIN msdb.dbo.backupmediafamily m ON b.media_set_id = m.media_set_id
WHERE database_name = 'YourDB'
ORDER BY backup_finish_date DESC
If the backup wasn't created in the default location, you can use this T-SQL (run this in SSMS) to find the file path for the most recent backup for all DBs on your SQL Server instance:
SELECT DatabaseName = x.database_name,
LastBackupFileName = x.physical_device_name,
LastBackupDatetime = x.backup_start_date
FROM ( SELECT bs.database_name,
bs.backup_start_date,
bmf.physical_device_name,
Ordinal = ROW_NUMBER() OVER( PARTITION BY bs.database_name ORDER BY bs.backup_start_date DESC )
FROM msdb.dbo.backupmediafamily bmf
JOIN msdb.dbo.backupmediaset bms ON bmf.media_set_id = bms.media_set_id
JOIN msdb.dbo.backupset bs ON bms.media_set_id = bs.media_set_id
WHERE bs.[type] = 'D'
AND bs.is_copy_only = 0 ) x
WHERE x.Ordinal = 1
ORDER BY DatabaseName;
As said by Faiyaz, to get default backup location for the instance, you cannot get it into msdb, but you have to look into Registry. You can get it in T-SQL in using xp_instance_regread stored procedure like this:
EXEC master.dbo.xp_instance_regread
N'HKEY_LOCAL_MACHINE', N'SOFTWARE\Microsoft\\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQLServer',N'BackupDirectory'
The double backslash (\\) is because the spaces into that key name part (Microsoft SQL Server).
The "MSSQL12.MSSQLSERVER" part is for default instance name for SQL 2014. You have to adapt to put your own instance name (look into Registry).
Set registry item for your server instance. For example:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL.2\MSSQLServer\BackupDirectory
Use the script below, and switch the DatabaseName with then name of the database that you've backed up. On the column physical_device_name, you'll have the full path of your backed-up database:
select a.backup_set_id, a.server_name, a.database_name, a.name, a.user_name, a.position, a.software_major_version, a.backup_start_date, backup_finish_date, a.backup_size, a.recovery_model, b.physical_device_name
from msdb.dbo.backupset a join msdb.dbo.backupmediafamily b
on a.media_set_id = b.media_set_id
where a.database_name = 'DatabaseName'
order by a.backup_finish_date desc
I dont think default backup location is stored within the SQL server itself.
The settings are stored in Registry. Look for "BackupDirectory" key and you'll find the default backup.
The "msdb.dbo.backupset" table consists of list of backups taken, if no backup is taken for a database, it won't show you anything
...\Program Files\Microsoft SQL Server\MSSQL 1.0\MSSQL\Backup
have you tried:
C:\Program Files\Microsoft SQL Server\MSSQL10.SQL2008\MSSQL\Backup
Script to get all backups in the last week can be found at:
http://wraithnath.blogspot.com/2010/12/how-to-find-all-database-backups-in.html
I have plenty more backup SQL scripts there also at
http://wraithnath.blogspot.com/search/label/SQL
So eventually on SQL Server 2019 you can just call built in SERVERPROPERTY function to query the default backup folder:
select serverproperty('InstanceDefaultBackupPath')
You may want to take a look here, this tool saves a BAK file from a remote SQL Server to your local harddrive: FIDA BAK to local

Resources