Jump to content

Cronjob Database Backup


kixe

Recommended Posts

  • 9 months later...
On 5/27/2016 at 7:06 AM, kixe said:

@Robin S
Thanks for pointing this out. This note is obsolete. I missed to remove it from earlier versions. The cronjob is triggered by every user.
 

Hey Kixe!
Thank you for this great module! I use it quite often.
For most projects I need the backup to get triggered when the client logout.
I don't want to give the client the backup permission, to prevent them to mess things up.
From your comment above it seems the permission is not needed.
But when I logout without the backup permission, the backup is not working.
It would be nice if the backup gets triggered by any user on logout, or at least an option for this.

  • Like 2
Link to comment
Share on other sites

Hi @kixe,

additionally to what @jploch said above, and when you are already at it ;-), it would be very helpful to have the option for auto-backup on login!

The real life usage of auto backup on logout is not consistent because some users simply leave the backend without logout process. So I would prefer to switch to auto backup on login process. :-)
(Login process can not be skipped like Logout process!)

 

  • Like 5
Link to comment
Share on other sites

@jploch @horst

Tonight a had a look in the module and I made an update to version 1.1.6

  • I removed any general permission from module. The Backup is triggered now by any user (if a time interval is set).
  • I added trigger option when a user logs in (thanks for the hint @horst).
  • I changed the name for the required permission (trigger on login or logout) from 'db-backup' to 'trigger-db-backup'. Also to prevent permission conflicts with ProcessDatabaseBackupsusing this permission.

 

  • Like 2
Link to comment
Share on other sites

13 hours ago, kixe said:

@jploch @horst

Tonight a had a look in the module and I made an update to version 1.1.6

Nice! Thanks for the quick update!
After I updated to the new version, the permission 'trigger-db-backup' wasn't available.
I created it manually and now it works. Maybe the permission wasn't created  because I updated, rather than installing a new copy? 
Anyway great update, triggering the backup on login is a nice option! 

  • Like 2
Link to comment
Share on other sites

4 hours ago, jploch said:

After I updated to the new version, the permission 'trigger-db-backup' wasn't available.
I created it manually and now it works. Maybe the permission wasn't created  because I updated, rather than installing a new copy? 

Yes, the permission will be created on install only.

  • Like 1
Link to comment
Share on other sites

  • 2 months later...
  • 2 weeks later...

on version 1.2.0 for some reason my javascript files that are compiled with procache have the path to the js file in the beginning of the compiled file which is breaking all js on the site... This isn't happening with the earlier versions of the module....

/home/user/public_html/site/modules/CronjobDatabaseBackup/CronjobDatabaseBackup.js?v=120-1572797219;

Link to comment
Share on other sites

  • 1 month later...
  • 2 months later...

@kixe

So the issue here is that the module has a JS file named the same as the module - this is a problem because this module is autoload, and as such the module's JS file is polluting the front end output for any of us who use $config->scripts, meaning that our scripts for use on the front end inadvertently includes the module's js file, which is not only unnecessary, but also is breaking my frontend api.

a much better way of loading the JS file is to rename it and then just load the JS on the module's setting page, you can do that by checking the input and loading your js.

For now i just disabled the js file, i don't even know if it is needed, or being used, and this has fixed the problem.

  • Like 1
Link to comment
Share on other sites

