restore-financialdb.ps1 748 B

123456789101112131415161718192021222324252627
  1. param (
  2. [string]$backup_dir = $(throw "请提供备份目录作为第一个参数")
  3. )
  4. # 设置环境变量
  5. $env:PGPASSWORD = 'postgres'
  6. $host_ = '127.0.0.1'
  7. $port = '5432'
  8. $username = 'postgres'
  9. $dbname = 'financialdb'
  10. $jobs = '16'
  11. # 检查数据库是否存在
  12. $checkDbExists = psql -U $username -tA -c "SELECT 1 FROM pg_database WHERE datname='$dbname'" | Out-String
  13. if ($checkDbExists.Trim() -ne "1") {
  14. Write-Output "数据库 $dbname 不存在,正在创建..."
  15. createdb -U $username $dbname
  16. } else {
  17. Write-Output "数据库 $dbname 已存在。"
  18. }
  19. # 执行 pg_restore
  20. Write-Output "开始从 $backup_dir 恢复数据库 $dbname..."
  21. pg_restore -O -c -v -h $host_ -p $port -U $username -d $dbname -Fd -j $jobs $backup_dir