Microsoft Dot Net Master

Microsoft Dot Net Master
Microsoft Dot Net Master

Saturday, April 30, 2011

How To Perform Scheduled Backups For SQL Server 2005 Express

SQL Server 2005 Express edition is a free, lightweight and embeddable version of SQL Server 2005 which includes SQL Server Management Studio Express for users to easily manage that databases. Although SQL Server 2005 Express edition supports backup and restore database but it does not supports scheduling backups.


 Below are the simple steps to perform in order to enable scheduling backups for SQL Server 2005 Express:

   1. Create a store procedure that allows generate the dynamic backup file name, with types of backup to run such as full, differential or transaction log backups and location of the Backup files.

      USE [master]

      CREATE PROCEDURE [dbo].[sp_BackupDatabase]
      @databaseName sysname, @backupType CHAR(1)
      AS
      BEGIN
      SET NOCOUNT ON;

      DECLARE @sqlCommand NVARCHAR(1000)
      DECLARE @dateTime NVARCHAR(20)

      SELECT @dateTime = REPLACE(CONVERT(VARCHAR, GETDATE(),111),'/','') +
      REPLACE(CONVERT(VARCHAR, GETDATE(),108),':','')

      IF @backupType = 'F'
      SET @sqlCommand = 'BACKUP DATABASE ' + @databaseName +
      ' TO DISK = ''C:\Backup\' + @databaseName + '_Full_' + @dateTime + '.BAK'''

      IF @backupType = 'D'
      SET @sqlCommand = 'BACKUP DATABASE ' + @databaseName +
      ' TO DISK = ''C:\Backup\' + @databaseName + '_Diff_' + @dateTime + '.BAK'' WITH DIFFERENTIAL'

      IF @backupType = 'L'
      SET @sqlCommand = 'BACKUP LOG ' + @databaseName +
      ' TO DISK = ''C:\Backup\' + @databaseName + '_Log_' + @dateTime + '.TRN'''

      EXECUTE sp_executesql @sqlCommand
      END

   2. Create a SQL script to run the backup. In this example, we will backup database master and saved the below SQL script as dbbackup.sql and save in “c:\Backup” folder.

      sp_BackupDatabase 'master', 'F'
      GO
      QUIT
   3. Create a scheduled task in Windows which can be found in Control Panel or Accessories -> System Tools -> Scheduled Tasks or Task Scheduler.





 Click on Add Scheduled Task or Create Task. Scheduling wizard will be displayed. Click Next, then click the Browse button to find SQLCMD.EXE from “C:\Program Files\Microsoft SQL Server\90\Tools\Binn”.






In Task Scheduler, define the above in Action tab.
Specify when to perform the task as well as the user name and password to run the operation. Once finished, give the scheduled task a name and save the task.
Click on the “Open advanced properties” to edit the command.







 Type the following command in Run:

 sqlcmd  -S prog3 -E -Q "EXECUTE sp_BackupDatabase 'master', 'F'"



      The meaning of the command:
          * sqlcmd
          * -S (this specifies the server\instance name for SQL Server)
          * serverName (this is the server\instance name for SQL Server)
          * -E (this allows you to make a trusted connection)
          * -i (this specifies the input command file)

If you want to test the task which has been created then you can go back to the Scheduled Tasks or Task Scheduler, right click on the task and select “Run”.
















Monday, April 25, 2011

Monday, April 18, 2011

What is Delay signing ?


During development process you will need strong name keys to be exposed to developer which is not a good practice from security aspect point of view.In such situations you can assign the key later on and during development you an use delay signing
Following is process to delay sign an assembly:
√ First obtain your string name keys using SN.EXE.
√ Annotate the source code for the assembly with two custom attributes from System.Reflection: ssemblyKeyFileAttribute, which passes the name of the file containing the public key as a parameter to its constructor. AssemblyDelaySignAttribute, which indicates that delay signing, is being used by passing true as a parameter to its constructor.
 For example as shown below:
[Visual Basic]
<Assembly:AssemblyKeyFileAttribute("myKey.snk")>
<Assembly:AssemblyDelaySignAttribute(true)>
[C#]
[assembly:AssemblyKeyFileAttribute("myKey.snk")]
[assembly:AssemblyDelaySignAttribute(true)]
The compiler inserts the public key into the assembly manifest and reserves space in the PE file for the full  strong name signature. The real public key must be stored while the assembly is built so that other assemblies that reference this assembly can obtain the key to store in their own assembly reference.
√ Because the assembly does not have a valid strong name signature, the verification of that signature must be turned off. You can do this by using the –Vr option with the Strong Name tool.The following example turns off verification for an assembly called myAssembly.dll.
Sn –Vr myAssembly.dll

√ Just before shipping, you submit the assembly to your organization's signing authority for the actual strong name signing using the –R option with the Strong Name tool. The following example signs an assembly called myAssembly.dll with a strong name using the sgKey.snk key pair.
Sn -R myAssembly.dll sgKey.snk

How to add and remove an assembly from GAC?


There are two ways to install .NET assembly in GAC:-
√ Using Microsoft Installer Package. You can get download of installer from http://www.microsoft.com.
√ Using Gacutil. Goto “Visual Studio Command Prompt” and type “gacutil –i (assembly_name)”, where  assembly_name) is the DLL name of the project.

What is the concept of strong names ?


Twist :- How do we generate strong names or what is the process of generating strong names, What is use the of SN.EXE , How do we apply strong names to assembly, How do you sign an assembly? Strong name is similar to GUID(It is supposed to be unique in space and time) in COM components.Strong Name is only needed when we need to deploy assembly in GAC. Strong Names helps GAC to differentiate between two versions. Strong names use public key cryptography (PKC) to ensure that no one can spoof it.PKC use public key and private key concept.

What is GAC ?


Twist :- What are situations when you register .NET assembly in GAC ?
GAC (Global Assembly Cache) is used where shared .NET assembly reside. GAC is used in the following ituations :-
√ If the application has to be shared among several application.
√ If the assembly has some special security requirements like only administrators
can remove the assembly. If the assembly is private then a simple delete of
assembly the assembly file will remove the assembly.
Note :- Registering .NET assembly in GAC can lead to the old problem of DLL hell, where COM version was stored in central registry. So GAC should be used when absolutely necessary.

Is versioning applicable to private assemblies?


Versioning concept is only applicable to global assembly cache (GAC) as private assembly lie in their individual folders.