вторник, 30 ноября 2021 г.

Последняя версия скрипта для бекапа 1С # PowerShell.exe -ExecutionPolicy Bypass -File C:\!Scripts\1c_pg_backup.ps1 # powershell -windowstyle minimized -c "powershell -c C:\!Scripts\1c_pg_backup.ps1 -verbose >> C:\!Scripts\1c_pg_backup.log *>&1" [CmdletBinding()] param( [String]$dbname="", [String]$dumpdir= 'E:\\tmp\\backup', [String]$PG_DIR='C:\Program Files\PostgreSQL 1C\12\', [String]$PGDUMP="$PG_DIR\bin\pg_dump.exe", [String]$PSQL="$PG_DIR\bin\psql.exe", [String]$dbserver='localhost', [String]$dbuser='postgres', [String]$dbpassword='Passw0rD', [String]$separateconfig="" ) Write-Verbose -message dbname=$dbname Write-Verbose -message dumpdir=$dumpdir Write-Verbose -message PGDUMP=$PGDUMP Write-Verbose -message PSQL=$PSQL Write-Verbose -message dbserver=$dbserver Write-Verbose -message dbuser=$dbuser Write-Verbose -message dbpassword=$dbpassword Write-Verbose -message datestamp=$datestamp $excludedb="('template0','template1','postgres')" # $excludedb="('template0','template1','postgres','atis-buh-new','atis-doc','mon-buh','mon-zup','ml-buh','ml-zup')" $sql_listdb="select datname from pg_database where not datname in $excludedb order by datname;" $env:PGPASSWORD = $dbpassword $datestamp = Get-Date -UFormat "%Y%m%d-%H%M%S" $alldbdir = "$dumpdir\\pgsql" $IPCUser = "Administrator" # Authentication $IPCPwd = "media!!2004" # for IPC$ share $DstHost = "backupserver" # robocopy dest $DstDir = "D$\1C\pgsql" # robocopy dest $SrcDir = "E:\tmp\backup\pgsql" # robocopy source $LogPath = "C:\!Scripts\1c_rbcp.log" # robocopy log $Params = "/E" # Robocopy params Write-Verbose -message "Проверяем указана ли база данных для копирования" if ($dbname -eq "") { Write-Verbose -message "Проверяем, существует ли папка $alldbdir" if(!(Test-Path -Path $alldbdir )){ Write-Verbose -message "Cоздаём папку $dumpdir" New-Item -ItemType directory -Path $alldbdir | Out-Null } else { Write-Verbose -message "Удаляем папку с дампами" Remove-Item -path $alldbdir -Force -Recurse | Out-Null New-Item -ItemType directory -Path $alldbdir | Out-Null } Write-Verbose -message "Делаем дамп каждой БД кроме $excludedb" $dblist = &$PSQL -t -h $dbserver -U $dbuser -c $sql_listdb foreach($db in $dblist) { $db = $db.TrimStart() if ($db -ne "") { $dbname=$db Write-Verbose -message "Делаем дамп БД $dbname" # &$PGDUMP -h $dbserver -U $dbuser --dbname=$dbname --file=$alldbdir\$($dbname)__$datestamp.sql.gz --compress=1 &$PGDUMP -Fc -b -h $dbserver -U $dbuser --dbname=$dbname --file=$alldbdir\$($dbname)__$datestamp.backup } Write-Verbose -message "Создание дампов завершено" Write-Verbose -message "Начинаем копирование дампов robocopy" Write-Verbose -message "Авторизуемся на $DstHost" NET USE \\$DstHost\ipc$ /U:"$IPCUser" $IPCPwd Write-Verbose -message "Копируем $SrcDir на \\$DstHost\$DstDir" robocopy.exe /NP $SrcDir \\$DstHost\$DstDir\ $Params /UNILOG:$LogPath Write-Verbose -message "Отключаемся от $DstHost" NET USE \\$DstHost\ipc$ /D Write-Verbose -message "Выполнение завершено" } else { if(!(Test-Path -Path $dumpdir\$dbname )){ Write-Verbose -message "Cоздаём папку $dumpdir\$dbname" New-Item -ItemType directory -Path $dumpdir\$dbname | Out-Null } else { Write-Verbose -message "Удаляем папку с дампами" Remove-Item -path $dumpdir\$dbname -Force -Recurse | Out-Null New-Item -ItemType directory -Path $dumpdir\$dbname | Out-Null } Write-Verbose -message "Добавляем разрешения для Network Service" $acl = Get-Acl "$dumpdir\$dbname" $ar = New-Object System.Security.AccessControl.FileSystemAccessRule("NT AUTHORITY\NETWORK SERVICE","FullControl","Allow") $acl.SetAccessRule($ar) $acl | Set-Acl "$dumpdir\$dbname" if ($separateconfig -eq "" ) { Write-Verbose -message "Делаем дамп указанной БД $dbname в $dumpdir\\$($dbname)__$datestamp.sql.gz" #&$PGDUMP -h $dbserver -U $dbuser --dbname=$dbname --file=$dumpdir\\$($dbname)\\$($dbname)__$datestamp.sql.gz --compress=1 &$PGDUMP -Fc -b -h $dbserver -U $dbuser --dbname=$dbname --file=$dumpdir\\$($dbname)\\$($dbname)__$datestamp.backup } else { # Write-Verbose -message "$PGDUMP -h $dbserver -U $dbuser --dbname=$dbname --file=$dumpdir\$dbname\$dbname-config-scheme__$datestamp.sql -s -t config" Write-Verbose -message "Делаем дамп схемы $dbname таблицы config scheme $dumpdir\$dbname\$dbname-config-scheme__$datestamp.sql" &$PGDUMP -h $dbserver -U $dbuser --dbname=$dbname --file=$dumpdir\$dbname\$dbname-config-scheme__$datestamp.sql -s -t config #Write-Verbose -message "$PSQL -h $dbserver -U $dbuser --dbname=$dbname -c ""COPY public.config TO '$dumpdir\\$dbname\\$dbname-config-datafile__$datestamp.bin' WITH BINARY;""" Write-Verbose -message "Делаем бинарный дамп $dbname таблицы config binary $dumpdir\\$dbname\\$dbname-config-datafile__$datestamp.bin" &$PSQL -h $dbserver -U $dbuser --dbname=$dbname -c "COPY public.config TO '$dumpdir\\$dbname\\$dbname-config-datafile__$datestamp.bin' WITH BINARY;" # Write-Verbose -message "$dumpdir\$($dbname)__$datestamp.sql.gz -T config --compress=1" Write-Verbose -message "Делаем частичный дамп $dbname без config $dumpdir\$dbname\$($dbname)__$datestamp.sql.gz" &$PGDUMP -h $dbserver -U $dbuser --dbname=$dbname --file=$dumpdir\$dbname\$($dbname)__$datestamp.sql.gz -T config --compress=1 } }

Комментариев нет:

Отправить комментарий