ThemeFuse / Unyson-Backups-Extension

Backup & Demo Content - This extension lets you create an automated backup schedule, import demo content or even create a demo content archive for migration purposes.
http://manual.unyson.io/en/latest/extension/backups/
10 stars 17 forks source link

Export custom folder #44

Closed spatricius closed 7 years ago

spatricius commented 7 years ago

Hi,

It seems one of plugins I use keeps all image content in a custom folder outside of wp_upload_dir, say wp-content/uploads/plugin_slug_blog_id/ while wp_upload_dir is wp-content/uploads/sites/3/

While I don't think this is a good practice, I have no way of changing it. So my question is: How can I export files from ( and import to ) this custom folder using Backups Extension?

Perhaps I could extend \_FW_Ext_Backups_Module_Tasks::add_backup_tasks() and change $dirs there. Would that work? Is there a more elegant way?

Thanks! P.

spatricius commented 7 years ago

Hi again @GheorgheP ,

Thanks for the commit, I appreciate it.

However, I think it does not solve my problem. As mentioned before, I have two folders. And I need to export actually both of them:

wp-content/uploads/plugin_slug_blog_id/
wp-content/uploads/sites/3/

so if I set one global path in $cfg, then I can export only that one. Am I right?

My thoughs are I could extend

FW_Ext_Backups_Task_Type_Files_Export
FW_Ext_Backups_Task_Type_Files_Restore

and hardcode paths there, then register them with:

fw_ext_backups_task_types_register

Do you think that would do the trick?

spatricius commented 7 years ago

Well, here's my workaround if anybody needs it. It's rather blunt. Get the job done though.

add_action( 'fw:ext:backups:add-backup-tasks', '_action_theme_fw_ext_backups_add_custom_dirs_backup_task', 10, 2 );
function _action_theme_fw_ext_backups_add_custom_dirs_backup_task( FW_Ext_Backups_Task_Collection $collection, array $dirs ) {

    // no need for custom content export in single site
    if ( ! is_multisite() ) return;

    $id_prefix = 'backup:';
    $full      = (bool) $dirs['is_full'];
    $tmp_dir   = fw_ext( 'backups' )->get_tmp_dir();
    $dirs      = $dirs['dirs'] + array(
        'dest_folder_1' => 'source_path_1',
        'dest_folder_2' => 'source_path_2',
    );

    $collection->empty_collection();

    $collection->add_task(new FW_Ext_Backups_Task(
        $id_prefix .'tmp-dir-clean:before',
        'dir-clean',
        array(
            'dir' => $tmp_dir
        )
    ));
    $collection->add_task(new FW_Ext_Backups_Task(
        $id_prefix .'db-export',
        'db-export',
        array(
            'dir' => $tmp_dir,
            'full' => $full,
        )
    ));

    foreach ( $dirs as $key => $dir ) {

        $collection->add_task( new FW_Ext_Backups_Task(
            $id_prefix . $key . '-files-export',
            'files-export',
            array(
                'source_dirs'   => array( $key => $dir ),
                'destination'   => $tmp_dir,
                'exclude_paths' => array(),
            )
        ) );

    }

    if (
        !$full
        &&
        /** @since 2.0.16 */
        apply_filters('fw:ext:backups:add-backup-task:image-sizes-remove', true, $collection)
    ) {
        $collection->add_task(new FW_Ext_Backups_Task(
            $id_prefix .'image-sizes-remove',
            'image-sizes-remove',
            array(
                'uploads_dir' => $tmp_dir .'/f/uploads',
            )
        ));
    }
    $collection->add_task(new FW_Ext_Backups_Task(
            $id_prefix .'zip',
            'zip',
            array(
                'source_dir' => $tmp_dir,
                'destination_dir' => fw_ext( 'backups' )->get_backups_dir(),
            ))
    );

    $collection->add_task( new FW_Ext_Backups_Task(
            $id_prefix . 'tmp-dir-clean:after',
            'dir-clean',
            array(
                'dir' => $tmp_dir
            ) )
    );

}

// rename plugin folder after import depending on blog id
add_action( 'fw:ext:backups:tasks:success', '_action_theme_fw_ext_backups_task_success', 10, 1 );
function _action_theme_fw_ext_backups_task_success( FW_Ext_Backups_Task_Collection $collection ) {

    // rename( $oldname, $newname );

}
GheorgheP commented 7 years ago

Hmmm,

Nope, you can export it in multiple directories, ok I did not test it, but it should work.

As you can see, you can set the directory as a fw_callback.

That means that the backup directory will be requested lazily, specifically in the moment when the backup will be made, so you can set in config a callback that will check if is multisite or what blog is it, or what is the current weather in London :D , so you could make the current backup.

You just need to add some thing like this in the config file:

$cfg = array();
$cfg['dirs.destination'] = fw_callback( 'my_fw_ext_backups_destination_directory' );

function my_fw_ext_backups_destination_directory() {
    return is_multisite()
        ? '/var/backups/' . get_current_blog_id() . '/'
        : '/var/backups/';
}
GheorgheP commented 7 years ago

But I have the idea to create multiple tasks collections, so you could use the desired collection when make backup, or create a backup with a desired tasks collection.

spatricius commented 7 years ago

I guess I didn't make it clear that I need to export 2 different paths, and then import them into 2 another different paths. As far as I understand, currently I can do only one ( source -> dest ) pair without changing the collection.

But your solution with the callback makes this easier. Thanks!

GheorgheP commented 7 years ago

Hmmm, exporting in 2 different directories same backup I think is not already the backup extension task, as this is a very specific requirement, but sure, it the code should allow developing this in case you need it.