Create tar files archive of files on server using PHP script
In this article we will share a nice little script we made to easily achive all files and folders (or certain directories) into a single compressed .tar file. This is ideal for moving your site between servers or to backup your complete server contents into a single file. It works fast and neat using PHP`s Phar. Make sure to also checkout our script to easily extract this archive on your server via another simple PHP script. Here`s how to use it:
Step 1: download the script below, save it as ‘archiver.php’
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<?php try { //make sure the script has enough time to run (300 seconds = 5 minutes) ini_set('max_execution_time', '300'); ini_set('set_time_limit', '0'); $target = isset($_GET["targetname"]) ? $_GET["targetname"] : 'archive.tar'; //default to archive.tar $dir = isset($_GET["dir"]) ? $_GET["dir"] : './.'; //defaults to all in current dir //setup phar $phar = new PharData($target); $phar->buildFromDirectory(dirname(__FILE__) . '/'.$dir); echo 'Compressing all files done, check your server for the file ' .$target; } catch (Exception $e) { // handle errors echo 'An error has occured, details:'; echo $e->getMessage(); } ?> |
<?php try { //make sure the script has enough time to run (300 seconds = 5 minutes) ini_set('max_execution_time', '300'); ini_set('set_time_limit', '0'); $target = isset($_GET["targetname"]) ? $_GET["targetname"] : 'archive.tar'; //default to archive.tar $dir = isset($_GET["dir"]) ? $_GET["dir"] : './.'; //defaults to all in current dir //setup phar $phar = new PharData($target); $phar->buildFromDirectory(dirname(__FILE__) . '/'.$dir); echo 'Compressing all files done, check your server for the file ' .$target; } catch (Exception $e) { // handle errors echo 'An error has occured, details:'; echo $e->getMessage(); } ?>
Step 2: Call the script
- Upload the script to your server
- Go to yourwebsite.com/achiver.php to call the script
Please note that the script wil by default tar all files and (sub)folders from the directory you uploaded the script to, into a file named archive.tar. You can overwrite the directory and archive name by calling the script with your own parameters, like this (replace the italic text for your own needs):
www.yourwebsite.com/archiver.php?targetname=achivename.tar&dir=relative/folder/to/put/in/archive/
Make sure to checkout how to extract files from an (.tar) archive like this.
No comments yet.