Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

If I wanted to zip up (or tar.gz up) a list of paths (recursively) on a Linux VPS, then email the ZIP/TAR to myself, then delete the ZIP/TAR, what's the easiest way to do this? A shell script on a cron job, a program, etc.?

For example, here's what I'd likely do:

  1. Stop apache, mysql, postgresql, and rack

  2. Zip up:

    # etc.
  3. Email the zip file as an attachment to

  4. Delete the zip file

  5. Resume apache, mysql, postgresql, and rack

share|improve this question
/var/lib/mysql/ is quite likely to exceed the maximum size for an attachment, even when zipped. You might want to consider scp instead. – Ladadadada Jun 20 '12 at 8:14
May I suggest doing mysqldump instead of stopping mysql and copying the datafiles? The dump will compress well and not include indexes and log data. – ptman Jun 20 '12 at 10:54
up vote 5 down vote accepted

You could use the next script, update if of course with your info:


[ -f /etc/redhat-release ] && service httpd stop
[ -f /etc/debian_version ] && service apache2 stop
service mysqld stop
service postgresql stop
#Do the same for rack, not sure what the service is called.

zip -r /tmp/ /etc/httpd/conf/httpd.conf /etc/httpd/conf.d/ /home/kerrick/ /var/lib/mysql/ # etc.
mail -s "test" <  /tmp/
rm -f /tmp/

[ -f /etc/redhat-release ] && service httpd start
[ -f /etc/debian_version ] && service apache2 start
service mysqld start
service postgresql start
#Do the same for rack, not sure what the service is called.

Make it run as a cron if needed. But indeed would be better to scp or ftp for instance rather then emailing as the zip package might be too big and will fail to be sent as an attachment.

share|improve this answer
I would use zip -9r for better compression and mail ... && rm ... So that the backup file is deleted only after a successful email. – jftuga Jun 20 '12 at 10:17

The easiest of course would be a bash-script running as a cron job, containing:

  • Stop Apache, MySQL, PostgreSQL, and Rack using the service command or /etc/init.d script invocations
  • One or several tar commands to create tarballs of the files you want to compress -- put the tarballs into a temporary dir e.g. /tmp
  • Send the tarballs using any mailer program that can handle attachment -or-
  • Do a base64-encode of the tarballs and pipe the result to the mailer program
  • Delete the tarballs in /tmp (or whatever directory you're using)
  • Start the services again

In addition, you can put in 'instrumentation' commands between all steps, writing into a (custom) logfile or to a syslog server.

share|improve this answer
Unfortunately, I know diddly-squat about bash scripts. Could you please show me an example that'd do these things that I could modify to fit my needs? – Kerrick Jun 20 '12 at 8:39

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.