Software, your way.
burger menu icon
WillMaster

WillMaster > LibraryWebsite Owner Tools

FREE! Coding tips, tricks, and treasures.

Possibilities weekly ezine

Get the weekly email website developers read:

 

Your email address

name@example.com
YES! Send Possibilities every week!

The Easy Backup

Backups often aren't made because it takes time away from other productive activities.

Still, if you ever need a backup file, you'll really, really need it.

How about an easy one-click backup; would that work for you?

Zip and Download is the basis for an easy, one-click backup system.

From Terri Zwierzynski:

Photo, Terri Zwierzynski of SoloWiz Services

"This is an awesome way to do backups. It literally takes me seconds — less than a minute — to get a backup that is downloaded to my computer! Thanks, Will:)"

SoloWiz Services - Making Your WordPress Website Beautiful and Worry‑Free

Once Zip and Download is set up, simply click on a link and tell your browser where to save the zip file.

That's it. Unless you have more than one directory to download. In that case, click the next link.

Note: It's easy. But it's also light weight. If you have large files or directories with many files to be zipped up and downloaded, or need to back up MySQL tables, use different software. Two software titles, and what each is good for, are mentioned near the end of this article.

The setup —

It isn't just the backups that are easy. The setup is fairly easy, too.

In essence, this is it:

  1. Copy the source code for Zip and Download from this article. Upload it to your server. (No customization required.)

  2. Ensure there's a directory on your server where Zip and Download can store temporary files.

  3. Create a link to Zip and Download for every directory that's to be zipped up and downloaded. (Separate files can be done individually, if you prefer.)

That's the setup.

The Zip and Download link can be on an HTML web page file on your hard drive that you load into your browser. It can be on your personal portal page. Or it may be put on any other web page that you visit every day.

The backup —

To make a backup, click the link and tell the browser where to save the zip file.

Click the link every week. Or every day. Whatever frequency you want for your backups.

More Detailed Setup Instructions

Setup consists of (i) uploading the Zip and Download script to your server, (ii) ensuring a directory is available for temporary files, and (iii) creating download links to click.

Step one —

Copy the Zip and Download PHP source code and save it as ZipAndDownload.php (or other .php file name you prefer).

Here's the Zip and Download PHP source code:

<?php
/* 
   Zip & Download
   Version 1.0
   December 5, 2015

   Will Bontrager Software LLC
   https://www.willmaster.com/
   Copyright 2015 Will Bontrager Software LLC

   This software is provided "AS IS," without 
   any warranty of any kind, without even any 
   implied warranty such as merchantability 
   or fitness for a particular purpose.
   Will Bontrager Software LLC grants 
   you a royalty free license to use this 
   software provided this notice appears 
   on all copies. 
*/

$TemporaryFileName = 'TemporaryCompressionFile';
if( ! class_exists('ZipArchive') ) { ExitWithMessage('Unfortunately, the ZipArchive class isn\'t available.'); }
if( empty($_GET['file']) and empty($_GET['dir']) ) { ExitWithMessage('No files specified.'); }
if( empty($_GET['temp']) ) { ExitWithMessage('No temporary directory specified.'); }
$tempdir = $_SERVER['DOCUMENT_ROOT'].preg_replace('!/*$!','',$_GET['temp']);
if( ! (file_exists($tempdir) and is_dir($tempdir)) ) { ExitWithMessage("Temporary directory '{$_GET['temp']}' doesn't exist."); }
$list = array();
if( isset($_GET['file']) )
{
   $file = "{$_SERVER['DOCUMENT_ROOT']}/{$_GET['file']}";
   if( file_exists($file) ) { $list[] = $file; }
}
elseif( isset($_GET['dir']) )
{
   $_GET['dir'] = preg_replace('!/*$!','/',$_GET['dir']);
   foreach( glob($_SERVER['DOCUMENT_ROOT']."{$_GET['dir']}*") as $file )
   {
      if( is_file($file) )
      {
         $file = preg_replace('!^.*/!','',$file);
         $list[] = "{$_SERVER['DOCUMENT_ROOT']}{$_GET['dir']}$file";
      }
   }
}
if( ! count($list) ) { ExitWithMessage('No files found.'); }
$tempdest = "$tempdir/$TemporaryFileName.zip";
if( file_exists($tempdest) ) { unlink($tempdest); }
$zip = new ZipArchive();
if( $zip->open($tempdest, ZipArchive::CREATE) !== TRUE ) { ExitWithMessage('Unable to create file in temporary directory<br>(ensure directory has sufficient permissions).'); }
foreach( $list as $source ) { $zip->addFile( $source, preg_replace('!^.*/!','',$source) ); }
$zip->close();
$filename = preg_replace('!^.*/!','',$tempdest);
header('Content-Type: application/octet-stream');
header("Content-Disposition: attachment; filename=\"$filename\"");
header('Content-Transfer-Encoding: binary');
readfile($tempdest);
exit;

