47
SQL Server Backup to Azure Christian Sanabria J. [email protected] @csanabriaj https://cr.linkedin.com/in/ csanabria MCP Office 365, ITILf, Scrum Master [email protected] Preparado con apoyo de: Juan Carlos Gilaranz B. http://www.mundosql.es Microsoft Active Professional 2014 España

SQL Server 2014 Backup to Azure - SQL Saturday CR 2015

Embed Size (px)

Citation preview

Page 1: SQL Server 2014 Backup to Azure - SQL Saturday CR 2015

SQL Server Backup to Azure

Christian Sanabria J.

[email protected]

@csanabriaj

https://cr.linkedin.com/in/csanabria

MCP Office 365, ITILf, Scrum Master

[email protected]

Preparado con apoyo de:

Juan Carlos Gilaranz B.

http://www.mundosql.es

Microsoft Active Professional 2014

España

Page 2: SQL Server 2014 Backup to Azure - SQL Saturday CR 2015

Organiza

http://tinyurl.com/ComunidadWindows

Page 3: SQL Server 2014 Backup to Azure - SQL Saturday CR 2015

Patrocinadores del SQL Saturday

Premier Sponsor

Gold Sponsor

Bronze Sponsor

Page 4: SQL Server 2014 Backup to Azure - SQL Saturday CR 2015

Agenda

¿Qué se requiere? Azure SQL Server

Configuración en Azure Servicio Datos necesarios

Creación de respaldos: Respaldar mediante interfaz gráfica Respaldar mediante T-SQL Comandos de Powershell

Versiones de SQL Server que no lo soportan. ¿Qué hacer?

5/19/2015 |

SQL Backup to Azure4 |

Page 5: SQL Server 2014 Backup to Azure - SQL Saturday CR 2015

¿Qué NO se trata?

Tipos ni planes de respaldos Detalles y conceptos de Azure / Servicios,

etc. Respaldos de servicios SQL en máquinas

virtuales Azure o Azure SQL Storage Hablamos de respaldo de SQL on-premises hacia

Azure

5/19/2015 |

SQL Backup to Azure5 |

Page 6: SQL Server 2014 Backup to Azure - SQL Saturday CR 2015

SQL Backup to Azure

Disponible desde SQL Server 2012 SP1 CU2 como opción adicional

También conocido como “Backup to URL”

5/19/2015 |

SQL Backup to Azure6 |

ProblemAs DBAs, we don't only have the responsibility to backup databases on regular basis, but also to ensure that backup drives and tapes are secure so databases can be restored when needed. I heard that SQL Server 2012 supports backups and restores using a Windows Azure Blob Storage account.  How does this work and how do I get started?By: Arshad Ali 

Page 7: SQL Server 2014 Backup to Azure - SQL Saturday CR 2015

Configuración de Windows Azure

https://manage.windowsazure.com

Ver:https://www.youtube.com/watch?v=NuVsVCCwLmA

5/19/2015 |

SQL Backup to Azure7 |

Page 8: SQL Server 2014 Backup to Azure - SQL Saturday CR 2015

Azure Management Portal

5/19/2015 |

SQL Backup to Azure8 |

Page 9: SQL Server 2014 Backup to Azure - SQL Saturday CR 2015

Acceso al Storage

5/19/2015 |

SQL Backup to Azure9 |

Page 10: SQL Server 2014 Backup to Azure - SQL Saturday CR 2015

Creación del contenedor dentro del servicio de Storage

5/19/2015 |

SQL Backup to Azure10 |

Permite listar contenido

Se debe conocer la ruta exacta

Page 11: SQL Server 2014 Backup to Azure - SQL Saturday CR 2015

Creación del contenedor

5/19/2015 |

SQL Backup to Azure11 |

Page 12: SQL Server 2014 Backup to Azure - SQL Saturday CR 2015

Contenedor

5/19/2015 |

SQL Backup to Azure12 |

Binary Large Objects (BLOBs)

Page 13: SQL Server 2014 Backup to Azure - SQL Saturday CR 2015

Configuración de SQL Server(on-premises)

5/19/2015 |

SQL Backup to Azure13 |

Configurar: Credencial Login Backup

Métodos: SSMS T-SQL Powershell SMO (Microsoft.SqlServer.Management.Smo)

Page 14: SQL Server 2014 Backup to Azure - SQL Saturday CR 2015

Se debe configurar

1. Credencial para acceso al Storage en Azure

5/19/2015 |

