Sam's Auto Backup V4.5

For osC V2.3.x




This contribution automatically backs up your database at regular intervals whilst you are logged into admin.
A cron job can also be set up to create timed daily backups, regardless of whether anyone logs in or not.
It will create an archive backup at a longer intervals, you set the intervals in admin.
Backups can now be split or extended past max execution time if you have a very large dBase.
It can also gzip the backups, if required, also set in admin.
Cron job backups occur in exactly the same way as admin backups, using the same admin settings. Cron job backups can be automatically emailed on completion.
A new module is added to allow automatic deletion of old backups, meant to be called as a cron job, but can be called by Auto Backup. Configuration settings also through admin.
Backups are compatible with Database Backup Manager (http://addons.oscommerce.com/info/5769) so selective restores can be made.

Basic install is just 7 easy steps.

Changes & Features in version 4.

  • Code re-written, now faster & more efficient (un-compressed backups are upto 50% smaller) backups take approx 1.5 secs per Mb (un-compressed).
  • SQL file is formatted to current standards. Data has regular 'inserts' to prevent memory issues during a restore.
  • Installation now even easier, just copy files and run the install script.
  • The code now includes an integral gzip function to ease install.
  • A separate language file has now been created to ease alterations for non English speakers.
  • Checks are now made at every stage of a backup to ensure its integrity, the previous backup is now only deleted (if required) once the new backup is successful. Similarly GZipping is checked throughout progress & the un-compressed file only removed if successful.
  • Backups can now be split or extended past Max execution time if you have a very large dBase.
  • The 'sessions' and 'whos_online' tables are now automatically excluded from auto-backups, other exclusions can easily be added if needed.
  • The backup status is now only checked once every 5 minutes instead of on every page load.
  • New Auto Delete module added to allow automatic deletion of old backups.
  • Added option to email cron job backups on completion.
  • As logfiles are created in the backup folder, modifications to Database Backup Manager are included to enable display/deletion of same.
  • Added new feature to Database Backup Manager the ability to email any selected backup file to the store owner.
  • Support Forum at http://forums.oscommerce.com/index.php?showtopic=303380.

Version Updates

V 4.5

  • WARNING: auto_backup_setup.php don't upgraded, I DON'T KNOW IF WORKS, DON'T USE IN LIVE STORE.
  • Bootstraped Page
  • Bootstraped Buttons
  • Replaced array() to []
  • Replaced require('file-path') to require 'file-path'
  • Replaced require('file-path') to require 'file-path'
  • Replaced some XHTML Tags to HTML Tags
  • Added Spanish translations (partially)
  • Changed files: admin/backup.php, admin/includes/auto_backup_db.php, admin/includes/functions/db_backup.php
    Added files: admin/includes/languages/espanol/auto_backup_db.php, admin/includes/languages/espanol/backup.php

V 4.4

  • Replaced mysql functions by mysqli.
  • Changed files: admin/backup.php, admin/includes/auto_backup_db.php, admin/includes/functions/db_backup.php

V 4.3

  • Previous versions were exporting non integer fields as a number if the content was a number. In my case, this was causing problems on the products Model_Name when it was a number and was starting with 0, where the 0 was missing in the backup file (it can happen in a lot more fields). This version fixes this.
  • Changed files: auto_backup_db.php

V 4.2

  • Added an option to the shop backoffice to enable/disable the automatic database backup. Addition by Modem 2.0
  • Modified email function to widen server compatibility
  • Fixed a small typo error in the include of Mail/mime.php in mail backup, as mentioned in the forum
  • Changed files: auto_backup_setup.php, mail_backups.php

V 4.1

  • Made database read more robust against malformed database entries.
  • Added 'Test Run' button to Auto Backup Setup which makes a forced run so users can easily confirm operation.
  • Added email backup feature to Database Backup Manager.
  • Added option to email backup on completion of cron job.
  • Fixed minor bug in cron job, erroneous function call made when creating backup complete message.
  • Changed files: backup.php, auto_backup_db.php, auto_backup_setup.php, languages/english/auto_backup.php
    Added files: mail_backups.php, mail_cron_backups.php


Usage

Backups only occur with registered admin for security, at regular intervals set in admin by yourself. When a new backup is created the previous interim one is deleted, subject to the Backup Archive Interval.

Backup's are archived at interval again set in admin such that when a new backup is created, the previous interim one is deleted so long as another older but less than the Backup Archive Interval exists. i.e. with 'Backup Archive Interval' set to 12Hrs. a archived backup is kept every 12Hrs..

If Auto Backup experiences any write failures whilst creating your backup, it will abort & create a log file in your backup folder called 'Auto Backup Write Failure.log', as long as that file exists Auto Backup will not run, but give a warning through the message stack on every page load, that is to prevent Auto Backup continually attempting to create a needed backup when problems will just cause another failure. Just fix issue and delete the log file to resume normal operation.

Backup status is checked at minimum 5 minute intervals, should you manually delete the latest backup to force a new Auto Backup process, its possible that may not happen till 5 minutes later.

Data inserts are broken up regularly at approx every 140 fields, if wished that can be adjusted by editing line 208 of auto_backup_db.php ($at_mod = (int)(140/sizeof($at_field_list));).

You can choose to split backups should the backup process approach maximum execution time or extend execution time for a continuous backup. Splitting reduces problems with admin page loads/updates if backup takes too long but may result in invalid backups if database changes during split. If your server is operating in safe mode splitting will always occur as execution time may not be extended.

If Auto Backup has to created a split backup the backup file created will have (split) within its name and the pause resume point will be marked with '# pause/resume point' should you wish to check the file. Pauses will always occur within data insertion zones.

Auto Backup V4 creates partial files during backup creation, these are not visible within Database Backup Manager and are renamed on successful completion. Should any partial files remain due to an interrupted session auto_backup_delete will automatically remove them. If a partial is lost during a split the backup will restart from the beginning.

If your server is operating in 'safe mode' splitting is forced, ensure you include the -n option when setting up any cron job.

Auto Backup Delete:
Auto Backup Delete will search all files within the backup folder and check all those with a timestamp (ie db_oscommerce_201202191637.sql.gz) any older than the specified limit (in days) will be deleted, however if Auto Backup Delete does not find any current backups (by the admin settings) it will abort the delete process, that is to ensure you don't end up deleting all your backups.

If wished you can manually run auto_backup_delete by putting your-site.com/catalog/admin/includes/auto_backup_delete.php in your browser to check its working for you, it will act exactly as normal, just output the text to the screen, it is not integrated into admin though (ie no menus).

The 'sessions' and 'whos_online' tables are now automatically excluded from auto-backups, if you wish to add extra tables for exclusion, just edit line 115 of auto_backup_db.php ($at_exclude_tables = array_flip(array_merge(array('sessions','whos_online'),$at_tables_done));) adding more 'comma separated' values.

Emailing backups:- Before you can send backups by email, you must create an email account to send them (or use an existing) then edit /admin/includes/mail_backups.php and add your host, username, password and port details for that account where shown in the file. Backups will be emailed to the store owners email address as set in Admin->My Store->E-Mail Address.
Backups will be emailed through the pear mail function if available, otherwise the less secure php mail function. If you receive a warning when selecting 'email backup' that pear has issues ask your host to add pear mail with smtp & mime classes. pear mail is safe & more robust the php mail, so enable if you can.

Warning:
Auto Backup V4 checks file write integrity at every stage, Database Backup Manager does not, so if Auto Backup reports an error, but following Database Backup Manager does not, don't assume Auto Backup had an issue that Database Backup Manager does not, more likely Auto Backup found a problem that Database Backup Manager ignored.

Support Forum at http://forums.oscommerce.com/index.php?showtopic=303380.

Downlaod found at http://addons.oscommerce.com/info/8374.




SETTINGS:

The settings will appear in configuration as Auto Backup, defaults are 20mins between backups, 24Hrs between archives, 30 days before archives are deleted and backups GZipped.

The previous Zip option is removed in this version, use GZip.

Admin Configuration Options:

Enable Auto Backup 			Default true
When it is set to true, automatic database backup is activated. In some cases, it is good to have an option to temporarily disable the database backup, so set it to false when needed, but don't forget to set it back to true once finished.

Auto Backup Interval 			Default 20 (mins)
Alter the automatic dBase backup interval in minutes, i.e. 20mins (5 mins minimum). When a new backup is created the previous interim one is deleted, subject to the Backup Archive Interval

Backup Archive Interval 		Default 24 (Hours)
Sets the interval to keep backups in hours. Suggest 24 Hrs so a backup archive is kept once a day. That means when a new backup is created, the previous interim one is deleted so long as another older but less than this age exists.

GZip Backups 				Default gzip 
Enable GZipping of auto-backup files. Once a backup has been successfully GZipped the uncompressed original is automatically deleted.

Split Backups On Time-out 		Default extend
Split backups if approaching maximum execution time or extend execution time. Splitting reduces problems with admin page loads/updates if backup takes too long but may result in invalid backups if database changes during split. If your server is operating in safe mode this setting is ignored & splitting is always enabled.

Auto Backup Displayed Time Format 	Default true 
Just for convenience, True = Use the auto backup date/time format ie 23rd Feb 2012 15:42 or False = use your system date/time format, ie 23/02/2012 15:34:33

Auto Run Auto Backup Delete 		Default false
Auto Backup Delete is meant to be called as a cron job, however you can enable auto-backup to call it, which it will do whenever the log file is over a day old or does not exist. The log file is named 'removed_backup.log'.

Email cron backups.			Default false
Enable automatic emailing of cron job backups on completion. By default backups will be emailed to the store owner as set in Admin->My Store->E-Mail Address.

Backup Delete Interval 			Default 30 (days)
The number of days to keep backups before they are permanently removed. This will apply as soon as Auto Backup Delete is run (by any method) and all datestamped backups will be examined (ie db_oscommerce_201202191637.sql.gz).

Ensure you have any files you may still need copied elsewhere before running Auto Backup Delete for the first time.

NOTE: If no current backups are found (by the admin settings) the delete process will be aborted.






Installation


Please read through all the instructions first & make sure you understand them, for safety you should always backup your site before making any changes. You can use Backup of all store files in zip format to make that process easier

1.- Copy auto_backup_db.php to /admin/includes/

2.- Copy auto_backup_delete.php to /admin/includes/

3.- Copy mail_backups.php to /admin/includes/

4.- Copy mail_cron_backups.php to /admin/includes/

5.- Copy auto_backup.php to /admin/includes/languages/english

6.- Copy auto_backup_setup.php to /admin

7.- Ensure you have already logged into your site admin & you have browsed so the sid (?osCAdminID=xx) no longer appears in the uri, then goto Auto Backup Setup by pasting into your browser address field something like: www.mysite.com/catalog/admin/auto_backup_setup.php

That will check your installation, add the settings to the database, create the backup directory if needed (its normally at catalog/admin/backups) and edit your application_top.php file to ensure Auto Backup is called.

Before auto backup setup modifies your application_top.php file it will create a backup of it in the same folder, named application_top.php.backup or similar (if that exists already).

If you prefer to make the changes to application_top.php manually, they are:

In "admin/includes/application_top.php" before the last line (?>) add the following line:
if (tep_session_is_registered('admin') && ENABLE_AUTO_BACKUP != 'false') require(DIR_WS_INCLUDES .'auto_backup_db.php');
This will only work if you log in to admin & create a admin session, if you use cpanel to log in you won't create the session, so to get around that, replace the above with:
require(DIR_WS_INCLUDES .'auto_backup_db.php');

That's the install complete! (but see below for osC2.2 & earlier) The following is only required to setup cron jobs:

8.- If you wish to set up a cron job for regular timed backups:

Click on 'Cron Jobs' in your hosting control panel
Select 'standard mode' (if that option is given).
Add your e-mail address to the e-mail field.
Select a suitable time in the time fields,
Insert the following in the command box:
php -n $HOME/public_html/catalog/admin/includes/auto_backup_db.php > $HOME/public_html/catalog/admin/backups/backup.log

Click on 'Save.

If you have changed the name of your admin directory, replace the 'admin' above, if your store is in the root, remove both the 'catalog/' above.
If the job fails to run you will receive an e-mail with details.
If the job runs but there are other errors, details will appear in the admin/backups/backup.log file.
If you exclude the '> $HOME/public_html/catalog/admin/backups/backup.log' above you will receive an e-mail every time the job is run, regardless of error state. However that e-mail will show the full path to your admin, so a security risk, so best leave that bit in.

The -n option is important for large dBases to ensure max_execution_time can be ignored.

The precise cron command does vary by server, refer to your host's documentation for details applicable to yours.

9.- If you wish to set up a cron job for regular deletion of old backups, its as 8. above but the command is like:

php $HOME/public_html/catalog/admin/includes/auto_backup_delete.php > $HOME/public_html/catalog/admin/backups/removed_backup.log

Upgrading:

Just copy the files as in 1 to 6 above, run the new setup routine as in 7, that's it. The old settings will be overwritten, so check if the defaults are suitable.

Addition for osC 2.2 and earlier:

If installing on osC 2.2 or earlier Auto Backup Setup and Database Backup Manager require an additional file for layout, template_top.php just copy that file to your admin/includes folder.

Testing Install:

Auto Backup Setup will report if the installation is successful, and setup has past all checks OK!.

Is a successful install Auto Backup Setup will also provide a button to test the Auto Backup Process, this will force a backup process, regardless of time limits and Auto Backup will then report through messagestack what it does, even if no backup is required.


Database Backup Manager Modifications

As logfiles are created in the backup folder, the following modifications to Database Backup Manager (http://addons.oscommerce.com/info/5769) are required to enable display/deletion of same

If you already have version 5.1.1 of Database Backup Manager then you've nothing further to do.

1.- Install Database Backup Manager as per instructions in its contribution file.

2.- Replace /admin/includes/languages/english/backup.php with the included file

3.- Replace /admin/backup.php with the included file

4.- If wished replace /admin/includes/functions/db_backup.php the only alteration being to replace depreciated ereg function

The modifications to the original are:






Requires PHP 5, SQL 4 or 5, any osC upto 2.3.1 and is register_globals off compatible.