old.php


/**
 * Backup your db using mysqldump
 * @arg rel_file_name the relative path to the file name to store the backup in or nothing to use the default
 */
$cli->load_command('backup-db',
    function($cli, $args){
        $err_rep = error_reporting();
        $_SERVER['REQUEST_URI'] = null;

        error_reporting(1|2|4);
        // echo "\nGenerating Sitemap File. Your deliver.php file MUST contain a var named \$server.";


        $_SERVER['REQUEST_URI'] = '/generic-error-page/';
        $_SERVER['HTTP_HOST'] = null;
        $_SERVER['REQUEST_METHOD'] = 'GET';

        $dir = getcwd();
        if (!is_file($dir.'/deliver.php'))$dir = $dir.'/test/Server/';

        ob_start();
        require($dir.'/deliver.php');
        ob_get_clean();

        $file = $args['--'][0] ?? 'db-backup-'.date('y-m-d_h\mi').'.sql';
        $file = getcwd().'/'.$file;

        if (is_dir($file)){
            $file = $file.'/db-backup-'.date('y-m-d_h\mi').'.sql';
        }

        $user = $lia->env->get('mysql.user');
        $password = $lia->env->get('mysql.password');
        $database = $lia->env->get('mysql.dbname');

        $host = $lia->env->get('mysql.host');

        passthru("mysqldump -h $host -u $user -p$password $database > \"$file\"");


    }, "mysqldump and backup your database. Optionally pass relative file name to store the backup in"
);


/**
 * Restore your database from an sql file, presumably from a mysqldump
 * @arg rel_file_name the relative path to the file name or directory. If directory, will prompt for sql file. If nothing, will use current working directory.
 * @arg which_file a numeric index of the file to load
 */
$cli->load_command('restore-db',
    function($cli, $args){
        $err_rep = error_reporting();
        $_SERVER['REQUEST_URI'] = null;

        error_reporting(1|2|4);
        // echo "\nGenerating Sitemap File. Your deliver.php file MUST contain a var named \$server.";


        $_SERVER['REQUEST_URI'] = '/generic-error-page/';
        $_SERVER['HTTP_HOST'] = null;
        $_SERVER['REQUEST_METHOD'] = 'GET';

        $dir = getcwd();
        if (!is_file($dir.'/deliver.php'))$dir = $dir.'/test/Server/';

        ob_start();
        require($dir.'/deliver.php');
        ob_get_clean();

        $dir = $args['--'][0] ?? '';
        $dir = getcwd().'/'.$dir;
        // var_dump($dir);
        // exit;
        if (is_file($dir)){
            // $file = $dir;
            $dir = getcwd();
            $file = $args['--'][0] ?? '';
        } else {
            $list = scandir($dir);
            $list = array_filter($list, function($file){if (substr($file,-4)=='.sql')return true; return false;});

            $list = array_values($list);
            echo "Files:";
            foreach ($list as $index=>$file){
                echo "\n$index: $file";
            }
            echo "\n";

            $answer = $args['--'][1] ?? null;
            if ($answer==null){
                if (!function_exists('readline')){
                    echo "readline() not available. Run the command again passing the relative dir AND the file index to restore from.";
                    return;
                }
                $answer = readline("Enter number of file to restore from: ");
            }
            $file = $list[$answer];
        }

        $lia->pdo->exec(file_get_contents($dir.'/'.$file));


    }, "Restore database from .sql file. Pass relative directory or file path."
);