Footer Goes Here14 |

Page 15: SQL Server 2014 Backup to Azure - SQL Saturday CR 2015

Se debe configurar

5/19/2015 |

Footer Goes Here15 |

Login para respaldos

Page 16: SQL Server 2014 Backup to Azure - SQL Saturday CR 2015

Permisos

Permissions BACKUP DATABASE and BACKUP LOG

permissions default to members of the sysadmin fixed server role and the db_owner and db_backupoperator fixed database roles.

https://msdn.microsoft.com/en-us/library/ms186865.aspx

Page 17: SQL Server 2014 Backup to Azure - SQL Saturday CR 2015

Opciones del respaldo

5/19/2015 |

Footer Goes Here17 |

Page 18: SQL Server 2014 Backup to Azure - SQL Saturday CR 2015

Finalmente

5/19/2015 |

Footer Goes Here18 |

19 percent processed.39 percent processed.59 percent processed.72 percent processed.84 percent processed.93 percent processed.Processed 648 pages for database 'BDDemoPASS', file 'BDDemoPASS' on file 1.100 percent processed.Processed 2 pages for database 'BDDemoPASS', file 'BDDemoPASS_log' on file 1.BACKUP DATABASE successfully processed 650 pages in 11.725 seconds (0.433 MB/sec).

SSMS

T-SQL

Page 19: SQL Server 2014 Backup to Azure - SQL Saturday CR 2015

Backup – T-SQL

BACKUP DATABASE [ProductInfoSPA] TO URL = N'https://sqlbackuptec.blob.core.windows.net/sqlbackupazure1/ProductInfoSPA_backup_2015_04_06_113208.bak' WITH CREDENTIAL = N'SQLBackup2AzureCred' , DESCRIPTION = N'Demo SQL PASS 2015', NOFORMAT, NOINIT, NAME = N'ProductInfoSPA-Full Database Backup', NOSKIP, NOREWIND, NOUNLOAD, COMPRESSION, STATS = 10GO

5/19/2015 |

Footer Goes Here19 |

Page 20: SQL Server 2014 Backup to Azure - SQL Saturday CR 2015

Restore – SSMS (2014)

5/19/2015 |

Footer Goes Here20 |

Page 21: SQL Server 2014 Backup to Azure - SQL Saturday CR 2015

Restore – SSMS (2014)

Main Text / Bullets Here, Gray, 30 pt. Main Text / Bullets Here, Gray, 30 pt.

Bullet Points, Line 2, 26 pt. Bullet Points, Line 3, 22 pt.

Bullet Points, Line 4, 20 pt.

5/19/2015 |

Footer Goes Here21 |

Page 22: SQL Server 2014 Backup to Azure - SQL Saturday CR 2015

Restore – T-SQL

USE [master] ALTER DATABASE [ProductInfoSPA] SET SINGLE_USER WITH ROLLBACK IMMEDIATE BACKUP LOG [ProductInfoSPA] TO URL =

N'https://sqlbackuptec.blob.core.windows.net/sqlbackupazure1/ProductInfoSPA_LogBackup_2015-04-06_16-24-57.bak' WITH CREDENTIAL = N'SQLBackup2AzureCred' , NOFORMAT, NOINIT, NAME = N'ProductInfoSPA_LogBackup_2015-04-06_16-24-57', NOSKIP, NOREWIND, NOUNLOAD, NORECOVERY , STATS = 5

RESTORE DATABASE [ProductInfoSPA] FROM URL = N'https://sqlbackuptec.blob.core.windows.net/sqlbackupazure1/ProductInfoSPA_backup_2015_04_06_113208.bak' WITH CREDENTIAL = N'SQLBackup2AzureCred' , FILE = 1, NORECOVERY, NOUNLOAD, REPLACE, STATS = 5

RESTORE LOG [ProductInfoSPA] FROM URL = N'https://sqlbackuptec.blob.core.windows.net/sqlbackupazure1/ProductInfoSPA_LogBackup_2015-04-06_16-21-51.bak' WITH CREDENTIAL = N'SQLBackup2AzureCred' , FILE = 1, NOUNLOAD, STATS = 5

ALTER DATABASE [ProductInfoSPA] SET MULTI_USER

GO

5/19/2015 |

Footer Goes Here22 |

Page 23: SQL Server 2014 Backup to Azure - SQL Saturday CR 2015

Restore exitoso

5/19/2015 |

Footer Goes Here23 |

