PowerShell: Menyalin database ke server baru

Berlaku untuk:Azure SQL Database

Contoh skrip Azure PowerShell ini membuat salinan database yang sudah ada di Azure SQL Database di server baru.

Jika Anda tidak memiliki langganan Azure, buat akun Azure gratis sebelum memulai.

Catatan

Artikel ini menggunakan modul Azure Az PowerShell, yang merupakan modul PowerShell yang direkomendasikan untuk berinteraksi dengan Azure. Untuk mulai menggunakan modul Az PowerShell, lihat Menginstal Azure PowerShell. Untuk mempelajari cara bermigrasi ke modul Az PowerShell, lihat Memigrasikan Azure PowerShell dari AzureRM ke Az.

Menggunakan Azure Cloud Shell

Azure meng-hosting Azure Cloud Shell, lingkungan shell interaktif yang dapat Anda gunakan melalui browser. Anda dapat menggunakan Bash atau PowerShell dengan Cloud Shell untuk bekerja dengan layanan Azure. Anda dapat menggunakan perintah Cloud Shell yang telah diinstal sebelumnya untuk menjalankan kode dalam artikel ini tanpa harus menginstal apa-apa di lingkungan lokal Anda.

Untuk memulai Azure Cloud Shell:

Opsi Contoh/Tautan
Pilih Coba di sudut kanan atas blok kode. Memilih Coba tidak secara otomatis menyalin kode ke Cloud Shell. Screenshot that shows an example of Try It for Azure Cloud Shell.
Buka https://shell.azure.com, atau pilih tombol Luncurkan Cloud Shell untuk membuka Cloud Shell di browser Anda. Screenshot that shows how to launch Cloud Shell in a new window.
Pilih tombol Cloud Shell pada bilah menu di kanan atas di portal Microsoft Azure. Screenshot that shows the Cloud Shell button in the Azure portal

Untuk menjalankan kode dalam artikel ini di Azure Cloud Shell:

  1. Mulai Cloud Shell.

  2. Pilih tombol Salin pada blok kode untuk menyalin kode.

  3. Tempelkan kode tersebut ke sesi Cloud Shell dengan memilih Ctrl+Shift+V untuk Windows dan Linux, atau dengan memilih Cmd+Shift+V untuk macOS.

  4. Pilih Enter untuk menjalankan kode.

Jika Anda memilih untuk memasang dan menggunakan PowerShell secara lokal, tutorial ini memerlukan Az PowerShell 1.4.0 atau yang lebih baru. Jika Anda perlu peningkatan, lihat Instal modul Azure PowerShell. Jika Anda menjalankan PowerShell secara lokal, Anda juga perlu menjalankan Connect-AzAccount untuk membuat koneksi dengan Azure.

Menyalin database ke server baru

# Connect-AzAccount
# The SubscriptionId in which to create these objects
$SubscriptionId = ''
# Set the resource group name and location for your source server
$sourceResourceGroupName = "mySourceResourceGroup-$(Get-Random)"
$sourceResourceGroupLocation = "westus2"
# Set the resource group name and location for your target server
$targetResourceGroupname = "myTargetResourceGroup-$(Get-Random)"
$targetResourceGroupLocation = "eastus"
# Set an admin login and password for your server
$adminSqlLogin = "SqlAdmin"
$password = "ChangeYourAdminPassword1"
# The logical server names have to be unique in the system
$sourceServerName = "source-server-$(Get-Random)"
$targetServerName = "target-server-$(Get-Random)"
# The sample database name
$sourceDatabaseName = "mySampleDatabase"
$targetDatabaseName = "CopyOfMySampleDatabase"
# The ip address range that you want to allow to access your servers
$sourceStartIp = "0.0.0.0"
$sourceEndIp = "0.0.0.0"
$targetStartIp = "0.0.0.0"
$targetEndIp = "0.0.0.0"

# Set subscription 
Set-AzContext -SubscriptionId $subscriptionId 

# Create two new resource groups
$sourceResourceGroup = New-AzResourceGroup -Name $sourceResourceGroupName -Location $sourceResourceGroupLocation
$targetResourceGroup = New-AzResourceGroup -Name $targetResourceGroupname -Location $targetResourceGroupLocation

# Create a server with a system wide unique server name
$sourceResourceGroup = New-AzSqlServer -ResourceGroupName $sourceResourceGroupName `
    -ServerName $sourceServerName `
    -Location $sourceResourceGroupLocation `
    -SqlAdministratorCredentials $(New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $adminSqlLogin, $(ConvertTo-SecureString -String $password -AsPlainText -Force))
$targetResourceGroup = New-AzSqlServer -ResourceGroupName $targetResourceGroupname `
    -ServerName $targetServerName `
    -Location $targetResourceGroupLocation `
    -SqlAdministratorCredentials $(New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $adminSqlLogin, $(ConvertTo-SecureString -String $password -AsPlainText -Force))

# Create a server firewall rule that allows access from the specified IP range
$sourceServerFirewallRule = New-AzSqlServerFirewallRule -ResourceGroupName $sourceResourceGroupName `
    -ServerName $sourceServerName `
    -FirewallRuleName "AllowedIPs" -StartIpAddress $sourcestartip -EndIpAddress $sourceEndIp
$targetServerFirewallRule = New-AzSqlServerFirewallRule -ResourceGroupName $targetResourceGroupname `
    -ServerName $targetServerName `
    -FirewallRuleName "AllowedIPs" -StartIpAddress $targetStartIp -EndIpAddress $targetEndIp

# Create a blank database in the source-server with an S0 performance level
$sourceDatabase = New-AzSqlDatabase  -ResourceGroupName $sourceResourceGroupName `
    -ServerName $sourceServerName `
    -DatabaseName $sourceDatabaseName -RequestedServiceObjectiveName "S0"

# Copy source database to the target server 
$databaseCopy = New-AzSqlDatabaseCopy -ResourceGroupName $sourceResourceGroupName `
    -ServerName $sourceServerName `
    -DatabaseName $sourceDatabaseName `
    -CopyResourceGroupName $targetResourceGroupname `
    -CopyServerName $targetServerName `
    -CopyDatabaseName $targetDatabaseName 

# Clean up deployment 
# Remove-AzResourceGroup -ResourceGroupName $sourceResourceGroupName
# Remove-AzResourceGroup -ResourceGroupName $targetResourceGroupname

Bersihkan penyebaran

Gunakan perintah berikut untuk menghapus grup sumber daya dan semua sumber daya yang terkait dengannya.

Remove-AzResourceGroup -ResourceGroupName $sourceresourcegroupname
Remove-AzResourceGroup -ResourceGroupName $targetresourcegroupname

Penjelasan skrip

Skrip ini menggunakan perintah berikut. Setiap perintah dalam tabel ditautkan ke dokumentasi spesifik perintah.

Perintah Catatan
New-AzResourceGroup Membuat grup sumber daya tempat semua sumber daya disimpan.
Baru-AzSqlServer Membuat server yang menghosting database dan kumpulan elastis.
Baru-AzSqlDatabase Membuat database atau kumpulan elastis.
Baru-AzSqlDatabase Membuat salinan database yang menggunakan rekam jepret saat ini.
Remove-AzResourceGroup Menghapus grup sumber daya termasuk semua sumber daya berlapis.

Langkah berikutnya

Untuk informasi selengkapnya tentang Azure PowerShell, lihat Dokumentasi Microsoft Azure PowerShell.

Sampel skrip SQL Database PowerShell tambahan dapat ditemukan di skrip Azure SQL Database PowerShell.