Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

caito's avatar
Level 1

Databse backup in Laravel without third-party libraries

I new to Laravel. I've been trying to create a controller that backups tables in the form of backup_date_.sql format without using any third-party library at all, but I'm getting frustrated. I've searched and I found some code examples. I've tried to use them within my BackupsController, but things are getting more and more difficult. The tables are loaded correctly from the database, and displayed in the view. Currently, I have four tables: migrations, password_resets, users and posts. So, need to store the variable $output in a file, so when I click backup button, the file is automatically stored locally. How can I do that, please? Thanks in advance.

0 likes
6 replies
caito's avatar
Level 1

BishoyWagih, I need to backup without any library. Just pure PHP and MySQL.

Cronix's avatar

At its simplest

exec('mysqldump dbName --user=dbUser --password=dbPassword > /path/to/save/fileName.sql', $errors);

if ( ! empty($errors)) {
    // there was a problem.
}

It just uses mysqldump to dump all tables in the database with dbName into a file at /path/to/save/fileName.sql.

I'd never do it this way, but it meets your requirements.

Snapey's avatar

Just pure PHP and MySQL

So why are you trying to do it in Laravel?

caito's avatar
Level 1

Cronix, I do not need to use exec because of the configuration of web servers which are different, and some of them banned commands such as system or exec. I made a controller and the file is downloaded, but some user informations are losts. So, I need a way to store all infos comming from database in a file. Please check my code if you do not mind: https://github.com/mestre-c/laravel_projects_2018/tree/master/laravelBackup. Thanks in advance.

Please or to participate in this conversation.