• Goltratec Blog

  • Nuevo virus de messenger que ofrece entrar en chatear.xxxxx.com

28th November 2009

Nuevo virus de messenger que ofrece entrar en chatear.xxxxx.com

Nuevo Virus de messenger

Un nuevo virus apenas controlado actualmente llega por MSN aparentando venir del contacto con el que se está chateando, si bien no es asi sino que es el propio virus que se envia para propagarse. Este virus ofrece entrar en http://chatear.xxxxxx.com.

De momento sabemos que reside en C:\windows\info.exe pero no es fácil acceder a dicho fichero, por lo que se ha de usar el ELIMOVER y marcar la casilla inferior izquierda para añadir .VIR al fichero original y asi no volver a cargarse tras el siguiente reinicio.

Probablemente corresponda a este fichero

C:\windows\info.exe
Tamaño 148.56 KB (152130 bytes)

posted in Redes, Web, windows | 0 Comments

18th November 2009

Videos sobre posicionamiento en la red

Aqui os dejo en enlace a una web donde mediante video tutoriales explican tecnicas para mejorar el posicionamiento en la red.

http://blog.posicionamientoconvideomarketing.com/

posted in Posicionamiento, Web | 0 Comments

9th September 2009

Telefónica duplicara la velocidad del ADSL ante de final de año

Así lo asegura el diario económico Expansión que indica que antes de fin de año, Telefónica duplicará la actual velocidad de conexión de todos sus clientes de ADSL, manteniendo el precio actual.

No dudamos que la información sea cierta. Primero porque Expansión suele estar “bien informado” en estos temas, y segundo y más importante, porque el gigante tiene que moverse ante la notoria pérdida de clientes a manos de los operadores alternativos, que están dando más a mejor precio.

Sin embargo, no se ha explicado en qué consistirá esa duplicación, que en el pasado se limitó a doblar la velocidad teórica de bajada, manteniendo los prehistóricos 320 Kbps durante cuatro largos años, un mundo en el sector tecnológico.

El paso (imprescindible) para aumentar la velocidad actual de 6 “megas” se realizaría gracias al empleo de tecnología VDSL2, lo que supondrá para Telefónica un gasto adicional al tener que cambiar los routers clientes.

Además, el diario económico anuncia que Telefónica pondría en el mercado antes de navidades, las primeras ofertas de 50 “megas”, aunque tampoco ha aclarado si existen condiciones técnicas para que todos los clientes puedan acceder, ni su precio de comercialización.

posted in General, Redes | 0 Comments

9th September 2009

Ya esta próximo el nuevo Windows 7

Pues lo dicho y como no podia ser de otra manera, Microsoft y su departamento de marketing se han puesto en marcha para que seamos incapaces de negarnos a hacer el cambio. Para deleite de todos os adjunto enlaces los videos promocionales.

posted in windows | 0 Comments

22nd August 2009

Instalar zoneminder en ubuntu

aun no lo he probado, pero me ha parecido interesante reflejarlo en el blog para todos aquellos que tengais curiosidad.

sudo apt-get install zoneminder

sudo ln -s /etc/zm/apache.conf /etc/apache2/conf.d/zoneminder.conf

sudo adduser www-data video

(I executed this first one, then lost track of this web page, spent hours trying to debug the black screen “shared memory not initialized by capture daemon” / “shared memory not valid” / “can’t get shared memory id” issues and finally found that I could fix them with “chmod 606 /dev/video0″… but turns out that your adduser fix is the more appropriate fix )

posted in General, PHP, Redes | 0 Comments

20th August 2009

Bloquear usb en windows

Para evitar que alguien use un pendrive, puedes ir a la carpeta oculta
Windows\inf, localizar el archivo “usbstor.pnf”, y clic derecho en
Propiedades y Seguridad. Ahí elegir el grupo de usuarios al que se le
denegará el acceso. Otros archivos que comienzan con “usb” corresponden a
diferentes tipos de dispositivos usb a los que se puede limitar el uso
modificando la Seguridad. En Windows XP Home Edition necesitas acceder en
Modo Seguro para ver la Pestaña Seguridad. Para iniciar en Modo Seguro
reinicia el equipo y pulsa la tecla F8 una vez por segundo o presiona la
tecla y sostenla, hasta que aparece la lista de opciones de Modo Seguro…
necesitas iniciar sesion con alguna cuenta administrativa.

Para deshabilitar el uso de USB mediante edicion del registro:
Inicio\Ejecutar\escribe: “regedit” (obviamente sin comillas) y Acepta.
Navega a la clave; HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services y
expande dicha clave, debajo encuentra la clave nombrada USBSTOR que
representa el controlador “usbstor.sys” que permite el uso de USB Storage o
Almacenamiento Masivo USB. (Otras claves corresponden a tipos diferentes de
dispositivos USB, para deshabilitar el controlador encuentra el valor
nombrado “Start” y edita los datos que actualmente debe ser 3 (por defecto)
con 0 (cero) para deshabilitarlo.

La cantidad de archivos en Windows\inf o valores en el registro
correspondientes a dispositivos USB depende de los dispositivos que se han
instalado ya que se crean durante la instalacion. La información para
identificar el tipo de dispositivos en el registro, deberá aparecer en los
datos del valor “Display Name” en el panel derecho de cada clave
correspondiente a tipo de dispositivo.

posted in windows | 0 Comments

29th July 2009

Como mejorar el rendimiento de Visual Studio 2005 - VB.net

Aqui os dejo este link. Acabo de aplicar lo que dice y aun no puedo asegurar que funcione correctamente.

http://code.msdn.microsoft.com/KB917452

posted in Programacion | 0 Comments

23rd July 2009

He perdido las direcciones de thunderbird

Bueno, como no podia ser de otra forma, hoy ha sido un dia movidito y hemos tenido un caso con uno de los clientes de correo interno. Concretamente thunderbird (mis alabanzas hacia mozilla) nos ha gastado una broma y ha hecho que al arrancarlo no tubiera una libreta de direcciones, concretamente la que usamos para enviar notificaciones a los clientes.

Pues nada, 5 min de google y resuelto. Existe un fichero llamado dentro del perfil de thunderbird (documents and settings\usuario\Datos de programa\thunderbird\profiles\nombreperfil) que se llama prefs.js. Al parecer, thunderbird (esto no se porque) lo ha renombrado a prefs1.js con lo que al iniciarlo te vuelve a pedir que lo configures. La solución ha sido tan sencilla como volver a renombrar prefs1.js por prefs.js.

posted in General, Linux, Redes, windows | 0 Comments

17th July 2009

Copia de Sql server 2000 por linea de comandos

Bueno, esta forma no es tan bonita como la del 2005 y permite menos cosas, pero es igual de efectiva.

En sql server2000 o en msde sqlcmd no existe, con lo que debemos usar osql. Y nada mas facil que en un fichero .bat establecer una linea como esta:

osql -p -E -S servidor -Q ” BACKUP DATABASE NomBD TO DISK = ‘C:\bd.bak’

Lo mas engorroso es crearnos un fichero por lotes y una tarea por cada una de las copias que queramos hacer.

Ale a disfrutarlo.

posted in Programacion, Sql Server, windows | 0 Comments

17th July 2009

Copia de Sql server 2005 por linea de comandos.

En el desarrollo de nuestro software usamos servidores sqlserver 2005 versión express, lo cual implica que no podemos (mediante utilidades gráficas) programar copias de seguridad. La solución pasa por instalar el sql management studio express que tambien nos instala las utilidades de lineas de comandos de sql express.

Buscando por la red he encontrado un procedimiento almacenado que guardaremos en nuestra bd master. Posteriormente crearemos un fichero sql que llamara a ese procedimiento con la configuración deseada y posteriormente crearemos un fichero por lotes que llamaremos desde una tarea programada de Windows.

Procedimientos almacenado (Descargalo como fichero)

use master
GO

-- set required options
exec sp_configure 'show advanced options',1
reconfigure
go
exec sp_configure 'xp_cmdshell',1
reconfigure
go
exec sp_configure 'Ole Automation Procedures',1
reconfigure
go

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[expressmaint]‘) and OBJECTPROPERTY(id, N’IsProcedure’) = 1)
drop procedure [dbo].[expressmaint]
GO

