• Sales: 1-800-961-2888
  • Support: 1-800-961-4454

Use cron to extract and compress (zip & unzip) on Cloud Sites


Scripting extraction and compression of files

General Process:

In Cloud Sites, the general process for extracting and compressing files through a cron job consists of the following steps:

  1. Creating the script File
  2. Uploading the aforementioned file to the site through FTP.
  3. Scheduling the cron job through the Cloud Sites Control Panel's Features tab.

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.

Creating the Script File

To create the script file, simply open up a text editor on your local machine and copy/paste the sample code (provided in the examples below) for the operation you wish to accomplish:

Once complete, save the file with the .sh extension. 

I. Compressing

Suggested Filename: compress.sh

NOTE: 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/").

Zip compression

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.

The final script should look similar to this:

Note: If you're only compressing a single file the script would be similar, but would not require the "r" in the options.

For example:

#!/bin/sh
zip -9p /DESTINATION/DIRECTORY/file.zip /SOURCE/DIRECTORY/targetfile.txt 

 

Tar.gz compression

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.

The final script should look similar to this:

II. Extracting

Suggested Filename: decompress.sh

Zip extraction

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.

The final script should look similar to this:

(NOTE: the -o option will force unzip to overwrite existing files!)

Where "file.tar" is the name that you assign to the compressed file.

Tar.gz extraction

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.

The final script should look similar to this:

Cron FAQ's:

What is a cron job?

How do I enable or disable cron?

How do I schedule a cron job?

How do I import a large mysql database?







© 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


See license specifics and DISCLAIMER