Using Cron to Compress Files in Cloud Sites
Note: |
| The code provided below is only an example. Your specific website may require different code. As outlined in our Terms of Service and Cloud Sites Spheres of Support Expectations, we are unable to help you troubleshoot code issues. We recommend speaking with your developer before implementing any scripts. |
In order to compress files in Cloud Sites through a cron job, you will need to create a script, upload it via FTP client and then schedule it through the Cloud Sites control panel Cron tab. This tutorial will take you through the process of creating the script file and compressing it. To schedule the cron job, you can see our article Creating Cron Jobs in Cloud Sites.
Tip: |
| When setting up the Cron job, it is important that the script file selected is Perl to properly execute the shell script (.sh file). Cron jobs that run over 900 seconds (15 minutes) are automatically terminated. |
Create the Script File
Warning: |
| It is important to use caution when running unfamiliar scripts. If you are unfamiliar with running scripts for cron jobs, please consult a developer to help you deploy them. |
To create the script file, open the text editor of your choice and use the code below.
#!/bin/sh zip -9pr /DESTINATION/DIRECTORY/file.zip /SOURCE/DIRECTORY/re
Note: |
The SOURCE directory can be found in your Cloud Sites control panel in the site details section of Websites. Regardless of the technology you chose for your site, the directory you use will always be Linux. The DESTINATION directory will be the directory created for the compressed files to be moved to once compression is complete.![]() |
Compressing
To compress an entire directory to zip format, add the following lines to the script:
Note: |
| file.zip is the name that you assign to the file. See the section above for the Source and Destination directory information. |
#!/bin/sh zip -9pr /DESTINATION/DIRECTORY/file.zip /SOURCE/DIRECTORY/
To compress an individual file the script is similar, but does not require the r command, and will indicate the individual file at the end of the statement.
#!/bin/sh zip -9p /DESTINATION/DIRECTORY/file.zip /SOURCE/DIRECTORY/targetfile.txt
If you want to archive and compress a directory into a gzipped tar format, you can use the following script. Remember to input your Destination and Source information. You will use file.tar.gz as the name you assign to the compressed file.
#!/bin/sh tar -cvzf /DESTINATION/DIRECTORY/file.tar.gz /SOURCE/DIRECTORY/