CREATE PROCEDURE [dbo].[expressmaint]
(
   @database      sysname,                   — database name | ALL_USER | ALL_SYSTEM
   @optype        varchar(7),                — LOG | DB | DIFF | REINDEX | REORG | CHECKDB
   @backupwith    varchar(500) = NULL,       — additional backup options
   @backupfldr    varchar(200) = NULL,       — folder to write backup to
   @reportfldr    varchar(200) = NULL,       — folder to write text report
   @verify        bit = 1,                   — verify backup
   @verifywith    varchar(500) = NULL,       — additional verify options
   @dbretainunit  varchar(10)  = NULL,       — minutes | hours | days | weeks | months | copies
   @dbretainval   int = 1,                   — specifies how many retainunits to keep backup
   @report        bit = 1,                   — flag to indicate whether to generate report
   @rptretainunit varchar(10)  = NULL,       — minutes | hours | days | weeks | months | copies
   @rptretainval  int = 1,                   — specifies how many retainunits to keep reports
   @checkattrib   bit = 0,                   — check if archive bit is cleared before deleting
   @delfirst      bit = 0,                   — delete before backup (handy if space issues)
   @debug         bit = 0,                   — print commands to be executed
   @compression   bit = 0                    — backup compression (SQL 2008 Enterprise Edition only)
)
AS
/*
   ExpressMaintTSQL

   see http://www.sqldbatips.com/showarticle.asp?ID=27 for documentation

   Date           Author                  Notes
   24/07/2004     Jasper Smith            Initial release
   28/07/2008     Jasper Smith            Fixed datepart issue with MONTHS retention unit
   06/12/2008     Jasper Smith            Added SQL 2008 support plus some minor fixes

*/
SET NOCOUNT ON
SET ARITHABORT ON
SET DATEFORMAT YMD

