|
14 | 14 | $dbPassword = 'hispmddb'; |
15 | 15 | $dbName = 'hisp_md'; |
16 | 16 |
|
17 | | -// Docker container name |
18 | | -$containerName = 'hispmd_postgres'; |
19 | | - |
20 | 17 | // Directory to store backups |
21 | 18 | $backupDir = './backup'; |
22 | 19 |
|
|
35 | 32 | exit; |
36 | 33 | } |
37 | 34 |
|
38 | | -// Ensure the database exists; if not, create it |
39 | | -$checkDbCommand = "docker exec -i $containerName sh -c \"PGPASSWORD='$dbPassword' psql -h $dbHost -U $dbUser -tAc 'SELECT 1 FROM pg_database WHERE datname = \"$dbName\"'\""; |
40 | | -exec($checkDbCommand, $output, $return_var); |
41 | | - |
42 | | -// If the database doesn't exist, create it |
43 | | -if ($return_var !== 0 || empty($output)) { |
44 | | - $createDbCommand = "docker exec -i $containerName sh -c \"PGPASSWORD='$dbPassword' psql -h $dbHost -U $dbUser -c 'CREATE DATABASE $dbName;'\""; |
45 | | - exec($createDbCommand, $output, $return_var); |
46 | | - if ($return_var !== 0) { |
47 | | - echo json_encode(['success' => false, 'message' => 'Error creating database. Details: ' . implode("\n", $output)]); |
48 | | - exit; |
49 | | - } |
| 35 | +// Connect to the PostgreSQL database (newly created) |
| 36 | +$connString = "host=$dbHost dbname=$dbName user=$dbUser password=$dbPassword"; |
| 37 | +$conn = pg_connect($connString); |
| 38 | + |
| 39 | +if (!$conn) { |
| 40 | + echo json_encode(['success' => false, 'message' => 'Failed to connect to PostgreSQL database']); |
| 41 | + exit; |
50 | 42 | } |
51 | 43 |
|
52 | | -// Drop all tables and other objects in the public schema (clean restore) |
53 | | -$dropAllCommand = "docker exec -i $containerName sh -c \"PGPASSWORD='$dbPassword' psql -h $dbHost -U $dbUser -d $dbName -c 'DROP SCHEMA IF EXISTS public CASCADE; CREATE SCHEMA public;'\""; |
54 | | -exec($dropAllCommand . ' 2>&1', $output, $return_var); |
| 44 | +// Drop all tables and other objects in the public schema |
| 45 | +$query = "DROP SCHEMA IF EXISTS public CASCADE; CREATE SCHEMA public;"; |
| 46 | +$result = pg_query($conn, $query); |
55 | 47 |
|
56 | | -if ($return_var !== 0) { |
57 | | - echo json_encode(['success' => false, 'message' => 'Error dropping schema. Details: ' . implode("\n", $output)]); |
| 48 | +if (!$result) { |
| 49 | + echo json_encode(['success' => false, 'message' => 'Error dropping schema. Details: ' . pg_last_error($conn)]); |
| 50 | + pg_close($conn); |
58 | 51 | exit; |
59 | 52 | } |
60 | 53 |
|
61 | | -// Copy the backup file to the Docker container |
62 | | -$copyBackupCommand = "docker cp $backupFilePath $containerName:/tmp/$backupFile"; |
63 | | -exec($copyBackupCommand . ' 2>&1', $output, $return_var); |
| 54 | +// Ensure pg_restore command is correct |
| 55 | +// Specify the host explicitly with the -h option |
| 56 | +// Pass the password via PGPASSWORD environment variable |
| 57 | + |
| 58 | +$restoreCommand = "PGPASSWORD='$dbPassword' pg_restore -h $dbHost -U $dbUser -d $dbName -v $backupFilePath"; |
| 59 | + |
| 60 | +// Provide the full path to pg_restore, if necessary |
| 61 | +$fullPathRestoreCommand = "PGPASSWORD='$dbPassword' /usr/bin/pg_restore -h $dbHost -U $dbUser -d $dbName -v $backupFilePath"; // Adjust to where pg_restore is located on your system |
| 62 | + |
| 63 | +// Execute the restore command with shell_exec |
| 64 | +$restoreResult = shell_exec($fullPathRestoreCommand . ' 2>&1'); |
| 65 | + |
| 66 | +// Capture the output for debugging |
| 67 | +if ($restoreResult === null) { |
| 68 | + echo json_encode(['success' => false, 'message' => 'Error executing restore command']); |
| 69 | + pg_close($conn); |
| 70 | + exit; |
| 71 | +} |
64 | 72 |
|
65 | | -if ($return_var !== 0) { |
66 | | - echo json_encode(['success' => false, 'message' => 'Error copying backup file to Docker container. Details: ' . implode("\n", $output)]); |
| 73 | +// Check for errors in the restore process |
| 74 | +if (strpos($restoreResult, 'error') !== false || strpos($restoreResult, 'failed') !== false) { |
| 75 | + echo json_encode(['success' => false, 'message' => 'Restore failed. Details: ' . $restoreResult]); |
| 76 | + pg_close($conn); |
67 | 77 | exit; |
68 | 78 | } |
69 | 79 |
|
70 | | -// Restore the backup |
71 | | -$restoreCommand = "docker exec -i $containerName sh -c \"PGPASSWORD='$dbPassword' pg_restore -h $dbHost -U $dbUser -d $dbName -v /tmp/$backupFile\""; |
72 | | -exec($restoreCommand . ' 2>&1', $output, $return_var); |
| 80 | +echo json_encode(['success' => true, 'message' => "Backup successfully restored: $backupFilePath"]); |
73 | 81 |
|
74 | | -if ($return_var === 0) { |
75 | | - echo json_encode(['success' => true, 'message' => "Backup successfully restored: $backupFilePath"]); |
76 | | -} else { |
77 | | - echo json_encode(['success' => false, 'message' => 'Error restoring backup. Details: ' . implode("\n", $output)]); |
| 82 | +// Close the PostgreSQL connection |
| 83 | +pg_close($conn); |
| 84 | + |
| 85 | + |
| 86 | +// at line 35 |
| 87 | +// we removed the line |
| 88 | +/* |
| 89 | +// Drop the existing database |
| 90 | +$dropDbCommand = "PGPASSWORD='$dbPassword' psql -h $dbHost -U $dbUser -d postgres -c 'DROP DATABASE IF EXISTS $dbName;'"; |
| 91 | +$dropDbOutput = shell_exec($dropDbCommand . ' 2>&1'); |
| 92 | +
|
| 93 | +if (strpos($dropDbOutput, 'ERROR') !== false) { |
| 94 | + echo json_encode(['success' => false, 'message' => 'Error dropping database. Details: ' . $dropDbOutput]); |
| 95 | + exit; |
| 96 | +} |
| 97 | +
|
| 98 | +// Recreate the database |
| 99 | +$createDbCommand = "PGPASSWORD='$dbPassword' psql -h $dbHost -U $dbUser -d postgres -c 'CREATE DATABASE $dbName;'"; |
| 100 | +$createDbOutput = shell_exec($createDbCommand . ' 2>&1'); |
| 101 | +
|
| 102 | +if (strpos($createDbOutput, 'ERROR') !== false) { |
| 103 | + echo json_encode(['success' => false, 'message' => 'Error creating database. Details: ' . $createDbOutput]); |
| 104 | + exit; |
78 | 105 | } |
79 | | -?> |
| 106 | +*/ |
0 commit comments