3

I am using SQL Server 2022 Express on Ubuntu Server 22.04, and PHP 8.3. I am trying to backup a database, the instruction is: BACKUP DATABASE WEBDEV TO DISK = '/temp/WEBDEV.bak', and when I execute it from Management Studio, it creates the file correctly, but when I execute it from PHP it does not show any error, but the file is not created. I already make the /temp/ directory with 777 permission, and the result is the same, It does not create the backup file and it does not show any error. My code is this:

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

$conn = new PDO("sqlsrv:server=localhost;database=WEBDEV;TrustServerCertificate=true", "bkuser", "thepassword");
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "BACKUP DATABASE WEBDEV TO DISK = '/temp/WEBDEV.bak'";
try {
    $conn->exec($sql);
    echo "Backup completed successfully";
} catch (PDOException $e) {
    echo "Error: " . $e->getMessage();
}

It does not show any error message, it shows the "Backup completed successfully" message. The SQL Server error log shows this:

BACKUP failed to complete the command BACKUP DATABASE WEBDEV. Check the backup application log for detailed messages.

But there is no backup log anywhere.

I checked access for the SQL Server user to the directory, and as I said, from Management Studio it creates correctly. That is why I tried with 777 permissions to be sure about the access to the directory.

Any ideas?

3
  • BACKUP DATABASE commands cannot be executed inside a transaction context. What's the setting of PDO::ATTR_AUTOCOMMIT on your connection? Commented Dec 27, 2024 at 0:38
  • 1
    My own experience in a similar situation is summarized here. Commented Dec 27, 2024 at 6:31
  • "But there is no backup log anywhere." You can access the error log via SSMS, or in /var/opt/mssql/log. Commented Dec 27, 2024 at 10:39

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.