2

I wanted to backup my whole folder and files that are inside www or public_html and put it just one archive format (.tar or .zip). That way when downloading it would just be fast and not messy.

My cpanels filemanager offer this kind of feature to me but I don't have an access to my client's cpanel. I only have access to his FTP where I can put php scripts there and just run those scripts.

I'm looking for a backup type of utility.

Cyclonecode
  • 29,115
  • 11
  • 72
  • 93
Pennf0lio
  • 3,888
  • 8
  • 46
  • 70
  • 2
    And you want to zip `/root` folder? It means PHP would have access to `/root` folder. It's not secure. – s.webbandit May 25 '12 at 09:00
  • Sorry, what i mean is just inside my 'public_html' or 'www' folder. I'm kinda confuse if it's called 'root' or not. – Pennf0lio May 25 '12 at 09:17

2 Answers2

2

With the correct permission you could use exec():

<?php
      exec("tar czf backup.tar.gz /path-to-dir");
?>
Cyclonecode
  • 29,115
  • 11
  • 72
  • 93
  • "correct" meaning "foolish" ? You're right but there is probably a better solution to the real problem than to give php read access to /root... – Denys Séguret May 25 '12 at 09:03
  • @dystroy - I assumed he was talking about the web root. – Cyclonecode May 25 '12 at 09:15
  • 1
    You're right, OP fixed his post. – Denys Séguret May 25 '12 at 09:29
  • in top 10 google result, so i would rather suggest http://stackoverflow.com/questions/4914750/how-to-zip-a-whole-folder-using-php?answertab=active#tab-top as a solution.. If your needing to zip anything outside of your web root folder / localhost access.. then you would use this answer. If a person took this example and added a user / client variable.. it could become deadly.. Or more simply use a proper php zip / class.. Not a raw exec to your os. – Angry 84 Mar 06 '15 at 06:24
  • With "$_SERVER['DOCUMENT_ROOT']" .. Please Note The Unset at the end, i suggest removing that of course. – Angry 84 Mar 06 '15 at 06:31
1

Assumption

I am going to assume that you don't actually mean the system root directory /, but the root directory of your website.

Using tar via exec

If your user has the ability to exec tar then you could just use a simple script like so:

exec('/usr/bin/tar -czf /home/pennfolio/backup.tgz /home/pennfolio/www');

PHP with Zip

Should you not have the ability then you could use PHP's Zip functionality, but it would require you to recursively drop through the folder structure and add each file to a ZipArchive instance.

There are answers that cover this already on StackOverflow like How to [recursively] Zip a directory in PHP?

Community
  • 1
  • 1
Treffynnon
  • 21,365
  • 6
  • 65
  • 98
  • yes, i think this is relevant. how many I know it's done archiving and is ready for download? or it didn't catch any errors... – Pennf0lio May 25 '12 at 09:21
  • Look at the return status code from exec. See the manual page for more information: http://php.net/manual/en/function.exec.php – Treffynnon May 25 '12 at 09:26