Author Topic: [MOD] - Backup site (no cron job need - no system command)  (Read 24582 times)

0 Members and 1 Guest are viewing this topic.

Offline thunderstrike

  • 4images Guru
  • *******
  • Posts: 2.327
    • View Profile
[MOD] - Backup site (no cron job need - no system command)
« on: September 15, 2007, 09:19:45 PM »
Detail: This MOD like http://www.4homepages.de/forum/index.php?topic=15095.0 but better. No system command use (linux) and cron work on user visit (guest include) for 4images. 50% Jan module for DB utils and 50% use for new ZIP module improve. You can choose (1 day minumum) for how day wait for backup process and send email message for say is backup on server in BACKUP_DIR folder. All admin receive email (allow email use or no work for them) for time you set.

Require:

- GZip, ZIP or BZip enable from host or no work.
- ZIP improve: http://www.4homepages.de/forum/index.php?topic=18745.0
- HTML Email: http://www.4homepages.de/forum/index.php?topic=18744.0

// Step 1

In includes/functions.php file,

add top ?>:

Code: [Select]
function backup_site_operation() {
    global $user_info, $site_db, $lang, $config, $site_template, $url, $db_name, $default_backup_tables;
   
  if (!isset($config['backup_site_next_time'])) {
      $site_db->query("INSERT INTO " . SETTINGS_TABLE . " (setting_name, setting_value) VALUES ('backup_site_next_time', '" . time() . "')");
  }
   
  $check_next_time = time() - 60 * 60 * 24 * BACKUP_SITE_DAYS_DELAY; 
  $backup_archive_name = ROOT_PATH . BACKUP_DIR . "/backup.zip";
  $debug_id = (defined('BACKUP_SITE_DEBUG_ID') && $user_info['user_level'] == ADMIN && $user_info['user_id'] == BACKUP_SITE_DEBUG_ID) ? true : false;
   
if ($check_next_time >= $config['backup_site_next_time'] || !@file_exists($backup_archive_name) || (isset($debug_id) && $debug_id) && $user_info['user_id'] == $debug_id) {
   
  @include_once(ROOT_PATH . "includes/db_utils.php");

  $crlf = (get_user_os() == "WIN") ? "\r\n" : ((get_user_os() == "MAC") ? "\r" : "\n");

  $tables_info = array();
  $db = (get_mysql_version() >= 32306)  ? "`$db_name`" : $db_name;
  $result = $site_db->query("SHOW TABLE STATUS FROM $db");
  if ($result) {
    while ($row = $site_db->fetch_array($result)) {
      $tables_info[$row['Name']] = ((isset($row['Type'])) ? $row['Type'] : $row['Engine']);
    }
    $site_db->free_result($result);
  }

  ob_start();
  @ob_implicit_flush(0);

  echo "#----------------------------------------------------------".$crlf;
  echo "# Database Backup for ".format_text($config['site_name'], 2).$crlf;
  echo "# ".date("Y-m-d H:i").$crlf;
  echo "#----------------------------------------------------------".$crlf;
  foreach ($default_backup_tables as $table) {
    @set_time_limit(1200);
    echo $crlf."#".$crlf."# Structure for Table ".$table.$crlf."#".$crlf;
    get_table_def_mysql($table, $crlf);
    get_table_content_mysql($table, $crlf);
  }

  $contents = ob_get_contents();
  ob_end_clean();

  @umask(0111);
  if ($config['gz_compress'] == 1 && extension_loaded("zlib")) {
    $file_name = "backup".time().".sql.gz";
    $fp = @gzopen(ROOT_PATH.DATABASE_DIR."/".$file_name, "w9");
    $ok = @gzwrite($fp, $contents);
    gzclose($fp);
  }
  else {
    $file_name = "backup".date("YmdHi").".sql";
    $fp = @fopen(ROOT_PATH.DATABASE_DIR."/".$file_name, "w");
    $ok = @fwrite($fp, $contents);
    fclose($fp);
  } 
 
  include(ROOT_PATH . "includes/archive.php");
  $backup_operation = new zip_file($backup_archive_name);
  $backup_operation->set_options(array('inmemory' => 0, 'recurse' => 1, 'storepaths' => 1, 'overwrite' => 1));
  $backup_operation->add_files(ROOT_PATH);
  $backup_operation->exclude_files(ROOT_PATH . "data/backup");
  $backup_operation->exclude_files(ROOT_PATH . "data/database/default");
 
  $backup_operation->create_archive();
  $backup_operation->download_file();
  if (count($backup_operation->errors) <= 0) {
      if ($user_info['user_level'] == ADMIN) {
          echo $lang['backup_site_create_success'] . "<br />";   
      }
    @unlink(ROOT_PATH.DATABASE_DIR."/".$file_name);
   
    @include_once(ROOT_PATH . 'includes/html_email/MAIL.php');
   
    // get ID value (random) for the embed image
    $id = MIME::unique();
   
    $site_email = new MAIL;
   
    $sql1 = "
   
    SELECT " . get_user_table_field("", "user_name") . get_user_table_field(", ", "user_email") . "
    FROM " . USERS_TABLE . "
    WHERE " . get_user_table_field("", "user_level") . " >= '" . ADMIN . "' AND " . get_user_table_field("", "user_allowemails") . " = 1 AND " . get_user_table_field("", "user_id") . " != " . $user_info['user_id'];
   
    $result1 = $site_db->query($sql1);       
   
    $site_email->From(stripslashes($config['site_email']), format_text($config['site_name'], 2));   
    if ($user_info['user_level'] == ADMIN && isset($user_info['user_email'])) {
        $site_email->AddTo(stripslashes($user_info['user_email']), format_text(trim($user_info['user_name']), 2));
    } else {
        $site_email->AddTo(stripslashes($config['site_email']), format_text($config['site_name'], 2));
    }

    $email_row = array();
    while ($row = $site_db->fetch_array($result1)) {
        $email_row[] = $row;
    }
    $site_db->free_result($result1);
    $num_email = sizeof($email_row);   
   
    $user_name = "";
    $user_email = "";
    for ($i = 0; $i < $num_email; $i++) {
        if ((function_exists('mailchek') && mailchek($email_row[$i][$user_table_fields['user_email']], 2)) || (FUNC::is_mail($email_row[$i][$user_table_fields['user_email']], true)) && !preg_match("/([\msn|yahoo|caramail|incredimail]+[\.]+[\com|net|ca|org|de|ru|sh|se|fr|au]+)/i", $email_row[$i][$user_table_fields['user_email']])) {

            $user_name = $email_row[$i][$user_table_fields['user_name']];
            $user_email = $email_row[$i][$user_table_fields['user_email']];
           
            if (isset($user_name) && !empty($user_name) && isset($user_email) && !empty($user_email) && !isset($user_info['user_name'])) {               
                $site_email->Text(str_replace(array("{site_name}", "{ftp_location}", "{user_name}", "{delay_time}"), array(format_text($config['site_name'], 2), format_text(stripslashes(trim($backup_archive_name)), 2), format_text(trim($user_name), 2), (int)BACKUP_SITE_DAYS_DELAY), $lang['backup_site_notify_text_message']));
               
                $site_email->Cc[] = array(
                'address'  => stripslashes($user_email), // required
                'name'     => format_text(trim($user_name), 2), // optional
                'charset'  => $lang['charset'], // optional
                'encoding' => 'base64' // optional
                );
            }
        }
    }
    $site_email->Subject(preg_replace("/" . $site_template->start . "site_name" . $site_template->end . "/siU", format_text($config['site_name'], 2), $lang['backup_site_notify_subject_title']));
   
    if ($user_info['user_name']) {
        $site_email->Text(str_replace(array("{site_name}", "{ftp_location}", "{user_name}", "{delay_time}"), array(format_text($config['site_name'], 2), format_text(stripslashes(trim($backup_archive_name)), 2), format_text(trim($user_info['user_name']), 2), BACKUP_SITE_DAYS_DELAY), $lang['backup_site_notify_text_message']));
    }
    if ($user_info['user_level'] == ADMIN) {
        echo $site_email->Send('client') ? preg_replace("/" . $site_template->start . "delay_time" . $site_template->end . "/siU", (int)BACKUP_SITE_DAYS_DELAY, $lang['backup_site_mail_sent']) : $lang['backup_site_mail_error'];
    }
    $site_email->Delto();
    $site_email->Delcc();
    $site_email->DelAttach();
    $site_email->Disconnect();
    unset ($email_row);
    unset ($user_name);
    unset ($user_email);
   
    $site_db->query("UPDATE " . SETTINGS_TABLE . " SET setting_value = '" . time() . "' WHERE setting_name = 'backup_site_next_time'");
       
  } elseif (count($backup_operation->errors) > 0) {
      echo $lang['backup_site_create_fail'];
  }
 
}

} // End function.

In includes/constants.php file,

find:

Quote
define('DATABASE_DIR', 'data/database');

add after:

Code: [Select]
define('BACKUP_DIR', 'data/backup'); // Change folder name !!! (backup to something else)

Change folder name ! (FTP / File manager and define). After, set CHMOD 777 to folder.

Add top ?>:

Code: [Select]
define('BACKUP_SITE_DAYS_DELAY', 1); // Set to 1 day.
//define('BACKUP_SITE_DEBUG_ID', 1); // Debug Admin ID

Is here you can set BACKUP_SITE_DAYS_DELAY (is 1 for now for 1 day). If want more (ex: 3 day - remove 1 and set 3). 3 days, ZIP file will create. ;)
BACKUP_SITE_DEBUG_ID is for debug. 1 is user ID. If need, remove // and replace for done.

// Step 3

In includes/page_header.php file,

find:

Quote
//-----------------------------------------------------
//--- Random Image ------------------------------------
//-----------------------------------------------------

add before:

Code: [Select]
//-----------------------------------------------------
//--- Backup site -------------------------------------
//-----------------------------------------------------

if (function_exists('backup_site_operation')) {
    backup_site_operation();   
}

// Step 4

In lang/english/main.php file,

add top ?>:

Code: [Select]
$lang['backup_site_create_success'] = "<span class=\"smalltext\"><font color=\"green\">Done !</font></span>";
$lang['backup_site_create_fail'] = "<span class=\"smalltext\"><font color=\"red\">Fail !</font></span>";
$lang['backup_site_notify_subject_title'] = "Backup site for: {site_name}";
$lang['backup_site_notify_text_message'] = "Dear {user_name}, \n\n{site_name} has just performed a backup operation of the entire 4images folder on a ZIP file.\nThe File is located in: {ftp_location} (SQL file included).\n\nThis message will appear every {delay_time} day(s).\n\nRegards.\n{site_name}";
$lang['backup_site_list_title'] = "Backup Gallery List";
$lang['backup_site_list_do_backup'] = "Gallery Backup";
$lang['backup_site_list_backup_success'] = "Gallery backed up.";
$lang['backup_site_list_backup_error'] = "<span class=\"smalltext\"><font color=\"red\">Gallery not backed up.</font></span>";
$lang['backup_site_mail_sent'] = "Email sent !<br />Next backup in: <b>{delay_time}</b> day(s).<br />";
$lang['backup_site_mail_error'] = "Error while sending email. Please check mail / web logs (cPanel access) and post in 4homepages.de forum - official topic - if need help.";