Page 24: SQL Server 2014 Backup to Azure - SQL Saturday CR 2015

Respaldo y restauración con Azure usando PowerShell

5/19/2015 |

Footer Goes Here24 |

import-module sqlps

# definir variables $storageAccount = "mystorageaccount" $storageKey = "<storageaccesskeyvalue>" $secureString = convertto-securestring $storageKey -asplaintext -force $credentialName = "mybackuptoURL"

#cd to computer level cd sqlserver:\sql\COMPUTERNAME

# get the list of instances $instances = Get-childitem

#pipe the instances to new-sqlcredentail cmdlet to create SQL credential $instances | new-sqlcredential -Name $credentialName -Identity $storageAccount -Secret $secureString

Powershell – crear credencial

Page 26: SQL Server 2014 Backup to Azure - SQL Saturday CR 2015

Referencias y código C#

5/19/2015 |

Footer Goes Here26 |

Page 27: SQL Server 2014 Backup to Azure - SQL Saturday CR 2015

SMO – C# - BackupDatabase

5/19/2015 |

Footer Goes Here27 |

Page 28: SQL Server 2014 Backup to Azure - SQL Saturday CR 2015

SMO – C# - RestoreDatabase

Page 29: SQL Server 2014 Backup to Azure - SQL Saturday CR 2015

Versiones de SQL Server que no lo soportan¿Qué hacer?

SQL Backup to Azure Tool Antes de SQL 2012 SP1 Configuración similar

Mismos datos

Se basa en reglas: Carpetas Tipos de archivo

Permite: Compresión Distintos tipos de encripción

Está en: http://www.microsoft.com/en-us/download/details.aspx?id=40740

Video de forma de uso en: https://www.youtube.com/watch?v=5epRBcwEz00

5/19/2015 |

Footer Goes Here29 |

Page 30: SQL Server 2014 Backup to Azure - SQL Saturday CR 2015

Configuración – agregar regla

5/19/2015 |

Footer Goes Here30 |

Page 31: SQL Server 2014 Backup to Azure - SQL Saturday CR 2015

Configuración - Ruta y patrón

5/19/2015 |

Footer Goes Here31 |

Page 32: SQL Server 2014 Backup to Azure - SQL Saturday CR 2015

Configuración – Storage y contenedor

5/19/2015 |

Footer Goes Here32 |

Page 33: SQL Server 2014 Backup to Azure - SQL Saturday CR 2015

Configuración – cifrado y compresión

5/19/2015 |

Footer Goes Here33 |

Page 34: SQL Server 2014 Backup to Azure - SQL Saturday CR 2015

Lista y opciones

5/19/2015 |

Footer Goes Here34 |

Page 35: SQL Server 2014 Backup to Azure - SQL Saturday CR 2015

Mejores prácticas

Usar nombres únicos para identificar cada backup fecha/hora/bd/servidor

Definir el acceso al contenedor como private Utilizar la misma región para mejorar rendimiento y manejar costos (*) Monitorear constantemente el fallo/éxito de las operaciones si se

automatiza Usar la opción WITH COMPRESSION para minimizar los costos de

