Please, don't mark it as dup of http://drupal.org/node/90677 because this other request is a compound one.

Here I only ask for one request: make backup work within cron drupal task. Backup should then be stored in the main directory and with a time stamp. It would be advisable, as per best practices, that the backup took into account the multi-site directory structure and that it included the contrib mods too.

CommentFileSizeAuthor
#20 backup-patch-cron.zip20.29 KBPetra

Comments

dmuth’s picture

Version: master » 5.x-1.x-dev
Assigned: Unassigned » dmuth

On my TODO list!

filiptc’s picture

Priority: Normal » Critical

+1
This is a critical feature

forseti’s picture

Here is a basic cron implementation, add this to backup.module

/**
* Implementation of hook_cron().
*
* removes backup files older than 24hrs.
*/
function backup_cron() {

watchdog('cron', "Cleaning old backup files...", WATCHDOG_WARNING);
$target = variable_get("backup_target", "");

$cleancmd = "find $target -name \"drupal-backup*tgz\" -type f -mtime 1 2>&1";

$cleanfp = popen($cleancmd, "r");

while ($line = fgets($cleanfp)) {
`rm -f $line`;
}

$retval = pclose($cleanfp);

}

I have almost finished a slightly better option for the cron that allows people to configure the time interval from 6, 12, 24 hrs or dissabled. Will post it too when properly tested.

Farreres’s picture

An interesting addition to this would be the ability to specify the desired number of backups that should be kept. That way, I could ask for having one backup every 24 hours, and also to keep up to 3 of these backups. When a fourth is generated, the oldest would be removed.

Another thing that would be desirable is that, while backup is being executed, drupal should be set to maintainance mode automatically and they restored to working mode, all of this automatically.

dmuth’s picture

Yes, I know this is a needed feature.

Right now, I am working on a feature to list/download/delete backups. When that is done, I will start on the cron feature.

dmuth’s picture

An interesting addition to this would be the ability to specify the desired number of backups that should be kept. That way, I could ask for having one backup every 24 hours, and also to keep up to 3 of these backups. When a fourth is generated, the oldest would be removed.

That's actually what I'm working on right now before I get started on the cron functionality.

Another thing that would be desirable is that, while backup is being executed, drupal should be set to maintainance mode automatically and they restored to working mode, all of this automatically.

Sorry, but I am not propared to set the site automatically to maintenance mode while doing a backup. This would only spawn massive amounts of complaints from people who (justifiably) wanted to know why their sites were taken down while doing a backup. And, should the script exit abnormally and the site be left in maintenance mode... that would be bad.

-- Doug

Farreres’s picture

Doing a backup without the system being in maintainance mode would generate possibly inconsistent data. If you feel unfonfortable, it could be offered as a configurable option. Another interesting thing would be that this module changed the default maintainance message. When backup places the site in maintainance, the message would be "a backup is being made, please be patient. Security is a must". This way, in case the script fails abruptly, there is a hint as to why the site is blocked, and the system admin only has to log in and place it back in production mode. Just my ideas.

dmuth’s picture

Fair enough -- I think an option would be workable. And I like your idea of changing the message that is displayed.

I will put that on my TODO list with the rest of the cron stuff.

-- Doug

dmuth’s picture

Version: 5.x-1.x-dev » 5.x-2.x-dev

I just wanted to let folks here know that this will be happening soon. I finished off the system to view/delete existing backups, so I'll be able to concentrate on the cron system now.

I'll be out of town this coming weekend (April 1st), but I will return next week and begin work on cron functionality.

Thanks for being patient with me.

-- Doug

lonebrave’s picture

First off, let me say I love the simplicity of making backups with this module.

I've spent a couple days searching for ways to make automated backups with this module when I finally decided to check the requested features to see if integrating it with cron had been suggested yet. Much to my pleasure I found this request.

All the ideas posted above seem good. I, too, am in favor of configurable options versus forcing what may be controversial function on everyone.

I realize development on this is done in free time, and I'm patiently waiting. Is there any update on the progress, though?

dmuth’s picture

Someone asked for a progress update, so here it is.

