.gz Help Needed

They have: 12 posts

Joined: Jun 2007

I'm trying learn how to write a script to linux zip a file to a .gz file type. Someone emailed me the following syntax but it doesn't work and I'm lost. What I'm trying to do is take one of my existing *.sql files and make a *.gz file out of it so it's smaller.

exec("tar --directory=$save_dir -czf ". $save_dir . $dbname . "-" . $date . "_sql.tar.gz $dbname-$date.sql");

Thanks for any help...

pr0gr4mm3r's picture

He has: 1,502 posts

Joined: Sep 2006

Since you are using the exec() function, I'm assuming that you are running that in PHP. If you are just running it in a shell, you don't need the exec() function.

The basic way to run the command would be...

<?php
tar
-czvf file.sql file.sql.tgz
?>

Change the file name accordingly, and stick it into the exec() function if needed. Also, if you are running the command in a different directory than the files, be sure to provide the relative path along with the file.

They have: 12 posts

Joined: Jun 2007

I'm running it in a PHP script. I'll give your syntax a shot and see how it works.

Thanks...

They have: 12 posts

Joined: Jun 2007

I put the full path in

exec("tar -czvf /var/www/backup/wx.sql /var/www/backup/wx.sql.tgz");

but it still doesn't work. The wx.sql files is about 4800kb and after the statement is run their is only the wx.sql file (no .tgz file) and it's about 45k. I looked inside the file and it's unreadable so it must be tgz format but surely it can't compress a 4800kb file down to 45kb? But there is still no .tgz file.

pr0gr4mm3r's picture

He has: 1,502 posts

Joined: Sep 2006

The files are reversed. You specify the compressed file first. Sorry, that was my fault.

SQL files do actually compress quite a bit because have the the file has phrases like "INSERT INTO table_name", etc.

They have: 12 posts

Joined: Jun 2007

I think that did it. I got the .tgz file created and it's size seems about right at 631kb. To check it, how do I un tgz it back into it's .sql state?

The .sql file changed size too: went from 4800kb to 622kb - I thought the source file would not be changed?

They have: 12 posts

Joined: Jun 2007

I got it figured out and working now. The source file is ok. I extracted the source from the tgz and did a file compare and it's a perfect copy of the source.

Thanks for your help...

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.