IMPORTANT NOTE: Please make sure that you set up the cron job "command type" to be Perl to properly execute the shell script (.sh file). Cron jobs that run over 900 seconds (15 minutes) are automatically terminated.
For these examples make sure to replace "/SOURCE/DIRECTORY/" and "/DESTINATION DIRECTORY" with the appropriate Web directories (like "/mnt/stor1-wc1-dfw1/123456/www.example.com/web/content/archives/").
If you're scripting file compression, create a file named "compress.sh" to start.
To compress a directory to zip format add these lines to the script:
#!/bin/sh zip -9pr /DESTINATION/DIRECTORY/file.zip /SOURCE/DIRECTORY/
Where "file.zip" is the name that you assign to the zip file.
If you're only compressing a single file the script would be similar, but would not require the "r" in the options:
#!/bin/sh zip -9p /DESTINATION/DIRECTORY/file.zip /SOURCE/DIRECTORY/targetfile.txt
Put this in the script to archive a directory in tar format:
#!/bin/sh tar -cvf /DESTINATION/DIRECTORY/file.tar /SOURCE/DIRECTORY/
Where "file.tar" is the name that you assign to the archive file.
Put this in the script to archive and compress a directory into a gzipped tar format:
#!/bin/sh tar -cvzf /DESTINATION/DIRECTORY/file.tar.gz /SOURCE/DIRECTORY/
Where "file.tar.gz" is the name that you assign to the compressed file.
If you're scripting file extraction, create a file named "decompress.sh" to start.
Add these lines to decompress from zip format:
#!/bin/sh unzip -o /SOURCE/DIRECTORY/file.zip -d /DESTINATION/DIRECTORY/
Where "file.zip" is the name of the zip file to be uncompressed.
(NOTE: the -o option will force unzip to overwrite existing files!)
Put this in the script to extract from tar format:
#!/bin/sh tar -xvf /SOURCE/DIRECTORY/file.tar -C /DESTINATION/DIRECTORY/
Where "file.tar" is the name that you assign to the compressed file.
Put this in the script to extract from tar format:
#!/bin/sh tar -xvzf /SOURCE/DIRECTORY/file.tar.gz -C /DESTINATION/DIRECTORY/
Where "file.tar.gz" is the name of the compressed file.
© 2011-2013 Rackspace US, Inc.
Except where otherwise noted, content on this site is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License

15 Comments
I wish you guys would make a
Re: Suggestion
I have battled with this for
So, here is my solution. Running this script will allow you to browse/manage files on your server also allowing you to zip and unzip!
http://extplorer.sourceforge.net/
Clarify?
#!/bin/sh
tar -xzvf /{webdir}/legacy/legacy.tar
I then set up a PERL type crontab with the following command:
/{webdir}/legacy/decompress.sh
Right? Wrong? Not seeing any action.
Re: clarify
I found these instructions
Will this happen in only some cases? Or should these instructions be corrected?
Thanks,
Anna
Re: leading slash
The difference between "/SOURCE/DIRECTORY" and "SOURCE/DIRECTORY" is that the first is an absolute path and the second describes a relative path.
The absolute path defines the path as if you were starting from the root directory of the server, "/", and describing it from there. The example at the beginning of the article that starts with "/mnt" is an example of an absolute path.
The one without the leading slash, "SOURCE/DIRECTORY", is called a relative path. If you're just specifying where the file is in relation to the account's home directory then the system fills in the home directory where that leading slash would be in an absolute path.
An absolute path can be safer in a script since it doesn't depend on where the system thinks its working directory is. But if you've specified a relative path that works with the script then you shouldn't have a problem and can keep using it in your script.
I also had to remove the / in
How to delete zip file after extraction
I too want the script to
re: Delete file
#!/bin/sh
zip -9p /DESTINATION/DIRECTORY/file.zip /SOURCE/DIRECTORY/targetfile.txt && rm -f /SOURCE/DIRECTORY/targetfile.txt
The "zip" and "rm" commands would all be on the same line, separated by the "&&".
Just make it easy to backup all of our sites.
Put a button on our main account page to backup all our stuff.
Tutorial is missing a synopsis (overview)
re: overview
Quick note for those with Windows hosting
Add new comment