function ExitWithMessage($s)
{
   echo $s;
   exit;
}
?>

Upload the ZipAndDownload.php to your server and make a note of its URL.

Zip and Download requires the ZipArchive PHP class be available. To find out if it's available, go ahead and load the URL of Zip and Download in your browser. It will tell you if ZipArchive isn't available.

Step two —

Zip and Download needs a directory where it can store temporary files. It uses the directory while it's compressing files and also for the zip file that downloads.

The directory can be an existing directory or you can make a directory especially for Zip and Download's use.

The directory must be writable. On some servers, default permissions 755 is sufficient. On others, you may need to specify 777 permissions.

Make a note of the directory location.

A note about directory location and security: The directory must be in a location that would normally be accessible with a browser. However, it may be password protected (which would require you to provide the password after you click the Zip and Download link). The reason you might want to protect the directory is so others can't download the zip file.

Step three —

This is where the Zip and Download links are constructed. You'll need both the URL to where Zip and Download is installed (from step one) and the directory location where Zip and Download will store temporary files (from step two).

Let's first create the URL for the links. Then talk about where to put the links.

Creating the URL —

For these examples, I'll assume Zip and Download is installed at http://example.com/ZipAndDownload.php and the directory for temporary files is /files (adjust the examples according to your URL and locations).

Here are the formats for the two types of links, (i) to zip up all the files in a directory into one file and download it and (ii) to zip an individual file and download it:

http://example.com/ZipAndDownload.php?dir=/books/data&temp=/files
http://example.com/ZipAndDownload.php?file=/books/log.txt&temp=/files

There are three items in each URL to mention.

  1. Colored blue: To zip and back up all files in a directory specify dir and to zip and back up an individual file specify file

    Specifying dir will zip up and download the files in the specified directory, but not any subdirectories. To back up its subdirectories, make additional links.

    Specifying file will zip up and download only that one file.

  2. Colored green: The green is either the directory location (dir) or the location of the file (file).

    Locations are the URL minus the http and domain name parts. As examples, the location /books/data is obtained from URL http://example.com/books/data and location /books/log.txt is obtained from URL http://example.com/books/log.txt.

    Only directories and files with locations that could be derived from URLs can be accessed and processed by Zip and Download.

  3. Colored red: The red location is the directory location where Zip and Download will put any temporary files and the file to automatically download.

    As noted for green locations, /files is derived from URL http://example.com/files minus the http and domain name parts.

Create one URL for each directory or individual file you want a one-click backup link for.

Where to put the links —

Zip and Download links use the URL or URLs you just created. Here are the two example URLs as links:

<a href="http://example.com/ZipAndDownload.php?dir=/books/data&temp=/files">Back up books data</a>
<a href="http://example.com/ZipAndDownload.php?file=/books/log.txt&temp=/files">Back up books' log.txt</a>

Use link text (and/or an image) according to your preference.

Put the Zip and Download links where you'll see them.

If you have a personal portal page that you visit every day, it may be an ideal place to put your Zip and Download links.

The Zip and Download links may also be put in an HTML file (which makes it a web page). It can be saved to your hard drive, perhaps on your desktop. Double-click on it to load it into your browser so you can click on the links.

What the Easy Backup System Can and Cannot Do

The maximum number of files in a directory and their maximum file sizes that Zip and Download can handle depend on your server's resources. The amount of memory it has available and how long it takes to ZIP up the files both have an effect on what the maximums are.

Because server capabilities are so diverse, you'll need to test to find out what your server's capabilities are. Try it. If it works, it works. If it hangs up or times out, the files were too large or there were too many files in the directory.

For a large number of files, the WebSite's Secret exclusive Directory Zip works for us to zip up entire sites. The zip file is stored on the server for manual download.

Zip and Download cannot download MySQL databases. To back up MySQL databases, use Database Back Up and Restore.

Zip and Download works wonderfully for easy backups. One click.

For directories and for specific files that need to be backed up frequently, it can be ideal. So long as it's used within its limits.

(This article first appeared in Possibilities ezine.)

Will Bontrager

Was this article helpful to you?
(anonymous form)

Support This Website

Some of our support is from people like you who see the value of all that's offered for FREE at this website.

"Yes, let me contribute."

Amount (USD):

Tap to Choose
Contribution
Method

All information in WillMaster Library articles is presented AS-IS.

We only suggest and recommend what we believe is of value. As remuneration for the time and research involved to provide quality links, we generally use affiliate links when we can. Whenever we link to something not our own, you should assume they are affiliate links or that we benefit in some way.

How Can We Help You? balloons
How Can We Help You?
bullet Custom Programming
bullet Ready-Made Software
bullet Technical Support
bullet Possibilities Newsletter
bullet Website "How-To" Info
bullet Useful Information List

© 1998-2001 William and Mari Bontrager
© 2001-2011 Bontrager Connection, LLC
© 2011-2024 Will Bontrager Software LLC