2

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 [email protected]

  4. Delete the zip file

  5. Resume apache, mysql, postgresql, and rack

2
  • 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, 2012 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, 2012 at 10:54

2 Answers 2

5

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" [email protected] <  /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.

1
  • 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, 2012 at 10:17
1

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.

1
  • 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, 2012 at 8:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .