Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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/httpd/conf/httpd.conf
    /etc/httpd/conf.d/*
    /home/kerrick/*
    /var/lib/mysql/*
    # etc.
    
  3. Email the zip file as an attachment to foobar@example.com

  4. Delete the zip file

  5. Resume apache, mysql, postgresql, and rack

share|improve this question
1  
/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

2 Answers 2

up vote 5 down vote accepted

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

#!/bin/sh

[ -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/all_needed.zip /etc/httpd/conf/httpd.conf /etc/httpd/conf.d/ /home/kerrick/ /var/lib/mysql/ # etc.
mail -s "test" yourmail@yourdomain.com <  /tmp/all_needed.zip
rm -f /tmp/all_needed.zip

[ -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

 
discard

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.