/************************
   VARIABLE DECLARATION
************************/

   DECLARE @fso             int
   DECLARE @file            int
   DECLARE @reportfilename  varchar(500)
   DECLARE @backupfilename  varchar(500)
   DECLARE @delfilename     varchar(500)
   DECLARE @cmd             varchar(650)
   DECLARE @backupfldrorig  varchar(200)
   DECLARE @databaseorig    sysname
   DECLARE @table           nvarchar(600)
   DECLARE @exists          varchar(5)
   DECLARE @err             int
   DECLARE @start           datetime
   DECLARE @finish          datetime
   DECLARE @runtime         datetime
   DECLARE @output          varchar(200)
   DECLARE @errormsg        varchar(210)
   DECLARE @datepart        nchar(2)
   DECLARE @execmd          nvarchar(1000)
   DECLARE @delcmd          nvarchar(1000)
   DECLARE @exemsg          varchar(8000)
   DECLARE @filecount       int              ; SET @filecount    = 0
   DECLARE @delcount        int              ; SET @delcount     = 0
   DECLARE @hr              int              ; SET @hr           = 0
   DECLARE @ret             int              ; SET @ret          = 0
   DECLARE @cmdret          int              ; SET @cmdret       = 0
   DECLARE @delbkflag       int              ; SET @delbkflag    = 0
   DECLARE @delrptflag      int              ; SET @delrptflag   = 0
   DECLARE @filecrt         int              ; SET @filecrt      = 0
   DECLARE @user            sysname          ; SET @user         = SUSER_SNAME()
   DECLARE @jobdt           datetime         ; SET @jobdt        = GETDATE()
   DECLARE @jobstart        char(12)         ;
   DECLARE @stage           int              ; SET @stage        = 1
   DECLARE @compressok      bit              ; SET @compressok   = 0
   DECLARE @versionmajor    int
   DECLARE @engineedition   int

   SET @jobstart = CONVERT(char(8),@jobdt,112)+LEFT(REPLACE(CONVERT(char(8),@jobdt,108),’:',”),4)
   IF RIGHT(@reportfldr,1)<>’\’ SET @reportfldr = @reportfldr + ‘\’
   IF RIGHT(@backupfldr,1)<>’\’ SET @backupfldr = @backupfldr + ‘\’
   SET @backupfldrorig = @backupfldr
   SET @databaseorig = @database

   SELECT @versionmajor = CAST(LEFT(CAST(SERVERPROPERTY(’ProductVersion’) as varchar(128)),CHARINDEX(’.',CAST(SERVERPROPERTY(’ProductVersion’) as varchar(128)))-1) as int),
          @engineedition = CAST(SERVERPROPERTY(’EngineEdition’) as int)
   IF (@versionmajor>=10 AND @engineedition = 3) SET @compressok = 1

   CREATE TABLE #files(filename varchar(255))
   CREATE TABLE #exists(exist int,isdir int,parent int)
   CREATE TABLE #databases(dbname sysname)

/**********************************
     INITIALIZE FSO IF @report = 1
***********************************/

   IF @report = 1
   BEGIN
      EXEC @hr=sp_OACreate ‘Scripting.FileSystemObject’,@fso OUT
      IF @hr <> 0
      BEGIN
         EXEC sp_OAGetErrorInfo @fso
         RAISERROR(’Error creating File System Object’,16,1)
         SET @ret = 1
      	GOTO CLEANUP
      END
   END

/************************
       CHECK INPUT
************************/

   — check SQL2005 or higher
   IF @versionmajor<9
	BEGIN
   	RAISERROR(’SQL2005 or higher is required for sp_expressmaint’,16,1)
      SET @ret = 1
   	GOTO CLEANUP
	END

   — check sysadmin
   IF IS_SRVROLEMEMBER(’sysadmin’) = 0
	BEGIN
   	RAISERROR(’The current user %s is not a member of the sysadmin role’,16,1,@user)
      SET @ret = 1
   	GOTO CLEANUP
	END

   — check database exists and is online
   IF @database NOT IN (’ALL_USER’,'ALL_SYSTEM’)
   BEGIN
      IF (DB_ID(@database) IS NULL) OR ((select state from sys.databases where name = @database) <>0)
   	BEGIN
      	RAISERROR(’Database %s is invalid or database status is not ONLINE’,16,1,@database)
         SET @ret = 1
      	GOTO CLEANUP
   	END
   END

   — check @optype is valid
   IF UPPER(@optype) NOT IN (’LOG’,'DB’,'DIFF’,'REINDEX’,'REORG’,'CHECKDB’)
	BEGIN
   	RAISERROR(’%s is not a valid option for @optype’,16,1,@optype)
      SET @ret = 1
   	GOTO CLEANUP
	END

   — check recovery mode is correct if trying log backup
   IF @database NOT IN (’ALL_USER’,'ALL_SYSTEM’)
   BEGIN
      IF (@optype = ‘LOG’ and ((select recovery_model from sys.databases where name = @database) = 3))
   	BEGIN
      	RAISERROR(’%s is not a valid option for database %s because it is in SIMPLE recovery mode’,16,1,@optype,@database)
         SET @ret = 1
      	GOTO CLEANUP
   	END
   END

   — no log backups for system databases
   IF @database = ‘ALL_SYSTEM’
   BEGIN
      IF @optype = ‘LOG’
   	BEGIN
      	RAISERROR(’%s is not a valid option for the option ALL_SYSTEM’,16,1,@optype)
         SET @ret = 1
      	GOTO CLEANUP
   	END
   END

   — check that @backupfldr exists on the server
   IF @optype NOT IN (’REINDEX’,'CHECKDB’,'REORG’)
   BEGIN
      IF @report = 1
      BEGIN
         EXEC sp_OAMethod @fso,’FolderExists’,@exists OUT,@backupfldr
         IF @exists <> ‘True’
      	BEGIN
         	RAISERROR(’The folder %s does not exist on this server’,16,1,@backupfldr)
            SET @ret = 1
         	GOTO CLEANUP
      	END
      END
      ELSE
      BEGIN
         INSERT #exists
         EXEC master.dbo.xp_fileexist @backupfldr
         IF (SELECT MAX(isdir) FROM #exists)<>1
      	BEGIN
         	RAISERROR(’The folder %s does not exist on this server’,16,1,@backupfldr)
            SET @ret = 1
         	GOTO CLEANUP
      	END
      END
   END

   — check that @reportfldr exists on the server
   IF @reportfldr IS NOT NULL or @report = 1
   BEGIN
      IF @report = 1
      BEGIN
         EXEC sp_OAMethod @fso,’FolderExists’,@exists OUT,@reportfldr
         IF @exists <> ‘True’
      	BEGIN
         	RAISERROR(’The folder %s does not exist on this server’,16,1,@reportfldr)
            SET @ret = 1
         	GOTO CLEANUP
      	END
      END
      ELSE
      BEGIN
         DELETE #exists
         INSERT #exists
         EXEC master.dbo.xp_fileexist @reportfldr
         IF (SELECT MAX(isdir) FROM #exists)<>1
      	BEGIN
         	RAISERROR(’The folder %s does not exist on this server’,16,1,@reportfldr)
            SET @ret = 1
         	GOTO CLEANUP
      	END
      END
   END

   — check @dbretainunit is a vaild value
   IF @optype NOT IN (’REINDEX’,'CHECKDB’,'REORG’)
   BEGIN
      IF UPPER(@dbretainunit) NOT IN (’MINUTES’,'HOURS’,'DAYS’,'WEEKS’,'MONTHS’,'COPIES’)
    	BEGIN
      	RAISERROR(’%s is not a valid value for @dbretainunit (”minutes | hours | days | weeks | months | copies”)’,16,1,@dbretainunit)
         SET @ret = 1
      	GOTO CLEANUP
   	END
   END

   –check @dbretainval is a vaild value
   IF @dbretainval<1
 	BEGIN
   	RAISERROR(’%i is not a valid value for @dbretainval (must be >0)’,16,1,@dbretainval)
      SET @ret = 1
   	GOTO CLEANUP
	END

   — check @rptretainunit is a vaild value if present
   IF UPPER(@rptretainunit) NOT IN (’MINUTES’,'HOURS’,'DAYS’,'WEEKS’,'MONTHS’,'COPIES’) and @rptretainunit IS NOT NULL
 	BEGIN
   	RAISERROR(’%s is not a valid value for @rptretainunit (”minutes | hours | days | weeks | months | copies”)’,16,1,@rptretainunit)
      SET @ret = 1
   	GOTO CLEANUP
	END

   –check @rptretainval is a vaild value
   IF @rptretainval<1
 	BEGIN
   	RAISERROR(’%i is not a valid value for @rptretainval (must be >0)’,16,1,@rptretainval)
      SET @ret = 1
   	GOTO CLEANUP
	END

/***********************************
   list of databases to process
************************************/

   IF @database IN (’ALL_USER’,'ALL_SYSTEM’)
   BEGIN
      IF @database = ‘ALL_USER’
         INSERT #databases(dbname)
         SELECT [name] from sys.databases where database_id > 4
         AND (@optype <> ‘LOG’ OR recovery_model <> ‘3′)
      ELSE
         INSERT #databases(dbname)
         SELECT [name] from sys.databases where database_id in (1,3,4)
   END
   ELSE
      INSERT #databases(dbname) SELECT @database

/***********************************
   INITIALIZE REPORT IF @report = 1
************************************/

   — generate report filename
   SELECT @reportfilename = @reportfldr + REPLACE(REPLACE(@database,’ ‘,’_'),””,’_') +
   CASE WHEN UPPER(@optype) = ‘DB’   THEN ‘_FullDBBackup_report_’
        WHEN UPPER(@optype) = ‘DIFF’ THEN ‘_DiffDBBackup_report_’
        WHEN UPPER(@optype) = ‘LOG’  THEN ‘_LogBackup_report_’
        WHEN UPPER(@optype) = ‘REINDEX’  THEN ‘_Reindex_report_’
        WHEN UPPER(@optype) = ‘REORG’  THEN ‘_Reorg_report_’
        WHEN UPPER(@optype) = ‘CHECKDB’  THEN ‘_CheckDB_report_’
   END + @jobstart + ‘.txt’

   — if no report just set @reportfilename to NULL
   IF @report = 0 SET @reportfilename = NULL

   IF @debug = 1
   BEGIN
      PRINT ‘@reportfilename = ‘ + ISNULL(@reportfilename,’NULL’)
   END

   IF @report = 1
   BEGIN
      — create report file
      EXEC @hr=sp_OAMethod @fso, ‘CreateTextFile’,@file OUT, @reportfilename
      IF (@hr <> 0)
      BEGIN
         EXEC sp_OAGetErrorInfo @fso
         RAISERROR(’Error creating log file’,16,1)
         SET @ret = 1
      	GOTO CLEANUP
      END
      ELSE
         — set global flag to indicate we have created a report file
         SET @filecrt = 1

      — write header
      EXEC sp_OAMethod @file,’WriteLine’,NULL,”
      SET @output = ‘Expressmaint utility, Logged on to SQL Server [' + @@SERVERNAME + '] as ‘ + ‘[' + @user + ']‘
      IF @debug = 1 PRINT @output
      EXEC sp_OAMethod @file,’WriteLine’,NULL,@output       

      IF UPPER(@optype) NOT IN (’REINDEX’,'CHECKDB’,'REORG’)
      BEGIN
         SET @output = ‘Starting backup on ‘ + convert(varchar(25),getdate(),100)
      END
      IF UPPER(@optype) = ‘CHECKDB’
      BEGIN
         SET @output = ‘Starting CheckDB on ‘ + convert(varchar(25),getdate(),100)
      END
      IF UPPER(@optype) IN (’REINDEX’,'REORG’)
      BEGIN
         SET @output = ‘Starting Reindex on ‘ + convert(varchar(25),getdate(),100)
      END 

      IF @debug = 1 PRINT @output
      EXEC sp_OAMethod @file,’WriteLine’,NULL,@output
      EXEC sp_OAMethod @file,’WriteLine’,NULL,”
   END

/************************
     BACKUP ACTIONS
************************/

   IF  UPPER(@optype) = ‘CHECKDB’ GOTO CHECK_DB
   IF  UPPER(@optype) IN (’REINDEX’,'REORG’) GOTO REINDEX

   — if @delfirst = 1  we need to delete prior backups that qualify
   IF @delfirst = 1 GOTO DELFIRST

   — this label is so that we can return here after deleting files if @delfirst = 1
   DOBACKUP:

   DECLARE dcur CURSOR LOCAL FAST_FORWARD
   FOR SELECT dbname FROM #databases ORDER BY dbname
   OPEN dcur
   FETCH NEXT FROM dcur into @database
   WHILE @@FETCH_STATUS=0
   BEGIN

      — set backup start time
      SET @start = GETDATE()

      — write to text report
      IF @report = 1
      BEGIN
         SET @output = ‘[' + CAST(@stage as varchar(10)) + '] Database ‘ + @database + ‘: ‘ +
                       CASE WHEN UPPER(@optype) = ‘DB’   THEN ‘Full Backup ‘
                            WHEN UPPER(@optype) = ‘DIFF’ THEN ‘Differential Backup ‘
                            WHEN UPPER(@optype) = ‘LOG’  THEN ‘Log Backup ‘
                       END + ’starting at ‘ + CONVERT(varchar(25),@start,100)
         IF @debug = 1 PRINT @output
         EXEC sp_OAMethod @file,’WriteLine’,NULL,@output
      END

      — backup subfolder
      SET @execmd = ‘IF NOT EXIST “‘ + @backupfldrorig + REPLACE(@database,””,”) + ‘\” MKDIR “‘ + @backupfldrorig + REPLACE(@database,””,”) + ‘\”‘
      EXEC master.dbo.xp_cmdshell @execmd,no_output
      SET @backupfldr = @backupfldrorig + REPLACE(@database,””,”) + ‘\’

      SELECT @backupfilename = @backupfldr + REPLACE(REPLACE(@database,’ ‘,’_'),””,’_') +
      CASE WHEN UPPER(@optype) = ‘DB’   THEN ‘_FullDBBackup_’
           WHEN UPPER(@optype) = ‘DIFF’ THEN ‘_DiffDBBackup_’
           WHEN UPPER(@optype) = ‘LOG’  THEN ‘_LogBackup_’
      END + @jobstart +
      CASE WHEN UPPER(@optype) = ‘LOG’ THEN ‘.TRN’ ELSE ‘.BAK’ END

      /************************
             FULL BACKUP
      ************************/

      IF UPPER(@optype) = ‘DB’
      BEGIN

         IF @compression=1 AND @compressok=1
            SET @execmd = N’BACKUP DATABASE [' + @database + '] TO DISK = ”’ + @backupfilename + ”” +
                           ‘ WITH COMPRESSION’ + CASE WHEN @backupwith IS NULL THEN ” ELSE (’,’ + @backupwith) END
         ELSE
            SET @execmd = N’BACKUP DATABASE [' + @database + '] TO DISK = ”’ + @backupfilename + ”” +
                          CASE WHEN @backupwith IS NULL THEN ” ELSE (’ WITH ‘ + @backupwith) END

         IF @debug = 1 PRINT ‘FULL BACKUP : ‘ + @execmd

         BEGIN TRY

            EXEC(@execmd)

         END TRY
         BEGIN CATCH — backup failure
            SELECT @err = @@ERROR,@ret = @err
            SELECT @errormsg = ‘Full backup of database ‘ + @database + ‘ failed with error : ‘ +  CAST(@err as varchar(10))
            SET @output = SPACE(4) + ‘*** ‘ + @errormsg + ‘ ***’
            IF @debug = 1 PRINT @output
            IF @report = 1
            BEGIN
               EXEC sp_OAMethod @file,’WriteLine’,NULL,@output
               SET @output = SPACE(4) + ‘Refer to SQL Error Log and NT Event Log for further details’
               EXEC sp_OAMethod @file,’WriteLine’,NULL,@output
               EXEC sp_OAMethod @file,’WriteLine’,NULL,”
            END
            CLOSE dcur
            DEALLOCATE dcur
            GOTO CLEANUP

         END CATCH 

         — backup success
         SET @finish = GETDATE()
         SET @output = SPACE(4) + ‘Database backed up to ‘ + @backupfilename
         IF @debug = 1 PRINT @output
         IF @report = 1
         BEGIN
            EXEC sp_OAMethod @file,’WriteLine’,NULL,@output
         END

         –calculate backup runtime
         SET @runtime = (@finish - @start)
         SET @output = SPACE(4) + ‘Full database backup completed in ‘
                     + CAST(DATEPART(hh,@runtime) as varchar(2)) + ‘ hour(s) ‘
                     + CAST(DATEPART(mi,@runtime) as varchar(2)) + ‘ min(s) ‘
                     + CAST(DATEPART(ss,@runtime) as varchar(2)) + ‘ second(s)’
         IF @debug = 1 PRINT @output
         IF @report = 1
         BEGIN
            EXEC sp_OAMethod @file,’WriteLine’,NULL,@output
            EXEC sp_OAMethod @file,’WriteLine’,NULL,”
         END

      END

      /************************
         DIFFERENTIAL BACKUP
      ************************/

      IF UPPER(@optype) = ‘DIFF’
      BEGIN

         IF @compression=1 AND @compressok=1
            SET @execmd = N’BACKUP DATABASE [' + @database + '] TO DISK = ”’ + @backupfilename + ”” +
                          N’ WITH DIFFERENTIAL,COMPRESSION’ + CASE WHEN @backupwith IS NULL THEN N” ELSE (N’,’ + @backupwith) END
         ELSE
            SET @execmd = N’BACKUP DATABASE [' + @database + '] TO DISK = ”’ + @backupfilename + N”’ WITH DIFFERENTIAL’ +
                          CASE WHEN @backupwith IS NULL THEN N” ELSE (N’,’ + @backupwith) END

         IF @debug = 1 PRINT ‘DIFFERENTIAL BACKUP : ‘ + @execmd

         BEGIN TRY

            EXEC(@execmd)

         END TRY
         BEGIN CATCH — backup failure

            SELECT @err = @@ERROR,@ret = @err
            SELECT @errormsg = ‘Differential backup of database ‘ + @database + ‘ failed with error : ‘ +  CAST(@err as varchar(10))
            SET @output = SPACE(4) + ‘*** ‘ + @errormsg + ‘ ***’
            IF @debug = 1 PRINT @output
            IF @report = 1
            BEGIN
               EXEC sp_OAMethod @file,’WriteLine’,NULL,@output
               SET @output = SPACE(4) + ‘Refer to SQL Error Log and NT Event Log for further details’
               EXEC sp_OAMethod @file,’WriteLine’,NULL,@output
            END
            CLOSE dcur
            DEALLOCATE dcur
            GOTO CLEANUP

         END CATCH 

         — backup success
         SET @finish = GETDATE()
         SET @output = SPACE(4) + ‘Database backed up to ‘ + @backupfilename
         IF @debug = 1 PRINT @output
         IF @report = 1
         BEGIN
            EXEC sp_OAMethod @file,’WriteLine’,NULL,@output
         END

         –calculate backup runtime
         SET @runtime = (@finish - @start)
         SET @output = SPACE(4) + ‘Differential database backup completed in ‘
                     + CAST(DATEPART(hh,@runtime) as varchar(2)) + ‘ hour(s) ‘
                     + CAST(DATEPART(mi,@runtime) as varchar(2)) + ‘ min(s) ‘
                     + CAST(DATEPART(ss,@runtime) as varchar(2)) + ‘ second(s)’
         IF @debug = 1 PRINT @output
         IF @report = 1
         BEGIN
            EXEC sp_OAMethod @file,’WriteLine’,NULL,@output
            EXEC sp_OAMethod @file,’WriteLine’,NULL,”
         END

      END

      /************************
             LOG BACKUP
      ************************/

      IF UPPER(@optype) = ‘LOG’
      BEGIN

         IF @compression=1 AND @compressok=1
            SET @execmd = N’BACKUP LOG [' + @database + '] TO DISK = ”’ + @backupfilename + ”” +
                           ‘ WITH COMPRESSION’ + CASE WHEN @backupwith IS NULL THEN ” ELSE (’,’ + @backupwith) END
         ELSE
            SET @execmd = N’BACKUP LOG [' + @database + '] TO DISK = ”’ + @backupfilename + ”” +
                          CASE WHEN @backupwith IS NULL THEN ” ELSE (’ WITH ‘ + @backupwith) END

         IF @debug = 1 PRINT ‘LOG BACKUP : ‘ + @execmd

         BEGIN TRY

            EXEC(@execmd)

         END TRY
         BEGIN CATCH — backup failure

            SELECT @err = @@ERROR,@ret = @err
            SELECT @errormsg = ‘Log backup of database ‘ + @database + ‘ failed with error : ‘ +  CAST(@err as varchar(10))
            SET @output = SPACE(4) + ‘*** ‘ + @errormsg + ‘ ***’
            IF @debug = 1 PRINT @output
            IF @report = 1
            BEGIN
               EXEC sp_OAMethod @file,’WriteLine’,NULL,@output
               SET @output = SPACE(4) + ‘Refer to SQL Error Log and NT Event Log for further details’
               EXEC sp_OAMethod @file,’WriteLine’,NULL,@output
            END
            CLOSE dcur
            DEALLOCATE dcur
            GOTO CLEANUP

         END CATCH 

         — backup success
         SET @finish = GETDATE()
         SET @output = SPACE(4) + ‘Log backed up to ‘ + @backupfilename
         IF @debug = 1 PRINT @output
         IF @report = 1
         BEGIN
            EXEC sp_OAMethod @file,’WriteLine’,NULL,@output
         END

         –calculate backup runtime
         SET @runtime = (@finish - @start)
         SET @output = SPACE(4) + ‘Log backup completed in ‘
                     + CAST(DATEPART(hh,@runtime) as varchar(2)) + ‘ hour(s) ‘
                     + CAST(DATEPART(mi,@runtime) as varchar(2)) + ‘ min(s) ‘
                     + CAST(DATEPART(ss,@runtime) as varchar(2)) + ‘ second(s)’
         IF @debug = 1 PRINT @output
         IF @report = 1
         BEGIN
            EXEC sp_OAMethod @file,’WriteLine’,NULL,@output
            EXEC sp_OAMethod @file,’WriteLine’,NULL,”
         END

      END

      SET @stage = (@stage + 1)

      FETCH NEXT FROM dcur into @database
   END

   CLOSE dcur
   DEALLOCATE dcur

   /************************
         VERIFY BACKUP
   ************************/

   IF @verify = 1
   BEGIN

      DECLARE dcur CURSOR LOCAL FAST_FORWARD
      FOR SELECT dbname FROM #databases ORDER BY dbname
      OPEN dcur
      FETCH NEXT FROM dcur into @database
      WHILE @@FETCH_STATUS=0
      BEGIN

         SELECT @backupfilename = @backupfldrorig + REPLACE(@database,””,”) + ‘\’ + REPLACE(REPLACE(@database,’ ‘,’_'),””,’_') +
         CASE WHEN UPPER(@optype) = ‘DB’   THEN ‘_FullDBBackup_’
              WHEN UPPER(@optype) = ‘DIFF’ THEN ‘_DiffDBBackup_’
              WHEN UPPER(@optype) = ‘LOG’  THEN ‘_LogBackup_’
         END + @jobstart +
         CASE WHEN UPPER(@optype) = ‘LOG’ THEN ‘.TRN’ ELSE ‘.BAK’ END

         SET @start = GETDATE()

         — write to text report
         IF @report = 1
         BEGIN
            EXEC sp_OAMethod @file,’WriteLine’,NULL,”
            SET @output = ‘[' + CAST(@stage as varchar(10)) + '] Database ‘ + @database + ‘: Verify Backup File…’
            IF @debug = 1 PRINT @output
            EXEC sp_OAMethod @file,’WriteLine’,NULL,@output
         END

         SET @execmd = N’RESTORE VERIFYONLY FROM DISK = ”’ + @backupfilename + ”” +
                       CASE WHEN @verifywith IS NULL THEN ” ELSE (’ WITH ‘ + @verifywith) END

         BEGIN TRY

            EXEC(@execmd)

         END TRY
         BEGIN CATCH

            SELECT @err = @@ERROR,@ret = @err
            SET @errormsg = ‘Verify of ‘ + @backupfilename + ‘ failed with Native Error : ‘ + CAST(@err as varchar(10))
            SET @output = SPACE(4) + ‘*** ‘ + @errormsg + ‘ ***’
            IF @debug = 1 PRINT @output
            IF @report = 1
            BEGIN
               EXEC sp_OAMethod @file,’WriteLine’,NULL,@output
            END
            CLOSE dcur
            DEALLOCATE dcur
            GOTO CLEANUP

         END CATCH

         — verify success
         SET @finish = GETDATE()
         SET @output = SPACE(4) + ‘Backup file ‘ + @backupfilename + ‘ verified’
         IF @debug = 1 PRINT @output

         IF @report = 1
         BEGIN
            EXEC sp_OAMethod @file,’WriteLine’,NULL,@output
         END

         –calculate verify runtime
         SET @runtime = (@finish - @start)
         SET @output = SPACE(4) + ‘Verify backup completed in ‘
                     + CAST(DATEPART(hh,@runtime) as varchar(2)) + ‘ hour(s) ‘
                     + CAST(DATEPART(mi,@runtime) as varchar(2)) + ‘ min(s) ‘
                     + CAST(DATEPART(ss,@runtime) as varchar(2)) + ‘ second(s)’
         IF @debug = 1 PRINT @output
         IF @report = 1
         BEGIN
            EXEC sp_OAMethod @file,’WriteLine’,NULL,@output
         END

         SET @stage = (@stage + 1)
         FETCH NEXT FROM dcur into @database
      END     

      CLOSE dcur
      DEALLOCATE dcur
   END

/************************
    DELETE OLD FILES
************************/

   — we have already deleted files so skip to the end
   IF @delfirst = 1 GOTO CLEANUP

   — this label is so that we can delete files prior to backup if @delfirst = 1
   DELFIRST:

   /************************
      DELETE OLD BACKUPS
   ************************/

   SET @datepart = CASE
      WHEN UPPER(@dbretainunit) = ‘MINUTES’ THEN N’mi’
      WHEN UPPER(@dbretainunit) = ‘HOURS’   THEN N’hh’
      WHEN UPPER(@dbretainunit) = ‘DAYS’    THEN N’dd’
      WHEN UPPER(@dbretainunit) = ‘WEEKS’   THEN N’ww’
      WHEN UPPER(@dbretainunit) = ‘MONTHS’  THEN N’mm’
   END

   IF @debug = 1 PRINT ‘@datepart for backups = ‘ + @datepart

   — write to text report
   IF @report = 1
   BEGIN
      EXEC sp_OAMethod @file,’WriteLine’,NULL,”
   END
   SET @output = ‘[' + CAST(@stage as varchar(10)) + '] Delete Old Backup Files…’
   IF @debug = 1 PRINT @output
   IF @report = 1
   BEGIN
      EXEC sp_OAMethod @file,’WriteLine’,NULL,@output
   END

   DECLARE dcur CURSOR LOCAL FAST_FORWARD
   FOR SELECT dbname FROM #databases ORDER BY dbname
   OPEN dcur
   FETCH NEXT FROM dcur into @database
   WHILE @@FETCH_STATUS=0
   BEGIN

      SET @backupfldr = + @backupfldrorig + REPLACE(@database,””,”) + ‘\’
      SELECT @backupfilename = @backupfldr + REPLACE(REPLACE(@database,’ ‘,’_'),””,’_') +
      CASE WHEN UPPER(@optype) = ‘DB’   THEN ‘_FullDBBackup_’
           WHEN UPPER(@optype) = ‘DIFF’ THEN ‘_DiffDBBackup_’
           WHEN UPPER(@optype) = ‘LOG’  THEN ‘_LogBackup_’
      END + @jobstart +
      CASE WHEN UPPER(@optype) = ‘LOG’ THEN ‘.TRN’ ELSE ‘.BAK’ END

      — load files in @backupfldr
      IF @checkattrib = 1
         SET @cmd = ‘dir /B /A-D-A /OD “‘ + @backupfldr + REPLACE(REPLACE(@database,’ ‘,’_'),””,’_') +
         CASE WHEN UPPER(@optype) = ‘DB’ THEN ‘_FullDBBackup_’
              WHEN UPPER(@optype) = ‘DIFF’ THEN ‘_DiffDBBackup_’
              WHEN UPPER(@optype) = ‘LOG’  THEN ‘_LogBackup_’ END + ‘*’ +
         CASE WHEN UPPER(@optype) = ‘LOG’ THEN ‘.TRN’ ELSE ‘.BAK’ END + ‘”‘
      ELSE
         SET @cmd = ‘dir /B /A-D /OD “‘ + @backupfldr + REPLACE(REPLACE(@database,’ ‘,’_'),””,’_') +
         CASE WHEN UPPER(@optype) = ‘DB’ THEN ‘_FullDBBackup_’
              WHEN UPPER(@optype) = ‘DIFF’ THEN ‘_DiffDBBackup_’
              WHEN UPPER(@optype) = ‘LOG’  THEN ‘_LogBackup_’ END + ‘*’ +
         CASE WHEN UPPER(@optype) = ‘LOG’ THEN ‘.TRN’ ELSE ‘.BAK’ END + ‘”‘

      IF @debug = 1 PRINT ‘@cmd = ‘ + @cmd

      DELETE #files
      INSERT #files EXEC master.dbo.xp_cmdshell @cmd
      DELETE #files WHERE filename IS NULL or filename = ISNULL(REPLACE(@backupfilename,@backupfldr,”),’nothing’)

      IF @debug = 1 SELECT * FROM #files

      — get count of files that match pattern
      SELECT @filecount = COUNT(*) from #files
      WHERE PATINDEX(’%File Not Found%’,filename) = 0
      AND PATINDEX(’%The system cannot find%’,filename) = 0 

      — remove files that don’t meet retention criteria if there are any files that match pattern
      IF UPPER(@dbretainunit) <> ‘COPIES’
      BEGIN
         IF @filecount>0
         BEGIN
            SET @delcmd = N’DELETE #files WHERE DATEADD(’ + @datepart + N’,’ + CAST(@dbretainval as nvarchar(10)) + N’,’ +
                    ‘CONVERT(datetime,(SUBSTRING(SUBSTRING(filename,((LEN(filename)-CHARINDEX(”_”,REVERSE(filename)))+2),12),7,2) +”/”
                    + SUBSTRING(SUBSTRING(filename,((LEN(filename)-CHARINDEX(”_”,REVERSE(filename)))+2),12),5,2) +”/”
                    + SUBSTRING(SUBSTRING(filename,((LEN(filename)-CHARINDEX(”_”,REVERSE(filename)))+2),12),1,4) +” ”
                    + SUBSTRING(SUBSTRING(filename,((LEN(filename)-CHARINDEX(”_”,REVERSE(filename)))+2),12),9,2) +”:”
                    + SUBSTRING(SUBSTRING(filename,((LEN(filename)-CHARINDEX(”_”,REVERSE(filename)))+2),12),11,2)),103)) > ”’ + CAST(@jobdt as nvarchar(25)) + N””

            IF @debug = 1 PRINT ‘@delcmd=’ + @delcmd
            EXEC master.dbo.sp_executesql @delcmd

            SELECT @delcount = COUNT(*) from #files
         END
         ELSE
         BEGIN
            SELECT @delcount = 0
         END
      END
      ELSE  — number of copies not date based (include current backup that’s not in #files)
      BEGIN
         IF @filecount>0
         BEGIN
            IF @dbretainval>1
            BEGIN
               SET @delcmd = N’DELETE #files WHERE filename IN(SELECT TOP ‘ + CAST((@dbretainval-1) as nvarchar(10)) +
                             N’ filename FROM #files ORDER BY substring(filename,((len(filename)+2)-charindex(”_”,reverse(filename))),12) DESC)’

               IF @debug = 1 PRINT ‘@delcmd=’ + @delcmd
               EXEC master.dbo.sp_executesql @delcmd
            END

            SELECT @delcount = COUNT(*) from #files

         END
         ELSE
         BEGIN
            SELECT @delcount = 0
         END
      END

      IF @debug = 1 PRINT ‘@delcount = ‘ + STR(@delcount)

      — if there are any matching files
      IF @filecount>0
      BEGIN
         — are there any files that need deleting
         IF @delcount>0
         BEGIN
            DECLARE FCUR CURSOR FORWARD_ONLY FOR
            SELECT * FROM #files
            OPEN FCUR
            FETCH NEXT FROM FCUR INTO @delfilename
            WHILE @@FETCH_STATUS=0
            BEGIN
               SET @cmd = ‘DEL /Q “‘ + @backupfldr + @delfilename + ‘”‘
               EXEC @cmdret = master.dbo.xp_cmdshell @cmd,no_output   

               — log failure to delete but don’t abort procedure
               IF @cmdret<>0
               BEGIN
                  SET @output = SPACE(4) + ‘*** Error: Failed to delete file ‘ + @backupfldr + @delfilename + ‘ ***’
                  IF @debug = 1 PRINT @output
                  IF @report = 1
                  BEGIN
                     EXEC sp_OAMethod @file,’WriteLine’,NULL,@output
                  END
                  SELECT @delbkflag = 1 , @cmdret = 0, @delcount = (@delcount-1)
               END
               ELSE
               BEGIN
                  SET @output = SPACE(4) + ‘Deleted file ‘ + @backupfldr + @delfilename
                  IF @debug = 1 PRINT @output
                  IF @report = 1
                  BEGIN
                     EXEC sp_OAMethod @file,’WriteLine’,NULL,@output
                  END
               END

               FETCH NEXT FROM FCUR INTO @delfilename
            END
            CLOSE FCUR
            DEALLOCATE FCUR
         END
      END

      — write to text report
      SET @output = SPACE(4) + CAST(@delcount as varchar(10)) + ‘ file(s) deleted.’
      IF @debug = 1 PRINT @output
      IF @report = 1
      BEGIN
         EXEC sp_OAMethod @file,’WriteLine’,NULL,@output
         EXEC sp_OAMethod @file,’WriteLine’,NULL,”
      END

      FETCH NEXT FROM dcur into @database
   END

   CLOSE dcur
   DEALLOCATE dcur

   — clear temporary table and variables
   DELETE #files
   SET @cmd = ”
   SET @delcmd = ”
   SET @delfilename = ”
   SET @datepart = ”
   SET @filecount = 0
   SET @delcount = 0
   SET @cmdret = 0
   SET @stage = @stage + 1

   /************************
      DELETE OLD REPORTS
   ************************/

   DELREPORTS:

   IF @rptretainunit IS NOT NULL
   BEGIN
      SET @datepart = CASE
         WHEN UPPER(@rptretainunit) = ‘MINUTES’ THEN N’mi’
         WHEN UPPER(@rptretainunit) = ‘HOURS’   THEN N’hh’
         WHEN UPPER(@rptretainunit) = ‘DAYS’    THEN N’dd’
         WHEN UPPER(@rptretainunit) = ‘WEEKS’   THEN N’ww’
         WHEN UPPER(@rptretainunit) = ‘MONTHS’  THEN N’mm’
   END

   IF @debug = 1 PRINT ‘@datepart for reports = ‘ + @datepart

   — write to text report
   SET @output = ‘[' + CAST(@stage as varchar(10)) + '] Delete Old Report Files…’
   IF @debug = 1 PRINT @output
   IF @report = 1
   BEGIN
      EXEC sp_OAMethod @file,’WriteLine’,NULL,@output
   END

   — load files in @reportfldr
   SET @cmd = ‘dir /B /A-D /OD “‘ + @reportfldr + REPLACE(REPLACE(@databaseorig,’ ‘,’_'),””,”) +
   CASE WHEN UPPER(@optype) = ‘DB’ THEN ‘_FullDBBackup_report_’
        WHEN UPPER(@optype) = ‘DIFF’ THEN ‘_DiffDBBackup_report_’
        WHEN UPPER(@optype) = ‘REINDEX’  THEN ‘_Reindex_report_’
        WHEN UPPER(@optype) = ‘CHECKDB’  THEN ‘_CheckDB_report_’
        WHEN UPPER(@optype) = ‘REORG’  THEN ‘_Reorg_report_’
        WHEN UPPER(@optype) = ‘LOG’  THEN ‘_LogBackup_report_’ END + ‘*.txt”‘

   IF @debug = 1 PRINT ‘@cmd = ‘ + @cmd

   INSERT #files EXEC master.dbo.xp_cmdshell @cmd
   DELETE #files WHERE filename IS NULL

   IF @debug = 1 SELECT * FROM #files

   — get count of files that match pattern
   SELECT @filecount = COUNT(*) from #files
   WHERE PATINDEX(’%File Not Found%’,filename) = 0
   AND PATINDEX(’%The system cannot find%’,filename) = 0 

   — remove files that don’t meet retention criteria if there are any files that match pattern
   IF UPPER(@rptretainunit) <> ‘COPIES’
   BEGIN
      IF @filecount>0
      BEGIN
         SET @delcmd = N’DELETE #files WHERE DATEADD(’ + @datepart + N’,’ + CAST(@rptretainval as nvarchar(10)) + N’,’ +
                 ‘CONVERT(datetime,(SUBSTRING(SUBSTRING(filename,((LEN(filename)-CHARINDEX(”_”,REVERSE(filename)))+2),12),7,2) +”/”
                 + SUBSTRING(SUBSTRING(filename,((LEN(filename)-CHARINDEX(”_”,REVERSE(filename)))+2),12),5,2) +”/”
                 + SUBSTRING(SUBSTRING(filename,((LEN(filename)-CHARINDEX(”_”,REVERSE(filename)))+2),12),1,4) +” ”
                 + SUBSTRING(SUBSTRING(filename,((LEN(filename)-CHARINDEX(”_”,REVERSE(filename)))+2),12),9,2) +”:”
                 + SUBSTRING(SUBSTRING(filename,((LEN(filename)-CHARINDEX(”_”,REVERSE(filename)))+2),12),11,2)),103)) > ”’ + CAST(@jobdt as nvarchar(25)) + N””

         IF @debug = 1 PRINT ‘@delcmd=’ + @delcmd
         EXEC master.dbo.sp_executesql @delcmd

         SELECT @delcount = COUNT(*) from #files
      END
      ELSE
      BEGIN
         SELECT @delcount = 0
      END
   END
   ELSE  — number of copies not date based
   BEGIN
      IF @filecount>0
      BEGIN
         SET @delcmd = N’DELETE #files WHERE filename IN(SELECT TOP ‘ + CAST(@rptretainval as nvarchar(10)) +
                       N’ filename FROM #files ORDER BY substring(filename,((len(filename)+2)-charindex(”_”,reverse(filename))),12) DESC)’

         IF @debug = 1 PRINT ‘@delcmd=’ + @delcmd
         EXEC master.dbo.sp_executesql @delcmd

         SELECT @delcount = COUNT(*) from #files
      END
      ELSE
      BEGIN
         SELECT @delcount = 0
      END
   END

   IF @debug = 1 PRINT STR(@delcount)

   — if there are any matching files
   IF @filecount>0
   BEGIN
      — are there any files that need deleting
      IF @delcount>0
      BEGIN
         DECLARE FCUR CURSOR FORWARD_ONLY FOR
         SELECT * FROM #files
         OPEN FCUR
         FETCH NEXT FROM FCUR INTO @delfilename
         WHILE @@FETCH_STATUS=0
         BEGIN
            SET @cmd = ‘DEL /Q “‘ + @reportfldr + @delfilename + ‘”‘
            EXEC @cmdret = master.dbo.xp_cmdshell @cmd,no_output   

            — log failure to delete but don’t abort procedure
            IF @cmdret<>0
            BEGIN

               SET @output = SPACE(4) + ‘*** Error: Failed to delete file ‘ + @reportfldr + @delfilename + ‘ ***’
               IF @debug = 1 PRINT @output
               IF @report = 1
               BEGIN
                  EXEC sp_OAMethod @file,’WriteLine’,NULL,@output
               END
               SELECT @delrptflag = 1 , @cmdret = 0, @delcount = (@delcount-1)
            END
            BEGIN
               SET @output = SPACE(4) + ‘Deleted file ‘ + @reportfldr + @delfilename
               IF @debug = 1 PRINT @output
               IF @report = 1
               BEGIN
                  EXEC sp_OAMethod @file,’WriteLine’,NULL,@output
               END
            END

            FETCH NEXT FROM FCUR INTO @delfilename
         END
         CLOSE FCUR
         DEALLOCATE FCUR
      END
   END

   — write to text report
   SET @output = SPACE(4) + CAST(@delcount as varchar(10)) + ‘ file(s) deleted.’
   IF @debug = 1 PRINT @output
   IF @report = 1
   BEGIN
      EXEC sp_OAMethod @file,’WriteLine’,NULL,@output
      EXEC sp_OAMethod @file,’WriteLine’,NULL,”
   END

   — update stage
   SET @stage = @stage + 1
   END
   — if we got here due to @delfirst = 1 go back and do the backups
   IF @delfirst = 1
      GOTO DOBACKUP
   ELSE
      GOTO CLEANUP

/************************
         CHECKDB
************************/

   CHECK_DB:

   IF @optype = ‘CHECKDB’
   BEGIN

      DECLARE dcur CURSOR LOCAL FAST_FORWARD
      FOR SELECT dbname FROM #databases ORDER BY dbname
      OPEN dcur
      FETCH NEXT FROM dcur into @database
      WHILE @@FETCH_STATUS=0
      BEGIN

         — write to text report
         IF @report = 1
         BEGIN
            EXEC sp_OAMethod @file,’WriteLine’,NULL,”
            SET @output = ‘[' + CAST(@stage as varchar(10)) + '] Database ‘ + @database + ‘: Check Data and Index Linkage…’
            IF @debug = 1 PRINT @output
            EXEC sp_OAMethod @file,’WriteLine’,NULL,@output
         END

         — set backup start time
         SET @start = GETDATE()

         SET @execmd = N’DBCC CHECKDB([' + @database + N']) WITH NO_INFOMSGS’
         IF @debug = 1 PRINT ‘DBCC Command : ‘ + @execmd

         BEGIN TRY

            EXEC(@execmd)

         END TRY
         BEGIN CATCH

            SELECT @err = @@ERROR,@ret = @err
            SET @errormsg = ‘CheckDB of ‘ + @database + ‘ failed with Native Error : ‘ + CAST(@err as varchar(10))
            SET @output = SPACE(4) + ‘*** ‘ + @errormsg + ‘ ***’
            PRINT @output
            IF @report = 1
            BEGIN
               EXEC sp_OAMethod @file,’WriteLine’,NULL,@output
            END
            CLOSE dcur
            DEALLOCATE dcur
            GOTO CLEANUP

         END CATCH

         SET @finish = GETDATE()

         –calculate checkdb runtime
         SET @runtime = (@finish - @start)
         SET @output = SPACE(4) + ‘CheckDB completed in ‘
                     + CAST(DATEPART(hh,@runtime) as varchar(2)) + ‘ hour(s) ‘
                     + CAST(DATEPART(mi,@runtime) as varchar(2)) + ‘ min(s) ‘
                     + CAST(DATEPART(ss,@runtime) as varchar(2)) + ‘ second(s)’
         IF @debug = 1 PRINT @output
         IF @report = 1
         BEGIN
            EXEC sp_OAMethod @file,’WriteLine’,NULL,@output
            EXEC sp_OAMethod @file,’WriteLine’,NULL,”
         END
         SET @stage = (@stage + 1)
         FETCH NEXT FROM dcur into @database      

      END

      CLOSE dcur
      DEALLOCATE dcur

      — delete reports
      IF @report = 1
      BEGIN
         EXEC sp_OAMethod @file,’WriteLine’,NULL,”
      END
      GOTO DELREPORTS
   END

/************************
     REINDEX/REORG
************************/

   REINDEX:

   IF @optype in (’REINDEX’,'REORG’)
   BEGIN

      DECLARE dcur CURSOR LOCAL FAST_FORWARD
      FOR SELECT dbname FROM #databases ORDER BY dbname
      OPEN dcur
      FETCH NEXT FROM dcur into @database
      WHILE @@FETCH_STATUS=0
      BEGIN

         — write to text report
         IF @report = 1
         BEGIN
            EXEC sp_OAMethod @file,’WriteLine’,NULL,”
            IF @optype = ‘REINDEX’
               SET @output = ‘[' + CAST(@stage as varchar(10)) + '] Database ‘ + @database + ‘: Index Rebuild (using original fillfactor)…’
            ELSE
               SET @output = ‘[' + CAST(@stage as varchar(10)) + '] Database ‘ + @database + ‘: Index Reorganize…’

            IF @debug = 1 PRINT @output
            EXEC sp_OAMethod @file,’WriteLine’,NULL,@output
            EXEC sp_OAMethod @file,’WriteLine’,NULL,”
         END

         — set start time
         SET @start = GETDATE()

         — all user tables
         CREATE TABLE #tables(tablename sysname)
         EXEC(N’INSERT #tables(tablename) SELECT DISTINCT(”['' + s.[name] + ”].['' + t.[name] + ”]”) FROM [' + @database + N'].sys.tables t ‘ +
              N’JOIN [' + @database + N'].sys.schemas s on t.schema_id=s.schema_id ‘ +
              N’JOIN [' + @database + N'].sys.indexes i on t.object_id=i.object_id ‘ +
              N’WHERE t.is_ms_shipped = 0 AND i.type>0′)

         DECLARE tcur CURSOR LOCAL FAST_FORWARD
         FOR SELECT tablename FROM #tables ORDER BY tablename
         OPEN tcur
         FETCH NEXT FROM tcur INTO @table
         WHILE @@FETCH_STATUS = 0
         BEGIN

            IF @report = 1
            BEGIN
               IF @optype = ‘REINDEX’
                  SET @output = SPACE(4) + N’Rebuilding indexes for table ‘ + @table
               ELSE
                  SET @output = SPACE(4) + N’Reorganizing indexes for table ‘ + @table

               EXEC sp_OAMethod @file,’WriteLine’,NULL,@output
            END   

            IF @optype = ‘REINDEX’
               SET @execmd = N’ALTER INDEX ALL ON [' + @database + N'].’ + @table + N’ REBUILD’
            ELSE
               SET @execmd = N’ALTER INDEX ALL ON [' + @database + N'].’ + @table + N’ REORGANIZE’

            IF @debug = 1 PRINT ‘Reindex Command : ‘ + @execmd

            BEGIN TRY

               EXEC(@execmd)

            END TRY
            BEGIN CATCH

               SELECT @err = @@ERROR,@ret = @err
               SET @errormsg = ‘Rebuild of indexes on [' + @database + N'].’ + @table + ‘ failed with Native Error : ‘ + CAST(@err as varchar(10))
               SET @output = SPACE(4) + ‘*** ‘ + @errormsg + ‘ ***’
               PRINT @output
               IF @report = 1
               BEGIN
                  EXEC sp_OAMethod @file,’WriteLine’,NULL,@output
               END
               CLOSE tcur
               DEALLOCATE tcur
               DROP TABLE #tables
               GOTO CLEANUP

            END CATCH

            FETCH NEXT FROM tcur INTO @table
         END

         CLOSE tcur
         DEALLOCATE tcur

         SET @finish = GETDATE()

         –calculate runtime
         SET @runtime = (@finish - @start)
         SET @output = SPACE(4) + ‘Index maintenance completed in ‘
                     + CAST(DATEPART(hh,@runtime) as varchar(2)) + ‘ hour(s) ‘
                     + CAST(DATEPART(mi,@runtime) as varchar(2)) + ‘ min(s) ‘
                     + CAST(DATEPART(ss,@runtime) as varchar(2)) + ‘ second(s)’
         IF @debug = 1 PRINT @output
         IF @report = 1
         BEGIN
            EXEC sp_OAMethod @file,’WriteLine’,NULL,”
            EXEC sp_OAMethod @file,’WriteLine’,NULL,@output
            EXEC sp_OAMethod @file,’WriteLine’,NULL,”
         END   

         DROP TABLE #tables

         SET @stage = (@stage + 1)
         FETCH NEXT FROM dcur into @database   

      END

      CLOSE dcur
      DEALLOCATE dcur

      — delete reports
      GOTO DELREPORTS
   END

/************************
         CLEAN UP
************************/

   CLEANUP:

   DROP TABLE #files
   DROP TABLE #exists
   DROP TABLE #databases

   — if we encountered errors deleting old backups return failure
   IF @delbkflag<>0
   BEGIN
      SET @errormsg = ‘Expressmaint encountered errors deleting old backup files’ + CHAR(13)
                    + CASE WHEN @report = 1 THEN (’Please see ‘ + @reportfilename + CHAR(13) + ‘ for further details’) ELSE ” END
      RAISERROR(@errormsg,16,1)
      SET @ret = 1
   END

   — if we encountered errors deleting old reports return failure
   IF (@delrptflag<>0 AND @delbkflag = 0)
   BEGIN
      SET @errormsg = ‘Expressmaint encountered errors deleting old report files’ + CHAR(13)
                    + CASE WHEN @report = 1 THEN (’Please see ‘ + @reportfilename + CHAR(13) + ‘ for further details’) ELSE ” END
      RAISERROR(@errormsg,16,1)
      SET @ret = 1
   END

   — if we created a file make sure we write trailer and destroy object
   IF @filecrt = 1
   BEGIN
      — write final part of report
      EXEC sp_OAMethod @file,’WriteLine’,NULL,”
      SET @output = ‘Expressmaint processing finished at ‘ + CONVERT(varchar(25),GETDATE(),100)
                  + ‘ (Return Code : ‘ + CAST(@ret as varchar(10)) + ‘)’
      IF @debug = 1 PRINT @output
      EXEC sp_OAMethod @file,’WriteLine’,NULL,@output
      EXEC sp_OAMethod @file,’WriteLine’,NULL,”

      — destroy file object
      EXEC @hr=sp_OADestroy @file
      IF @hr <> 0 EXEC sp_OAGetErrorInfo @file
   END

   IF @report = 1
   BEGIN
      EXEC @hr=sp_OADestroy @fso
      IF @hr <> 0 EXEC sp_OAGetErrorInfo @fso
   END

RETURN @ret
GO

PRINT ‘Stored Procedure created successfully’
Script con la configuración (descargalo como fichero):


exec expressmaint
   @database      = ‘ALL_USER’,
   @optype        = ‘DB’,
   @backupfldr    = ‘c:\backups’,
   @reportfldr    = ‘c:\reports’,
   @verify        = 1,
   @dbretainunit  = ‘weeks’,
   @dbretainval   = 1,
   @rptretainunit = ‘weeks’,
   @rptretainval  = 1,
   @report        = 1
Fichero por lotes(Descargalo como fichero):


sqlcmd -S .\SQLExpress -i c:\expressmaint.sql
Ahora solo nos queda crear una nueva tarea programda en Windows.

posted in Programacion, Sql Server, windows | 1 Comment

  • Publicidad

  • Calendar

  • March 2010
    M T W T F S S
    « Nov    
    1234567
    891011121314
    15161718192021
    22232425262728
    293031