-
Notifications
You must be signed in to change notification settings - Fork 63
/
ScriptReplication.ps1
69 lines (53 loc) · 2.69 KB
/
ScriptReplication.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# ScriptReplication.ps1
cls
function Backup-SMCReplication
{
param ([string ] $sqlServer
, [string] $outputDirectory
)
Import-Module Repl
[string] $path = "$outputDirectory\$((get-date).toString('yyyy-MMM-dd'))\"
if ( -not (test-path $path)) {
New-Item $path -ItemType Directory | Out-Null
}
foreach($publication in Get-ReplPublication $sqlServer)
{
[string] $fileName = "{0}{1}.sql" -f $path,$publication.DatabaseName.Replace(" ", "")
$fileName = "{0}Create_{1}_{2}.sql" -f $path,$publication.DatabaseName.Replace(" ", ""),$publication.Name.Replace(" ", "")
$publication.Script([Microsoft.SqlServer.Replication.scriptoptions]::Creation `
-bor [Microsoft.SqlServer.Replication.scriptoptions]::EnableReplicationDB `
-bor [Microsoft.SqlServer.Replication.scriptoptions]::IncludeCreateLogreaderAgent `
-bor [Microsoft.SqlServer.Replication.scriptoptions]::IncludeCreateQueuereaderAgent `
-bor [Microsoft.SqlServer.Replication.scriptoptions]::IncludePublicationAccesses `
-bor [Microsoft.SqlServer.Replication.scriptoptions]::IncludeCreateSnapshotAgent `
-bor [Microsoft.SqlServer.Replication.scriptoptions]::IncludeArticles `
-bor [Microsoft.SqlServer.Replication.scriptoptions]::IncludePublisherSideSubscriptions `
-bor [Microsoft.SqlServer.Replication.scriptoptions]::IncludeSubscriberSideSubscriptions
) | Out-File $fileName
}
}
function Delete-SMCReplication()
{
param ([string ] $sqlServer
, [string] $outputDirectory
)
Import-Module Repl
[string] $path = "$outputDirectory\$((get-date).toString('yyyy-MMM-dd'))\"
if ( -not (test-path $path)) {
New-Item $path -ItemType Directory | Out-Null
}
foreach($publication in Get-ReplPublication $sqlServer)
{
[string] $fileName = "{0}{1}.sql" -f $path,$publication.DatabaseName.Replace(" ", "")
$fileName = "{0}Delete_{1}_{2}.sql" -f $path,$publication.DatabaseName.Replace(" ", ""),$publication.Name.Replace(" ", "")
$publication.Script([Microsoft.SqlServer.Replication.scriptoptions]::Deletion `
-bor [Microsoft.SqlServer.Replication.scriptoptions]::IncludeDisableReplicationDB `
-bor [Microsoft.SqlServer.Replication.scriptoptions]::IncludePublisherSideSubscriptions `
-bor [Microsoft.SqlServer.Replication.scriptoptions]::IncludeSubscriberSideSubscriptions `
-bor [Microsoft.SqlServer.Replication.scriptoptions]::IncludeArticles `
-bor [Microsoft.SqlServer.Replication.scriptoptions]::IncludeReplicationJobs
) | Out-File $fileName
}
}
Backup-SMCReplication -sqlServer PSQLDLS30 -outputDirectory e:\Dexma\logs
Delete-SMCReplication -sqlServer PSQLDLS30 -outputDirectory e:\Dexma\logs