-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackup-db.php
38 lines (32 loc) · 1.13 KB
/
backup-db.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<?php
// Set the maximum execution time and memory limit to prevent timeouts
set_time_limit(0);
ini_set('memory_limit', '-1');
// Include the Moodle config.php file to get database connection details
require_once('config.php');
// Define the path where the dump file will be saved
$backupDir = $CFG->dataroot . '/backup/';
$backupFile = $backupDir . 'moodle_db_backup_' . date('Y-m-d_H-i-s') . '.sql.gz';
// Ensure the backup directory exists
if (!file_exists($backupDir)) {
mkdir($backupDir, 0777, true);
}
// Construct the mysqldump command
$mysqldumpCmd = sprintf(
'mysqldump --user=%s --password=%s --host=%s %s | gzip > %s',
escapeshellarg($CFG->dbuser),
escapeshellarg($CFG->dbpass),
escapeshellarg($CFG->dbhost),
escapeshellarg($CFG->dbname),
escapeshellarg($backupFile)
);
// Execute the command
exec($mysqldumpCmd, $output, $returnVar);
// Check if the dump was successful
if ($returnVar === 0) {
echo "Database dump was successful. The backup file is located at: $backupFile\n";
} else {
echo "Error: Database dump failed. Return code: $returnVar\n";
echo "Output: " . implode("\n", $output) . "\n";
}
?>