Skip to content

Commit 2e58505

Browse files
updated main db api for restore steps
1 parent 999a175 commit 2e58505

1 file changed

Lines changed: 60 additions & 33 deletions

File tree

api/database/restore.php

Lines changed: 60 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@
1414
$dbPassword = 'hispmddb';
1515
$dbName = 'hisp_md';
1616

17-
// Docker container name
18-
$containerName = 'hispmd_postgres';
19-
2017
// Directory to store backups
2118
$backupDir = './backup';
2219

@@ -35,45 +32,75 @@
3532
exit;
3633
}
3734

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;
5042
}
5143

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);
5547

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);
5851
exit;
5952
}
6053

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+
}
6472

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);
6777
exit;
6878
}
6979

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"]);
7381

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;
78105
}
79-
?>
106+
*/

0 commit comments

Comments
 (0)