1
- $ServerName = " Your Source Database Server"
2
- $DestinationServer = " Your Destintination Database Server"
3
- $UserName = " Database User"
4
- $Password = " Database User's Password"
5
- $SourceDatabase = " Database to copy from"
6
- $DestinationDB = " Database to copy to"
7
- $fileName = " ..\Schema.sql" # Path to the Schema.sql file included in this repo
8
- $tempPath = " ..\BCP\" # A temp directory on a drive with enough free space to save the bcp files during the export and import operations
9
- $logPath = " C:\Script_Utility\log\"
1
+ function copy-database {
2
+ param (
3
+ $ServerName = " Localhost\sqlexpress" ,
4
+ $DestinationServer = " localhost\sqlexpress" ,
5
+ $SourceUser = " Database User" ,
6
+ $SourcePassword = " Database User's Password" ,
7
+ $DestinationUser = " Database User" ,
8
+ $DestinationPassword = " Database User's Password" ,
9
+ $SourceDatabase = " websearch" , # name of the databse to copy too
10
+ $DestinationDB = " websearchSchema" , # name of the destination database
11
+ $CopyData = $false , # flag to trigger bulk copy of data to destination database
12
+ $fileName = " .\ExtractDB\Schema.sql" , # Path to the Schema.sql file included in this repo
13
+ $tempPath = " .\BCP\" , # A temp directory on a drive with enough free space to save the bcp files during the export and import operations
14
+ $logPath = " .\log\" # directory to write error logs too
15
+ )
16
+ # Get Schema Data from the database
17
+ $DS = Invoke-Sqlcmd - MaxCharLength 150000 - ServerInstance $ServerName - Database $SourceDatabase - InputFile $fileName -As DataSet
10
18
11
- # Get Schema Data from the database
12
- $DS = Invoke-Sqlcmd - MaxCharLength 150000 - ServerInstance $ServerName - User $UserName - Password $Password - Database $SourceDatabase - InputFile $fileName -As DataSet
19
+ # For each row
20
+ foreach ($sql in $DS.Tables [0 ].Rows){
21
+ Write-Output $sql.TableName
13
22
14
- # For each row
15
- foreach ($sql in $DS.Tables [0 ].Rows){
16
- Write-Output $sql.TableName
23
+ # Create a variable for our log file name
24
+ $errLog = " $ ( $logPath ) $ ( $sql.TableName ) _sqlout.txt"
17
25
18
- # Execute file on each database
19
- Invoke-Sqlcmd - ServerInstance $DestinationServer - Database $DestinationDB - Query $sql.SqlStatement - OutputSqlErrors $true - verbose
20
-
21
- if ($error.count -gt 0 )
22
- {
23
- $error | Out-File - FilePath " $ ( $logPath ) $ ( $sql.TableName ) _sqlout.txt"
24
- $sql.SqlStatement | Out-File - FilePath " $ ( $logPath ) $ ( $sql.TableName ) _sqlout.txt"
25
-
26
- $error.Clear ()
27
- }
28
- Write-Output $sql.SqlStatement
26
+ copy-schema - Sql $sql.SqlStatement - DestinationDatabase $DestinationDB - DestinationServer $DestinationServer - ErrorFile $errLog
29
27
30
- # If we just created the table, let's import the data before applying an constraints or indexes
31
- if ($sql.ScriptType -eq " Table" )
32
- {
33
- # add code to create format file
34
- $bcp = " bcp $ ( $sql.SchemaName ) .$ ( $sql.TableName ) format nul -n -f $ ( $tempPath ) $ ( $sql.TableName ) .fmt -S $ ( $ServerName ) -d $ ( $SourceDatabase ) -U $ ( $UserName ) -P $ ( $Password ) -e $ ( $logPath ) $ ( $sql.TableName ) _fmt_err.txt"
35
- Invoke-Expression $bcp
36
- Write-Output $bcp
28
+ # If we just created the table, let's import the data before applying an constraints or indexes
29
+ if ($sql.ScriptType -eq " Table" -and $CopyData -eq $true ){
30
+ copy-data - SchemaName $sql.SchemaName - TableName $sql.TableName - SourceServer $ServerName - DestinationServer $DestinationServer - SourceDatabase $SourceDatabase - DestinationDatabase $DestinationDB - WorkingFolder $tempPath
37
31
38
- $bcp = " bcp $ ( $sql .SchemaName ) . $ ( $sql .TableName ) out $ ( $tempPath ) $ ( $sql .TableName ) .bcp -S $ ( $ServerName ) -d $ ( $SourceDatabase ) -U $ ( $UserName ) -P $ ( $Password ) -E -n -t -e $ ( $logPath ) $ ( $sql .TableName ) _out_err.txt "
39
- Invoke-Expression $ bcp
40
- Write-Output $bcp
32
+ # Remove temp data file before moving on
33
+ remove-item - path " $ ( $tempPath ) $ ( $sql .TableName ) . bcp"
34
+ remove-item - path " $ ( $tempPath ) $ ( $sql .TableName ) .fmt "
41
35
42
- # add code to import in to use format file
43
- $bcp = " bcp $ ( $sql.SchemaName ) .$ ( $sql.TableName ) in $ ( $tempPath ) $ ( $sql.TableName ) .bcp -b 100 -S $ ( $DestinationServer ) -d $ ( $DestinationDB ) -T -t -E -f $ ( $tempPath ) $ ( $sql.TableName ) .fmt -e $ ( $logPath ) $ ( $sql.TableName ) _in_err.txt"
44
- Invoke-Expression $bcp
45
- Write-Output $bcp
36
+ if ($error.count -gt 0 ){
37
+ $error | Out-File - FilePath " $ ( $errLog ) "
38
+ $sql.SqlStatement | Out-File - Append - FilePath " $ ( $errLog ) "
46
39
47
- # Remove temp data file before moving on
48
- remove-item - path " $ ( $tempPath ) $ ( $sql.TableName ) .bcp"
49
- remove-item - path " $ ( $tempPath ) $ ( $sql.TableName ) .fmt"
50
-
51
- # Clean up Logs
52
- if ((Get-Item " $ ( $logPath ) $ ( $sql.TableName ) _out_err.txt" ).Length -eq 0 ){
53
- Remove-Item " $ ( $logPath ) $ ( $sql.TableName ) _out_err.txt"
40
+ $error.Clear ()
41
+ }
54
42
}
43
+ }
44
+ }
55
45
56
- if ((Get-Item " $ ( $logPath ) $ ( $sql.TableName ) _in_err.txt" ).Length -eq 0 ){
57
- Remove-Item " $ ( $logPath ) $ ( $sql.TableName ) _in_err.txt"
46
+ function copy-schema {
47
+ param (
48
+ [string ] $Sql ,
49
+ [string ] $DestinationDatabase ,
50
+ [string ] $DestinationServer ,
51
+ [string ] $ErrorFile
52
+ )
53
+ # Execute file on destination database
54
+ Invoke-Sqlcmd - ServerInstance $DestinationServer - Database $DestinationDB - Query $Sql - OutputSqlErrors $true - verbose
55
+ if ($error.count -gt 0 )
56
+ {
57
+ $error | Out-File - FilePath " $ ( $ErrorFile ) "
58
+ $sql.SqlStatement | Out-File - Append - FilePath " $ ( $ErrorFile ) "
59
+
60
+ $error.Clear ()
58
61
}
62
+ Write-Output $Sql
63
+ }
59
64
60
- if ((Get-Item " $ ( $logPath ) $ ( $sql.TableName ) _fmt_err.txt" ).Length -eq 0 ){
61
- Remove-Item " $ ( $logPath ) $ ( $sql.TableName ) _fmt_err.txt"
62
- }
65
+ # TODO: Breakup into extract and import
66
+ function copy-data {
67
+ param (
68
+ [string ] $SchemaName ,
69
+ [string ] $TableName ,
70
+ [string ] $SourceDatabase ,
71
+ [string ] $DestinationDatabase ,
72
+ [string ] $SourceServer ,
73
+ [string ] $DestinationServer ,
74
+ [string ] $WorkingFolder ,
75
+ [string ] $SourceUser ,
76
+ [string ] $DestinationUser ,
77
+ [string ] $SourcePassword ,
78
+ [string ] $DestinationPassword
79
+ )
80
+
81
+ $sourceAuth = " -T "
82
+ $destAuth = " -T "
83
+
84
+ if ($SourceUser -ne " " -and $SourcePassword -ne " " ){
85
+ $sourceAuth = " -U $ ( $SourceUser ) -P $ ( $SourcePassword ) "
86
+ }
87
+
88
+ if ($DestinationUser -ne " " -and $DestinationPassword -ne " " ){
89
+ $destAuth = " -U $ ( $DestinationUser ) -P $ ( $DestinationPassword ) "
63
90
}
64
- }
91
+
92
+ # add code to create format file
93
+ $bcp = " bcp $ ( $SchemaName ) .$ ( $TableName ) format nul -n -f $ ( $WorkingFolder ) $ ( $TableName ) .fmt -S $ ( $SourceServer ) -d $ ( $SourceDatabase ) $ ( $sourceAuth ) "
94
+ Invoke-Expression $bcp
95
+ Write-Output $bcp
96
+
97
+ $bcp = " bcp $ ( $SchemaName ) .$ ( $TableName ) out $ ( $WorkingFolder ) $ ( $TableName ) .bcp -b 100 -S $ ( $SourceServer ) -d $ ( $SourceDatabase ) $ ( $sourceAuth ) -E -n -t "
98
+ Invoke-Expression $bcp
99
+ Write-Output $bcp
100
+
101
+ # add code to import in to use format file
102
+ $bcp = " bcp $ ( $SchemaName ) .$ ( $TableName ) in $ ( $WorkingFolder ) $ ( $TableName ) .bcp -b 100 -S $ ( $DestinationServer ) -d $ ( $DestinationDatabase ) $ ( $destAuth ) -t -E -f $ ( $WorkingFolder ) $ ( $TableName ) .fmt"
103
+ Invoke-Expression $bcp
104
+ Write-Output $bcp
105
+ }
0 commit comments