Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Craft 4.0 support #1

Open
NeleDeBruycker opened this issue May 12, 2023 · 5 comments
Open

Craft 4.0 support #1

NeleDeBruycker opened this issue May 12, 2023 · 5 comments

Comments

@NeleDeBruycker
Copy link

Hi,

Would it be possible to also support Craft 4.0?
We want to upgrade a project that is using this plugin to Craft 4.0 but we get an error.

Thanks in advance,
Nele

@samhernandez
Copy link
Owner

samhernandez commented May 17, 2023 via email

@NeleDeBruycker
Copy link
Author

Hi @samhernandez do you have any updates on this?

1 similar comment
@NeleDeBruycker
Copy link
Author

Hi @samhernandez do you have any updates on this?

@NeleDeBruycker
Copy link
Author

Hi @samhernandez any chance you can have a look at this?

@Antimated
Copy link

Antimated commented Mar 12, 2024

Hi @samhernandez any chance you can have a look at this?

I modified the code of this repo and made my own module for it inside my own project, might be of use for you :). This is tested on Craft 4.8.1 using Laravel Valet

Note: This only supports MYSQL but could easily be updated to use PSQL

File location: modules/backupmodule/BackupModule.php

<?php

namespace modules\backupmodule;

use Craft;
use craft\helpers\App;
use yii\base\ErrorException;
use yii\base\Exception;
use yii\base\Module as BaseModule;
use yii\base\NotSupportedException;

/**
 * BackupModule module
 *
 * @method static BackupModule getInstance()
 */
class BackupModule extends BaseModule
{
    /**
     * Initializes the module.
     *
     * This method is called after the module is created and initialized with property values
     * given in configuration. The default implementation will initialize [[controllerNamespace]]
     * if it is not set.
     *
     * If you override this method, please make sure you call the parent implementation.
     * @throws ErrorException
     * @throws NotSupportedException
     * @throws Exception
     */
    public function init(): void
    {
        Craft::setAlias('@modules/backupmodule', __DIR__);

        // Set the controllerNamespace based on whether this is a console or web request
        if (Craft::$app->request->isConsoleRequest) {
            $this->controllerNamespace = 'modules\\backupmodule\\console\\controllers';
        } else {
            $this->controllerNamespace = 'modules\\backupmodule\\controllers';
        }


        if (Craft::$app->getRequest()->isCpRequest) {
            try {
                $this->patchMysqlCommands();
            } catch (ErrorException $e) {
                throw new ErrorException($e);
            } catch (NotSupportedException $e) {
                throw new NotSupportedException($e);
            } catch (Exception $e) {
                throw new Exception($e);
            }
        }
    }


    /**
     * Patches the backup and restore commands with the given path to mysql and mysqldump.
     * Useful for when the "mysql" or "mysqldump" commands are not found (e.g. when ran on a Valet environment)
     * @throws ErrorException
     * @throws NotSupportedException
     * @throws Exception
     */
    protected function patchMysqlCommands(): void
    {
        $mysqlDumpPath = App::env('MYSQLDUMP_PATH');
        $mysqlPath = App::env('MYSQL_PATH');

        if (Craft::$app->db->getDriverName() !== 'mysql') {
            return;
        }

        if ($mysqlDumpPath) {
            Craft::$app->config->general->backupCommand = str_replace(
                'mysqldump',
                $mysqlDumpPath,
                Craft::$app->db->getSchema()->getDefaultBackupCommand()
            );
        }

        if ($mysqlPath) {
            Craft::$app->config->general->restoreCommand = str_replace(
                'mysql',
                $mysqlPath,
                Craft::$app->db->getSchema()->getDefaultRestoreCommand()
            );
        }
    }
}

In your composer.json file update your autoload psr-4 entry to include the module:

    "autoload": {
        "psr-4": {
            "modules\\backupmodule\\": "modules/backupmodule/"
        }
    },

Also have to add your module in app.php:

<?php

use craft\helpers\App;
use modules\backupmodule\BackupModule;

return [
    'id' => App::env('APP_ID') ?: 'CraftCMS',
    'modules' => [
        'backup-module' => [
            'class' => BackupModule::class,
        ]
    ],
    'bootstrap' => ['backup-module'],
];

Finally run the following command to make sure composer changes are picked up:

composer dump-autoload -a

Don't forget to add these to your .env file

MYSQL_PATH="/usr/local/opt/[email protected]/bin/mysql"
MYSQLDUMP_PATH="/usr/local/opt/[email protected]/bin/mysqldump"

Ofcourse these paths could be different for you. You could run which mysql mysqldump in terminal to find out the paths you need.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants