public static function get(OutputInterface &$output)
 {
     $path = Env::get('BACKUP_DIRECTORY');
     $mysqldump = Env::get('MYSQLDUMP_BIN', '/usr/bin/mysqldump');
     $user = Env::get('MYSQL_USER');
     $pass = Env::get('MYSQL_PASSWORD');
     $host = Env::get('MYSQL_HOST', 'localhost');
     $compression = Env::get('COMPRESS_TYPE', false);
     $databases = Env::get('MYSQL_DATABASES', '*');
     if (!$user) {
         throw new ProcessException("MySQL user should be configured");
     }
     if (!$path) {
         throw new ProcessException("Backup directory should be configured");
     }
     if (!is_dir($path)) {
         throw new ProcessException("Backup directory is wrong or does not exists.");
     }
     $path = rtrim($path, DIRECTORY_SEPARATOR);
     $args = [];
     $file = $path . '/' . date('Y-m-d-His') . '.sql';
     array_push($args, $mysqldump);
     array_push($args, "-h{$host}");
     array_push($args, "-u{$user}");
     if ($pass) {
         array_push($args, "-p{$pass}");
     }
     if ($databases == '*') {
         array_push($args, '--all-databases');
         $output->writeln('<info>Backup of all databases</info>');
     } else {
         array_push($args, "--databases {$databases}");
         $output->writeln("<info>Backup of databases {$databases}</info>");
     }
     if ($compression) {
         $cformat = $compression == 'gzip' ? 'gz' : $compression;
         $output->writeln("<info>Using compression {$cformat}</info>");
         array_push($args, "| {$compression} > {$file}.{$cformat}");
     } else {
         array_push($args, " > {$file}");
     }
     $output->writeln("<info>File created into {$file}</info>");
     // Launch process
     return new Process(implode(' ', $args));
 }
 public static function run()
 {
     $path = Env::get('BACKUP_DIRECTORY');
     $to_keep = Env::get('BACKUPS_TO_KEEP', false);
     if ($to_keep == false) {
         return true;
     }
     if (!$path) {
         throw new ProcessException("Backup directory should be configured");
     }
     if (!is_dir($path)) {
         throw new ProcessException("Backup directory is wrong or does not exists.");
     }
     $path = rtrim($path, DIRECTORY_SEPARATOR);
     $scanned = scandir($path, SCANDIR_SORT_DESCENDING);
     $backups = [];
     $deleted = [];
     $count_files = 0;
     foreach ($scanned as $node) {
         if ($node == '.' || $node == '..') {
             continue;
         }
         if (strpos($node, '.') === 0) {
             continue;
         }
         $nodePath = $path . DIRECTORY_SEPARATOR . $node;
         if (is_dir($nodePath)) {
             continue;
         }
         if ($count_files >= $to_keep) {
             array_push($deleted, $nodePath);
         } else {
             array_push($backups, $nodePath);
         }
         $count_files++;
     }
     if (empty($deleted)) {
         return true;
     }
     // delete old files
     foreach ($deleted as $to_delete) {
         unlink($to_delete);
     }
 }