Tar command using PHP

Hello Sitepoint,

After a good amount of RTFM on the tar command for *nix, I am backed into a corner and hope someone could point me in the right direction. I am currently trying to make a PHP script that will dump my database, and tar, bzip my public_html folder to a backup folder that I will potentially try to rsync to my local machine to move off site.

The database portion went great(Thanks to PHP anthology sitepoint book!), but now I am trying to come up with a similar class that will handle the public_html directory, and I just can not get the system command to work correctly.

Just some background, my back_it_up.php script(which will be the script that will handle the processing) resides in my home/usr/backups/ directory. My public_html directory from where my backup script is …/public_html/.

What I was thinking of doing would just to use PHP to run another system command like so:

tar cvfj backup.tar.bz2 -C ../ public_html

This works fine for the public_html folder, but there is one particular folder I would like to archive, zip seperately that is inside public_html and I would just like the contents of that directory. So then I try this:

tar cvfj seperateDir.tar.bz2 -C ../ public_html seperateDir

And what I get is all of public_html and seperateDir, including the folder when I just want the contents. I have looked exhaustivly at resources on the tar command, but all the examples I find do not necessarily cover what I am trying to do. Does anyone have any thoughts or suggestions? Or possibly point me in the right direction? I am assuming this is so simple that I have missed in completely and I will feel like…well you know when I finally find the solution.:blush:

Thanks Sitepoint!

I am not fully familar with tar on *nix environments. However, could not not be very easliy achieved just using another tar command instead of trying to do it in one?

Sorry, I meant Unix or Linux by the *nix acronym. I suppose I could do it with more than one command but I like to try to be as efficient as possible and what I gather from the documentation, it can be done but there are not many good examples.

The only ones that I have seen are after you change the directory with -C flag, you are supposed to not include slashes in the path you want to change to. The best examples I have found are at http://www.gnu.org/software/tar/manual/html_node/directory.html. The only problem with those examples is that they are changing the directory to a file that is at the same level. Mine needs to be(from where the script is called) …/public_html/anotherDir/.

So when I try:

tar -cvfj backup.tar.bz2 -C ../ public_html anotherDir

I get all of public_html tar-balled and zipped and anotherDir as well. I just want the contents of anotherDir tar-balled and zipped.

Thanks in advance if anyone has any solutions or knows where there are some better examples.