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.

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

//taking database backup
echo "Taking database backup \n";
if(isset($envData['db'])){
    $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;'; 
    exec($mysqlDump);
}

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

die('finished');
?>

Leave a Reply