Copy-SqlServerDatabase
SYNOPSIS
Perform a backup and restore of a SQL Server database with user and permission management.
SYNTAX
Copy-SqlServerDatabase [-SourceServerName] <String> [-DestinationServerName] <String> [-SourceDBName] <String>
[-DestinationDBName] <String> [[-BackupFile] <String>] [[-force] <Boolean>] [<CommonParameters>]
DESCRIPTION
This function performs a backup of a SQL Server database to a file server, restores it to a destination server, optionally drops existing users, and applies standard permissions to the new database. It supports overwriting existing databases and backup files if the `-Force` parameter is specified.
EXAMPLES
EXAMPLE 1
Copy-SqlServerDatabase -SourceServerName "SQLSource" -DestinationServerName "SQLDestination" -SourceDBName "TestDB" -DestinationDBName "TestDB_Copy"
Performs a backup of "TestDB" from "SQLSource" and restores it as "TestDB_Copy" on "SQLDestination".
EXAMPLE 2
Copy-SqlServerDatabase -SourceServerName "SQLSource" -DestinationServerName "SQLDestination" -SourceDBName "TestDB" -DestinationDBName "TestDB_Copy" -Force
Performs a backup and restore of "TestDB" with overwriting enabled for existing databases and backup files.
EXAMPLE 3
Copy-SqlServerDatabase -SourceServerName "SQLSource" -DestinationServerName "SQLDestination" -SourceDBName "TestDB" -DestinationDBName "TestDB_Copy" -BackupFile "\\server\path\TestDB.bak"
Uses a custom backup file path for the operation.
PARAMETERS
-SourceServerName
The name of the SQL Server instance from which the database will be backed up.
Type: String
Parameter Sets: (All)
Aliases:
Required: True
Position: 1
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
-DestinationServerName
The name of the SQL Server instance to which the database will be restored.
Type: String
Parameter Sets: (All)
Aliases:
Required: True
Position: 2
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
-SourceDBName
The name of the source database to be backed up.
Type: String
Parameter Sets: (All)
Aliases:
Required: True
Position: 3
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
-DestinationDBName
The name of the destination database. MDF/LDF files will be renamed to match this name.
Type: String
Parameter Sets: (All)
Aliases:
Required: True
Position: 4
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
-BackupFile
The path to the backup file. If not specified, a default UNC path will be used. Ensure proper permissions are set on the path.
Type: String
Parameter Sets: (All)
Aliases:
Required: False
Position: 5
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
-force
If specified, overwrites the existing database and backup file if they already exist.
Type: Boolean
Parameter Sets: (All)
Aliases:
Required: False
Position: 6
Default value: False
Accept pipeline input: False
Accept wildcard characters: False
CommonParameters
This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see about_CommonParameters.