Magento 2 backup script for developer

Magento 2 developer usually require taking backup of live environment and setup on local or creating staging environment. We need to take backup of Database, App folder and composer.json minimal. We can add media and other assets for full fledged setup. I created a simple script to create backup of these two folder after reading env.php file. It will decrease time to take backup. We just need to put these script at root and run it. It is very flexible and you can make changes to your requirement as well. I will keep updating it and will add new features.

//create backup file
$envData = include('app/etc/env.php');

//taking database backup
echo "Taking database backup \n";
    $mysqlDump = 'mysqldump --user='.$envData['db']['connection']['default']['username'].' --password='.$envData['db']['connection']['default']['password'].' --host='.$envData['db']['connection']['default']['host'] .' '.$envData['db']['connection']['default']['dbname'] .' > db.sql;'; 

//zipping files
echo "Zipping required files to setup environment \n";
exec('zip -r db.sql composer.json app/');


Leave a Reply