storage y el tiempo Usar encripción (*) Hacer pruebas de tiempo de respaldo y recuperación Revisar los SLA’s de Azure(

http://www.microsoft.com/en-us/download/details.aspx?id=6656)

https://msdn.microsoft.com/en-us/library/jj919149.aspx

5/19/2015 |

Footer Goes Here35 |

Page 36: SQL Server 2014 Backup to Azure - SQL Saturday CR 2015

¿Cómo adapto mi plan de respaldos?

Page 38: SQL Server 2014 Backup to Azure - SQL Saturday CR 2015

Gracias!

PASS PASS LATAM PASS CR Chapter

5/19/2015 |

Footer Goes Here38 |

Todos ustedes!!!

Page 39: SQL Server 2014 Backup to Azure - SQL Saturday CR 2015

Slides de apoyo

Page 40: SQL Server 2014 Backup to Azure - SQL Saturday CR 2015

Estructura del Storage en Azure

5/19/2015 |

Footer Goes Here40 |

Page 41: SQL Server 2014 Backup to Azure - SQL Saturday CR 2015

Tail-log backup (respaldo del log)

A tail-log backup is a transaction log backup that includes the portion of the log that has not previously been backed up (known as the active portion of the log). A tail-log backup does not truncate the log and is generally used when the data files for a database have become inaccessible but the log file is undamaged.

5/19/2015 |

Footer Goes Here41 |

Page 42: SQL Server 2014 Backup to Azure - SQL Saturday CR 2015

Error de master key decryption

Page 43: SQL Server 2014 Backup to Azure - SQL Saturday CR 2015

Respaldo FULL (incluyendo BD’s del sistema)

 

This includes both user databases and msdb system database. The script filters out tempdb and model system databases.

5/19/2015 |

Footer Goes Here43 |

import-module sqlps

# set the parameter values $storageAccount = "mystorageaccount" $blobContainer = "privatecontainertest" $backupUrlContainer = "https://$storageAccount.blob.core.windows.net/$blobContainer/" $credentialName = "mybackuptoURL"

# cd to computer level cd SQLServer:\SQL\COMPUTERNAME $instances = Get-childitem

# loop through each instances and backup up all the databases -filter out tempdb and model databases foreach ($instance in $instances) { $path = "sqlserver:\sql\$($instance.name)\databases" $alldatabases = get-childitem -Force -path $path | Where-object {$_.name -ne "tempdb" -and $_.name -ne "model"} $alldatabases | Backup-SqlDatabase -BackupContainer $backupUrlContainer -SqlCredential $credentialName -Compression On -script }

Referencia:https://msdn.microsoft.com/en-us/library/dn223322.aspx

Page 44: SQL Server 2014 Backup to Azure - SQL Saturday CR 2015

Full Database Backup for ALL User Databases

import-module sqlps $storageAccount = "mystorageaccount" $blobContainer = "privatecontainertest" $backupUrlContainer = "https://$storageAccount.blob.core.windows.net/$blobContainer/" $credentialName = "mybackuptoURL"

# cd to computer level cd SQLServer:\SQL\COMPUTERNAME $instances = Get-childitem

# loop through each instances and backup up all the user databases foreach ($instance in $instances) {

$databases = dir "sqlserver:\sql\$($instance.name)\databases" $databases | Backup-SqlDatabase -BackupContainer $backupUrlContainer -SqlCredential $credentialName -Compression On

}

5/19/2015 |

Footer Goes Here44 |

Page 45: SQL Server 2014 Backup to Azure - SQL Saturday CR 2015

Full Database Backup for MASTER and MSDB (SYSTEM DATABASES) On All the Instances of SQL Server

The following script can be used to back up master and msdb databases on all the instances of SQL Server installed on the computer.

import-module sqlps $storageAccount = "mystorageaccount" $blobContainer = "privatecontainertest" $backupUrlContainer = "https://$storageAccount.blob.core.windows.net/$blobContainer/" $credentialName = "mybackupToUrl" $sysDbs = "master", "msdb"

#cd to computer level cd sqlserver:\sql\COMPUTERNAME $instances = Get-childitem foreach ($instance in $instances) {

foreach ($s in $sysdbs) { Backup-SqlDatabase -Database $s -path "sqlserver:\sql\$

($instance.name)" -BackupContainer $backupUrlContainer -SqlCredential $credentialName - Compression On

} }

5/19/2015 |

Footer Goes Here45 |

Page 46: SQL Server 2014 Backup to Azure - SQL Saturday CR 2015

Ejemplo de script Powershell

$db = $svr.Databases['AdventureWorks']

$dbname = $db.Name

$dt = get-date -format yyyyMMddHHmmss

$dbbk = new-object ('Microsoft.SqlServer.Management.Smo.Backup')

$dbbk.Action = 'Database'

$dbbk.BackupSetDescription = "Full backup of " + $dbname

$dbbk.BackupSetName = $dbname + " Backup"

$dbbk.Database = $dbname

$dbbk.MediaDescription = "Disk"

$dbbk.Devices.AddDevice($bdir + "\" + $dbname + "_db_" + $dt + ".bak", 'File')

$dbbk.SqlBackup($svr)

5/19/2015 |

Footer Goes Here46 |

Page 47: SQL Server 2014 Backup to Azure - SQL Saturday CR 2015

Uso del Cmdlet Backup-SqlDatabase

$svnm = $svr.Name $db = $svr.Databases['AdventureWorks'] $dbname = $db.Name $dt = get-date -format yyyyMMddHHmmss $bfil = "$bdir\$($dbname)_db_$($dt).bak" Backup-SqlDatabase -ServerInstance $svnm -

Database $dbname -BackupFile $bfil

5/19/2015 |

Footer Goes Here47 |

http://sqlmag.com/powershell/powershell-lets-you-back-sql-server-your-way