db_restore_tool.bat 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. @echo off
  2. setlocal enabledelayedexpansion
  3. :: Set database configuration
  4. set DB_NAME=soilgd
  5. set DB_USER=postgres
  6. :: Get project root directory
  7. set SCRIPT_DIR=%~dp0
  8. for %%i in ("%SCRIPT_DIR%..\..\") do set PROJECT_ROOT=%%~fi
  9. set BACKUP_FILE=%PROJECT_ROOT%soilgd.dump
  10. echo =====================================
  11. echo PostgreSQL Database Restore Tool
  12. echo =====================================
  13. echo.
  14. :: Check if backup file exists
  15. if not exist "%BACKUP_FILE%" (
  16. echo Error: Backup file not found: %BACKUP_FILE%
  17. echo Please ensure soilgd.dump exists in project root directory.
  18. echo Project root: %PROJECT_ROOT%
  19. pause
  20. exit /b 1
  21. )
  22. echo Will restore database from backup file:
  23. echo Backup file: %BACKUP_FILE%
  24. echo.
  25. set FULL_BACKUP_PATH=%BACKUP_FILE%
  26. echo =====================================
  27. echo WARNING: This will delete existing database and recreate it!
  28. echo Database: %DB_NAME%
  29. echo Backup file: %FULL_BACKUP_PATH%
  30. echo =====================================
  31. echo.
  32. echo Continue? (Y/N)
  33. set /p CONFIRM=
  34. if /i not "%CONFIRM%"=="Y" (
  35. echo Operation cancelled.
  36. pause
  37. exit /b 0
  38. )
  39. echo.
  40. echo Starting restore process...
  41. :: Step 1: Terminate active connections and drop existing database
  42. echo 1. Terminating active connections to database %DB_NAME%...
  43. psql -U %DB_USER% -d postgres -c "SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname = '%DB_NAME%' AND pid <> pg_backend_pid();" 2>nul
  44. echo 2. Dropping existing database %DB_NAME%...
  45. dropdb -U %DB_USER% %DB_NAME% 2>nul
  46. if %ERRORLEVEL% EQU 0 (
  47. echo Database dropped successfully
  48. ) else (
  49. echo Database may not exist or cannot be dropped (this is usually normal)
  50. )
  51. :: Step 2: Create new empty database
  52. echo 3. Creating new database %DB_NAME%...
  53. createdb -U %DB_USER% %DB_NAME% 2>nul
  54. if %ERRORLEVEL% NEQ 0 (
  55. echo Database creation failed, attempting to force drop and recreate...
  56. :: Force drop database with more aggressive approach
  57. psql -U %DB_USER% -d postgres -c "DROP DATABASE IF EXISTS %DB_NAME%;" 2>nul
  58. :: Try creating again
  59. createdb -U %DB_USER% %DB_NAME%
  60. if %ERRORLEVEL% NEQ 0 (
  61. echo Database creation still failed!
  62. echo Possible causes:
  63. echo 1. PostgreSQL service not running
  64. echo 2. Insufficient user permissions
  65. echo 3. Database is in use by other processes
  66. echo Please check and retry.
  67. pause
  68. exit /b 1
  69. )
  70. )
  71. echo Database created successfully
  72. :: Step 3: Restore data
  73. echo 4. Restoring data from backup file...
  74. pg_restore -U %DB_USER% -d %DB_NAME% "%FULL_BACKUP_PATH%"
  75. if %ERRORLEVEL% EQU 0 (
  76. echo Data restored successfully!
  77. ) else (
  78. echo Data restore failed! Please check:
  79. echo 1. Is backup file corrupted?
  80. echo 2. Is PostgreSQL service running?
  81. echo 3. Are user permissions correct?
  82. pause
  83. exit /b 1
  84. )
  85. echo.
  86. echo =====================================
  87. echo Database restore completed successfully!
  88. echo Database: %DB_NAME%
  89. echo Backup source: %FULL_BACKUP_PATH%
  90. echo =====================================
  91. echo.
  92. pause