I have been amazingly busy the last 3-4 weekends, which meant no free time for me. :-(

There seems to be a lull in that though, so with a little luck I can take a whack at some of these bugs this coming week.

-- Doug

Petra’s picture

Make backup work within cron drupal task will be a good feature!

I don't know how large the most backups are - some people want to get their backups via email - my backup of a multisite-installtion is about 40MB...

Backup every 6 hrs sounds like a much visited side with Community (many postings/comments). Here should be thought also of people, which (must) use poormanscron.

To "all backups delete, which are older than 24h": imagine you are on vacation or you recover from a good party and someone hacks your side and you really need the backup. Then you have however only backups from your hacked side.

It should remain a backup from (e.g.) the 1. of month and from each first day of the week (and one from the last day). So one does not have to insure oneself each day that still everything is correct and if someone really messed up his site, he can restore it.

Advanced would be naturally a monthly fullbackup with weekly differential backups regarding the fullbackup and daily incremental backups regarding the weekly backups. But that is probably something oversized for this modules.

Petra’s picture

I tried it myself: I copied the lines from function backup_action_backup() into a new function backup_cron(), I added the line
include_once ("functions.inc.php");
and deleted all lines with drupal_set_message();. Likewise I inserted forsetis * removes backup files more older than 24hrs., however the files are to be deleted after 7 days (I want each day one backup and the oldest should be 1 week old).
For this I changed
$cleancmd = "find $target -name \"drupal-backup*tgz\" -type f -mtime 1 2>&1";
to
$cleancmd = "find $target -name \"drupal-backup*tgz\" -type f -mtime mtime 7 2>&1";
(don't know if it works, I had to wait a few days.)

In backup.module I can specify a directory where the backup will be written. If I specify a directory in drupal's root directory the backup works per cron. If I specify a directory in a subdirectory (for example: sites/example.com/backup) I receive after the first cronrun following error message:

backup_files(): Command 'tar cfz /tmp/backup-htdocs-zpjRL5 CHANGELOG.txt cron.php ... drupal-backup-db-20070714144432-955933870.sql.gz ' returned value: 2

It is probably caused by the already existing backupfile. If I delete all old backupfiles, then cron runs completely and creates a backupfile .

@make backupfile smaller

I have my sites in one multisite-installtion and backup copies always the entire data base and all files. The backupfile momentarily has already 40 MB. How could one make it smaller?
- One idea is to empty the cache tables before, because these do not have to become backuped. But in a multisite-installtion cache_clear_all() would only empty the cache of the current domain, but not all cache tables of the other installations.
- Another idea is to make backup only for the current domain but therefor some changes would have to be made in the module.

I'll be out of town this coming weekend (April 1st), but I will return next week and begin work on cron functionality.

Doug, are you working on cron functionality? Can I help, perhaps testing?

spyderpie’s picture

Love the backup module .. would love cron too :)

Petra’s picture

I wrote one - it keeps backupfiles for 8 days and makes a backupfile each cronrun.
Be carefull, backupfiles could have 100 MB and when you run cron 3 times a day, you would have 2400MB backupfiles...

This funktion needs to store the backup-files in a directory in the root-directory of drupal (you can define one on the administration-site).

backupfiles need to have chmod 0705 instead of 0604 for deleting the files when cron runs over crontab (it runs as anomymous)

change in functions.inc.php

function backup_files($db_file, &$backup_file, $prefix_dir = "") {

...
...

  //
  // Make our backup file world-readable.
  //
  if (!chmod($backup_file, 0705)) {
    $error = "chmod() failed";
    return($error);
  }
  
  // Assume success
  return(null);

} // End of backup_files()

put in backup.module

/**
* Make backup per cron.
*/
function backup_cron() { 

  $db_file = "";
  include_once("functions.inc.php");
  
  //
  // Backup our database
  //
  $target = variable_get("backup_target", "");
  watchdog("backup", t("Starting backup by cron..."));
  
  // removes backup files older than 7days.

  $message = t("Cleaning old backup files...");
  watchdog("backup", $message);

  $cleancmd = "find $target -name drupal-backup*tar.gz -type f -mtime +6 2>&1";

  $cleanfp = popen($cleancmd, "r");

  while ($line = fgets($cleanfp)) {
  
    $message = t("Files cleaned successfully: ".$line);
    watchdog("backup", $message); 
    `rm -f $line`;
  }
  
  $retval = pclose($cleanfp);   

  //
  // If we are not backing up to the Drupal root directory, then let's set
  // the permissions of existing backup files to 000 while the backup is run.
  //
  if (!empty($target)) {

    $error = backup_get_backups($files);
    if (!empty($error)) {
      $error = "backup_database(): " . $error;
      watchdog("backup", $error, WATCHDOG_ERROR);
      return(null);
    }

    $error = backup_permission_null($files);
    if (!empty($error)) {
      $error = "backup_database(): " . $error;
      watchdog("backup", $error, WATCHDOG_ERROR);
      return(null);
    }

  }

  $error = backup_database($db_file, $target);
  if (!empty($error)) {
    $error = "backup_database(): " . $error;
    watchdog("backup", $error, WATCHDOG_ERROR);
    return(null);
  }

  $message = t("Database backed up successfully.");
  watchdog("backup", $message);

  //
  // Backup our filesystem
  //
  $error = backup_files($db_file, $backup_file, $target);
  if (!empty($error)) {
    $error = "backup_files(): " . $error;
    watchdog("backup", $error, WATCHDOG_ERROR);
    return(null);
  }

  //
  // If we are not writing to the Drupal root directory, restore the 
  // permissions on our backup files.
  //
  if (!empty($target)) {

    $error = backup_permission_rw($files);
    if (!empty($error)) {
      $error = "backup_database(): " . $error;
      watchdog("backup", $error, WATCHDOG_ERROR);
      return(null);
    }

  }

} // End of backup_cron()

physiotek’s picture

i instaled the petra patch
i will look how it works and give feedback

Petra’s picture

I use it since I posted the patch - works fine.
If you have trouble using it, tell me, perhaps I can help.

Farreres’s picture

Could you please apply the patch and generate an alpha version people can test?

andrabr’s picture

I wish I was competent enough to add cronPlus support: it allows to differentiate when processes are run, and I am not paranoid enough for hourly backups ;-)

Petra’s picture

StatusFileSize
new20.29 KB

I can't apply a patch, cause I use an old version of backup.module.
But I attached the whole module - still works fine for me.

rapsli’s picture

Petra patch works fine.. also for newer versions. Including this into the module would be a nice feature!