@Macrura My two cents on this: I feel like it's the other way around — $config as a data store is (for the most part) internal to ProcessWire's backend. The $config->scripts and $config->styles arrays are wired up in a way that makes sense for the admin panel to work properly. Using them for the frontend is surely convenient (I've done that myself on a lot of sites) but probably not meant to be used that way and prone to breaking. I've personally stopped using the internal file arrays for that reason and created namespaced versions for the frontend.

  • Like 3
Link to comment
Share on other sites

8 hours ago, d'Hinnisdaël said:

My two cents on this: I feel like it's the other way around — $config as a data store is (for the most part) internal to ProcessWire's backend. The $config->scripts and $config->styles arrays are wired up in a way that makes sense for the admin panel to work properly. Using them for the frontend is surely convenient (I've done that myself on a lot of sites) but probably not meant to be used that way and prone to breaking. I've personally stopped using the internal file arrays for that reason and created namespaced versions for the frontend.

In one sense that could be an interpretation, but another interpretation is that it can be used on the front end, and i know of some other devs that do use it on live sites so i think it's still risky to autoload scripts or styles on a module without just loading the assets where you need them..

Quote

Array used by ProcessWire admin to keep track of what javascript files its template should load. It will be blank otherwise. Feel free to use it for the same purpose in your own sites.

I don't currently know any modules where the $config->styles/scripts load on the front end inadvertently, but i'll keep a lookout. I have a module that loads into those for the purposes of use on the front end (Soundmanager).

The idea is to create an ecosystem where markup generating modules can have a way to load dependencies on the front end - in Soundmanager2, you need to load the various files based on your settings. By checking to see if there is anything in $config-styles/scripts, i can have modules that can load dependencies using the core $config class.

Maybe there is a better way, but $config is a useful class and i'm using it all over the place for front end development..

Link to comment
Share on other sites

1 hour ago, Macrura said:

i know of some other devs that do use it on live sites so i think it's still risky to autoload scripts or styles on a module without just loading the assets where you need them

The thing is, it's a risk that's carried by the site developer rather than the module developer. If an autoload module adds scripts and styles to $config->scripts and $config->styles it's your site that's at risk of breaking, not the module. Personally I'd never want to take that risk.

The simple solution is to create your own custom FilenameArray in $config that is reserved for front-end usage. This old post from @Soma explains how:

 

  • Like 2
Link to comment
Share on other sites

  • 4 weeks later...

@kixe, I feel that the settings "Maximum number of backups" and "Remove backups older than" should only apply to backups created by CronjobDatabaseBackup. If the user has made manual backups, or backups have been made by a different module, then CronjobDatabaseBackup should never delete those backups. What do you think?

  • Like 3
Link to comment
Share on other sites

4 hours ago, Robin S said:

@kixe, I feel that the settings "Maximum number of backups" and "Remove backups older than" should only apply to backups created by CronjobDatabaseBackup. If the user has made manual backups, or backups have been made by a different module, then CronjobDatabaseBackup should never delete those backups. What do you think?

+1

I have automatic backups every hour during development and do manual backups when reaching milestones and they get deleted from time to time so I need to manually restore them. In my case it would be enough to exclude special filenames from deleting.

  • Like 1
Link to comment
Share on other sites

13 hours ago, Robin S said:

@kixe, I feel that the settings "Maximum number of backups" and "Remove backups older than" should only apply to backups created by CronjobDatabaseBackup. If the user has made manual backups, or backups have been made by a different module, then CronjobDatabaseBackup should never delete those backups. What do you think?

+2

All Backups from CronjobDatabaseBackup must be identified by comment signs, not by filename format. Filename formats may change for some reason.

Link to comment
Share on other sites

On 4/18/2020 at 4:37 AM, Robin S said:

@kixe, I feel that the settings "Maximum number of backups" and "Remove backups older than" should only apply to backups created by CronjobDatabaseBackup. If the user has made manual backups, or backups have been made by a different module, then CronjobDatabaseBackup should never delete those backups. What do you think?

This should have been already implemented with the option to set a custom storage path. Unfortunately I recognized a bug while looking in this. This bug is fixed now and I made some additional changements. Please update to v1.2.3.

The automated cleanup effects only the custom path and not site/assets/backups/database/ used by ProcessDatabaseBackups. Currently you cannot use ProcessDatabaseBackups to edit backups stored under a custom path.

I added functionality to protect files by 'stopword'. Files having this in its names or descriptions will be protected from beeing deleted by the cronjob. Just set the 'stopword' in the module config.

@bernhard @horst @Robin S
I hope the fix helps and feeds your needs (?)

 

  • Like 1
  • Thanks 2
Link to comment
Share on other sites

@kixe what do you think of this scenario: CronjobDatabaseBackup writes a Identifier Comment Word into all its files, and if that identifier is missing, just ignore it in all routines.?

So, stopword also sounds good, but needs manual action everytime. The other way round, if I create manual dumps and want to delegate them to CDBs cleaning routine, I can include the identifier word. This has less harm when forgot or mistake on including a word.

 

  • Like 2
Link to comment
Share on other sites

I actually ended up writing my own backup script because I wanted the backup to run by cron job at a fixed time rather than have the unpredictability inherent in LazyCron.

But in terms of this module my vote would go for having CronjobDatabaseBackup identify the backups that it creates either by filename prefix or something distinctive in the Description as per @horst's suggestion. Then the backups can be safely stored in /site/assets/backups/database/ where they can be viewed and restored via ProcessDatabaseBackups and there is no requirement to use a "file protection string" to protect backups not created by CronjobDatabaseBackup.

Link to comment
Share on other sites

  • 2 weeks later...
On 4/21/2020 at 2:32 AM, Robin S said:

I actually ended up writing my own backup script because I wanted the backup to run by cron job at a fixed time rather than have the unpredictability inherent in LazyCron.

I use this 5 lines of code, placed in an access controlled folder under root, to run a cronjob at fixed time. Just set 'cycle' in module settings to 'none' to prevent database backups triggered by LazyCron as well.

<?php namespace ProcessWire;
$root = dirname(__DIR__);
include_once("$root/index.php");
$cdb = $modules->CronjobDatabaseBackup;
$cdb->cronBackup();

 

On 4/20/2020 at 5:04 PM, horst said:

The other way round, if I create manual dumps and want to delegate them to CDBs cleaning routine, I can include the identifier word

Thanks for the input. I thought about it. In the end, I'll leave it as it is, and the inclusion of manually created dumps in the cleaning routine remains the standard. When I manually create a dump, I usually move or download it immediately. I rarely (never?) leave it on the live server and I want the files removed automatically. The default name created by ProcessDatabaseBackups is the database name. Just use this as the 'stopword' and use a different name format in the setup of CronjobDatabaseBackup and voila it works exactly as you want it.  Furthermore there is also the option to save the files in a user-defined directory. Manually created dumps are then never affected by the CDB's cleaning routine.

  • Like 3
Link to comment
Share on other sites

  • 3 weeks later...
On 4/18/2016 at 5:19 PM, kixe said:

Yes. The Backup is triggered by the hook function cronBackup() expecting a HookEvent as single argument, which is in fact not needed. If you want to trigger the backup from anywhere, just create a HookEvent object and call the cronBackup() function. Done.
 


$event = new HookEvent;
$modules->get('CronjobDatabaseBackup')->cronBackup($event);

Thanks for the module, it is installed on all PW sites I've ever made.
For the code snippet I still couldn't figure out how to run a database backup from an ordinary cron job.
Will I have to create a php file with the hook and call that from the cron r can I call a module file directly from cron?
Or is it possible to integrate it with the PWCron module?

Update: I got it working in a PHP file this way based on @kixe's answer:

<?php
namespace ProcessWire;

include_once("./index.php");

$event = new HookEvent();
$cron_backup = $modules->CronjobDatabaseBackup;
$cron_backup->cronBackup($event);

 

  • Like 1
Link to comment
Share on other sites

  • 5 months later...

I think custom path wont work because of this line in the init method:
 

$customPath = self::checkPath($this->wire('config')->paths->root.$this->field_storage_path);
if ($this->field_storage_path && $customPath) $this->backup->setPath($customPath);

As the custom path is concatenated to the root-path checkPath will return false and the $customPath will never be set.

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...