// Step 5

Download backup.zip file attachment. Replace admin/backup.php file (or compare if use other MOD on forum - I give no support for compare).

// Step 6

Go to ACP -> Backup database. Should see new window.

// Step 7 (09/19/2007)

In includes/db_field_definitions.php file,

Code: [Select]
$default_backup_tables = array(
table1,
table2
);

Replace: table1, table2 with all define table you have (only define name) from includes/constants.php file.

Should work (use ACP - > setting for email address too). ;)
« Last Edit: September 20, 2007, 02:02:34 AM by thunderstrike »
8 steps need when ask question -

- PHP version (ACP - > phpinfo())
- mySQL version (ACP - > phpinfo())
- 4images version
- Post screenshot / URL
- Post code in BB Code (no need full file for code) or post attach file
- It doesn't work. What is say - what is do for no work
- Install MOD ? If so - please say (troubleshooting)
- Read FAQ ? Install Bug fixes ?

Offline thunderstrike

  • 4images Guru
  • *******
  • Posts: 2.327
    • View Profile
Re: [MOD] - Backup site (no cron job need - no system command)
« Reply #1 on: September 16, 2007, 06:12:45 PM »
This is fix for host no support large files on server in memory.

// Step 1

In includes/functions.php file,

find:

Quote
if ($check_next_time >= $config['backup_site_next_time'] || !@file_exists($backup_archive_name) || (isset($debug_id) && $debug_id)) {

replace:

Code: [Select]
if (format_date($config['date_format'] . " " . $config['time_format'], $check_next_time) <= format_date($config['date_format'] . " " . $config['time_format'], $config['backup_site_next_time']) && (isset($debug_id) && $debug_id)) {

find:

Quote
$backup_operation->set_options(array('inmemory' => 0, 'recurse' => 1, 'storepaths' => 1, 'overwrite' => 1));

replace:

Code: [Select]
$backup_operation->set_options(array('inmemory' => 1, 'recurse' => 1, 'storepaths' => 1, 'overwrite' => 1));

// Step 2

In includes/constants.php file,

find:

Quote
//define('BACKUP_SITE_DEBUG_ID', 1);

replace:

Code: [Select]
define('BACKUP_SITE_DEBUG_ID', 1);

Replace 1 for webmaster ID for download ZIP file (header).
Detail: Visit once per 24 hour (or more) for download ZIP file.
8 steps need when ask question -

- PHP version (ACP - > phpinfo())
- mySQL version (ACP - > phpinfo())
- 4images version
- Post screenshot / URL
- Post code in BB Code (no need full file for code) or post attach file
- It doesn't work. What is say - what is do for no work
- Install MOD ? If so - please say (troubleshooting)
- Read FAQ ? Install Bug fixes ?

Offline thunderstrike

  • 4images Guru
  • *******
  • Posts: 2.327
    • View Profile
Re: [MOD] - Backup site (no cron job need - no system command)
« Reply #2 on: September 20, 2007, 01:56:46 AM »
Fix Step 1 of 1st post.
Add Step 7
8 steps need when ask question -

- PHP version (ACP - > phpinfo())
- mySQL version (ACP - > phpinfo())
- 4images version
- Post screenshot / URL
- Post code in BB Code (no need full file for code) or post attach file
- It doesn't work. What is say - what is do for no work
- Install MOD ? If so - please say (troubleshooting)
- Read FAQ ? Install Bug fixes ?

Offline Nicky

  • Administrator
  • 4images Guru
  • *****
  • Posts: 3.195
    • View Profile
Re: [MOD] - Backup site (no cron job need - no system command)
« Reply #3 on: September 20, 2007, 09:30:45 AM »
hi,

just a question because i didn't test it.

how backup should work every 1 day without cronjob when example site is not visited for 2 days?
cheers
Nicky
Your first three "must do" before you ask a question ! (© by V@no)
- please read the Forum Rules ...
- please study the FAQ ...
- please try to Search for your answer ...

nicky.net 4 4images
Signature stolen from mawenzi

Offline thunderstrike

  • 4images Guru
  • *******
  • Posts: 2.327
    • View Profile
Re: [MOD] - Backup site (no cron job need - no system command)
« Reply #4 on: September 20, 2007, 12:46:08 PM »
Quote
how backup should work every 1 day without cronjob when example site is not visited for 2 days?

Users must visit for this MOD to work.
8 steps need when ask question -

- PHP version (ACP - > phpinfo())
- mySQL version (ACP - > phpinfo())
- 4images version
- Post screenshot / URL
- Post code in BB Code (no need full file for code) or post attach file
- It doesn't work. What is say - what is do for no work
- Install MOD ? If so - please say (troubleshooting)
- Read FAQ ? Install Bug fixes ?

Offline Nicky

  • Administrator
  • 4images Guru
  • *****
  • Posts: 3.195
    • View Profile
Re: [MOD] - Backup site (no cron job need - no system command)
« Reply #5 on: September 20, 2007, 01:26:18 PM »
ok, thank you & have a nice day.
cheers
Nicky
Your first three "must do" before you ask a question ! (© by V@no)
- please read the Forum Rules ...
- please study the FAQ ...
- please try to Search for your answer ...

nicky.net 4 4images
Signature stolen from mawenzi

Offline thunderstrike

  • 4images Guru
  • *******
  • Posts: 2.327
    • View Profile
Re: [MOD] - Backup site (no cron job need - no system command)
« Reply #6 on: September 20, 2007, 05:50:13 PM »
Is possible move this sticky ?
8 steps need when ask question -

- PHP version (ACP - > phpinfo())
- mySQL version (ACP - > phpinfo())
- 4images version
- Post screenshot / URL
- Post code in BB Code (no need full file for code) or post attach file
- It doesn't work. What is say - what is do for no work
- Install MOD ? If so - please say (troubleshooting)
- Read FAQ ? Install Bug fixes ?

Offline Loda

  • Sr. Member
  • ****
  • Posts: 353
    • View Profile
    • Fotosucht Schweiz
Re: [MOD] - Backup site (no cron job need - no system command)
« Reply #7 on: October 08, 2007, 09:10:38 AM »
hi..
i've got an error message..
Quote
Error while sending email. Please check mail / web logs (cPanel access) and post in 4homepages.de forum - official topic - if need help.
i insert all your steps..
need i the smtp server in the settings?
my version 1.7
PHP Version 4.4.7
MySQL-Client-Version: 4.1.11
backup is done
but the mod backup it the hole day.
« Last Edit: October 08, 2007, 10:44:39 AM by Loda »

Offline thunderstrike

  • 4images Guru
  • *******
  • Posts: 2.327
    • View Profile
Re: [MOD] - Backup site (no cron job need - no system command)
« Reply #8 on: October 08, 2007, 02:23:28 PM »
Quote
i insert all your steps..

And message from web error logs ? Error message say check web error logs and where are the web error logs in your post ? Please post info in PM (in bbcode please :!:) and I check.
8 steps need when ask question -

- PHP version (ACP - > phpinfo())
- mySQL version (ACP - > phpinfo())
- 4images version
- Post screenshot / URL
- Post code in BB Code (no need full file for code) or post attach file
- It doesn't work. What is say - what is do for no work
- Install MOD ? If so - please say (troubleshooting)
- Read FAQ ? Install Bug fixes ?

Offline Loda

  • Sr. Member
  • ****
  • Posts: 353
    • View Profile
    • Fotosucht Schweiz
Re: [MOD] - Backup site (no cron job need - no system command)
« Reply #9 on: October 08, 2007, 05:31:53 PM »
hmm.. i can only see the Internal Errors ... i don't know, where i can find the web logs.. i think my hoster doesn't support it to me..

Offline thunderstrike

  • 4images Guru
  • *******
  • Posts: 2.327
    • View Profile
Re: [MOD] - Backup site (no cron job need - no system command)
« Reply #10 on: October 08, 2007, 05:38:41 PM »
Quote
i don't know, where i can find the web logs.. i think my hoster doesn't support it to me..

Check in FTP / cPanel for web logs.
8 steps need when ask question -

- PHP version (ACP - > phpinfo())
- mySQL version (ACP - > phpinfo())
- 4images version
- Post screenshot / URL
- Post code in BB Code (no need full file for code) or post attach file
- It doesn't work. What is say - what is do for no work
- Install MOD ? If so - please say (troubleshooting)
- Read FAQ ? Install Bug fixes ?

Offline Loda

  • Sr. Member
  • ****
  • Posts: 353
    • View Profile
    • Fotosucht Schweiz
Re: [MOD] - Backup site (no cron job need - no system command)
« Reply #11 on: October 08, 2007, 06:58:49 PM »
yes yes... i found it...

Offline thunderstrike

  • 4images Guru
  • *******
  • Posts: 2.327
    • View Profile
Re: [MOD] - Backup site (no cron job need - no system command)
« Reply #12 on: October 08, 2007, 07:27:28 PM »
And ... is say ? (in PM for report)
8 steps need when ask question -

- PHP version (ACP - > phpinfo())
- mySQL version (ACP - > phpinfo())
- 4images version
- Post screenshot / URL
- Post code in BB Code (no need full file for code) or post attach file
- It doesn't work. What is say - what is do for no work
- Install MOD ? If so - please say (troubleshooting)
- Read FAQ ? Install Bug fixes ?

Offline clubbu

  • Newbie
  • *
  • Posts: 47
    • View Profile
Re: [MOD] - Backup site (no cron job need - no system command)
« Reply #13 on: August 03, 2011, 01:04:45 PM »
Hello, work with 1.7.10?

Rembrandt

  • Guest
Re: [MOD] - Backup site (no cron job need - no system command)
« Reply #14 on: August 03, 2011, 06:28:57 PM »
Hello, work with 1.7.10?
test it for us ..   :)