# .\1c_pg_restore.ps1 -verbose -dbname buh-new-20211130-1129 -dumpfile e:\tmp\buh-new__20211130-112901.backup
[CmdletBinding()]
param(
[String]$dbname="",
[String]$dumpfile="",
[String]$PG_DIR='C:\Program Files\PostgreSQL 1C\12\',
[String]$CREATEDB="$PG_DIR\bin\createdb.exe",
[String]$PGRESTORE="$PG_DIR\bin\pg_restore.exe",
[String]$PSQL="$PG_DIR\bin\psql.exe",
[String]$dbserver='localhost',
[String]$dbuser='postgres',
[String]$dbpassword='password'
)
Write-Verbose -message dbname=$dbname
Write-Verbose -message CREATEDB=$CREATEDB
Write-Verbose -message PGRESTORE=$PGRESTORE
Write-Verbose -message PSQL=$PSQL
Write-Verbose -message dbserver=$dbserver
Write-Verbose -message dbuser=$dbuser
Write-Verbose -message dbpassword=$dbpassword
$env:PGPASSWORD = $dbpassword
$env:PGUSER = $dbuser
$env:PGHOST = $dbserver
Write-Verbose -message "Check -dbname is set $dbname"
if ($dbname -eq "") {
Write-Host "-dbname not set"
exit
}
Write-Verbose -message "Check -dumpfile is set $dumpfile"
if ($dumpfile -eq "") {
Write-Host "-dumpfile not set "
exit
} else {
Write-Verbose -message "Check existance $dumpfile"
if (!(Test-Path $dumpfile)) {
Write-Host $dumpfile does not exist
exit
}
}
Write-Verbose -message "Check existance $dbname"
# $dblist = &$PSQL --host=$dbserver --username=$dbuser --dbname $dbname -c "\q" 2>&1
$dblist = &$PSQL --dbname $dbname -c "\q" 2>&1
if ($LastExitCode -eq 0) {
Write-Host "$dbname alredy exists, exiting"
exit
}
Write-Verbose -message "$dbname Creating db"
$createdbparams = "--encoding=utf8 --template=template1 --locale=ru_RU.UTF-8 --lc-collate=ru_RU.UTF-8 --lc-ctype=ru_RU.UTF-8 --encoding=UTF8"
$result = & $CREATEDB $dbname $createdbparams
if ($LastExitCode -ne 0) {
Write-Host "$dbname can not create, exiting"
exit
}
Write-Verbose -message "$dbname restoring db $PGRESTORE -d $dbname"
$result = & $PGRESTORE --dbname=$dbname $dumpfile 2>&1
if ($LastExitCode -ne 0) {
Write-Host "$dbname not restored, exiting"
exit
}
Write-Verbose -message "$dumpfile restored to $dbname"
[CmdletBinding()]
param(
[String]$dbname="",
[String]$dumpfile="",
[String]$PG_DIR='C:\Program Files\PostgreSQL 1C\12\',
[String]$CREATEDB="$PG_DIR\bin\createdb.exe",
[String]$PGRESTORE="$PG_DIR\bin\pg_restore.exe",
[String]$PSQL="$PG_DIR\bin\psql.exe",
[String]$dbserver='localhost',
[String]$dbuser='postgres',
[String]$dbpassword='password'
)
Write-Verbose -message dbname=$dbname
Write-Verbose -message CREATEDB=$CREATEDB
Write-Verbose -message PGRESTORE=$PGRESTORE
Write-Verbose -message PSQL=$PSQL
Write-Verbose -message dbserver=$dbserver
Write-Verbose -message dbuser=$dbuser
Write-Verbose -message dbpassword=$dbpassword
$env:PGPASSWORD = $dbpassword
$env:PGUSER = $dbuser
$env:PGHOST = $dbserver
Write-Verbose -message "Check -dbname is set $dbname"
if ($dbname -eq "") {
Write-Host "-dbname not set"
exit
}
Write-Verbose -message "Check -dumpfile is set $dumpfile"
if ($dumpfile -eq "") {
Write-Host "-dumpfile not set "
exit
} else {
Write-Verbose -message "Check existance $dumpfile"
if (!(Test-Path $dumpfile)) {
Write-Host $dumpfile does not exist
exit
}
}
Write-Verbose -message "Check existance $dbname"
# $dblist = &$PSQL --host=$dbserver --username=$dbuser --dbname $dbname -c "\q" 2>&1
$dblist = &$PSQL --dbname $dbname -c "\q" 2>&1
if ($LastExitCode -eq 0) {
Write-Host "$dbname alredy exists, exiting"
exit
}
Write-Verbose -message "$dbname Creating db"
$createdbparams = "--encoding=utf8 --template=template1 --locale=ru_RU.UTF-8 --lc-collate=ru_RU.UTF-8 --lc-ctype=ru_RU.UTF-8 --encoding=UTF8"
$result = & $CREATEDB $dbname $createdbparams
if ($LastExitCode -ne 0) {
Write-Host "$dbname can not create, exiting"
exit
}
Write-Verbose -message "$dbname restoring db $PGRESTORE -d $dbname"
$result = & $PGRESTORE --dbname=$dbname $dumpfile 2>&1
if ($LastExitCode -ne 0) {
Write-Host "$dbname not restored, exiting"
exit
}
Write-Verbose -message "$dumpfile restored to $dbname"
Ратнер Арсений, arsenyratner@gmail.com, 7 985 273 2090
Комментариев нет:
Отправить комментарий