LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Server (https://www.linuxquestions.org/questions/linux-server-73/)
-   -   directory backup cron script (https://www.linuxquestions.org/questions/linux-server-73/directory-backup-cron-script-764270/)

goltoof 10-24-2009 08:51 PM

directory backup cron script
 
What's a good cron script for backing up and zipping a directory of files, or multiple directories with files, to a backup directory on my server, on a daily basis?

I found an easy to use mysql backup script, now I need to backup my site directory, but not all the directories in it. So I need a method in the script to omit certain directories from backing up, ie dirs that contain gigs worth of files.

This seems like it should be one of the most common crons to set a server up with but two pages deep in google (and here) I have yet to find anything remotely resembling a solution.

nilleso 10-24-2009 10:51 PM

first get your backup working then worry about adding it to cron. try something like this:
Quote:

tar --exclude=/mydir/sitedir/trash --exclude=/mydir/sitedir/mp3 -cvpzf /whereever/mysitedir.tgz /mydir/sitedir
this will backup all of /mydir/sitedir minus the trash and mp3 directories to a gzip-compressed tar file
then add the working command to croncron:
Quote:

1 1 * * * /usr/bin/bash -lc "placeentirecommandabovehere"

goltoof 10-25-2009 03:04 AM

Thanks, works fine except for the "exclude" option gives me invalid "e" token.

In addition to this I got to make it create a new backup for each day (by date), create a monthly permament backup in separate directory, and delete all daily backups that are older than 30 days.

nilleso 10-25-2009 10:21 AM

It's --exclude, not -exclude.

for your additional questions, do you have any ideas on how you're going to do that? this is starting to sound like homework

cheers :)

markush 10-25-2009 01:48 PM

Hello goltoof,

I'm using flexbackup (http://www.edwinh.org/flexbackup/) which is a perlscript. It has a configurationfile in the /etc-directory and one can define sets of directories to backup and one can do full, differential and incremental backups. Since I'm not running a Linux-server I manage the backups with the anacron-daemon.

Markus

goltoof 10-25-2009 07:19 PM

Quote:

Originally Posted by nilleso (Post 3731868)
It's --exclude, not -exclude.

for your additional questions, do you have any ideas on how you're going to do that? this is starting to sound like homework
cheers :)

It is homework... assigned by a catastrophic website failure causing me a lot of lost work :)

daily backup
Code:

* 0 * * * /usr/bin/bash tar --exclude=/web/webdir/images -cvpzf /backups/daily/site_`date +%Y%m%d_%H`.tgz /web/webdir
monthly backup
Code:

* * 1 * * /usr/bin/bash tar --exclude=/web/webdir/images -cvpzf /backups/monthly/site_`date +%Y%m%d_%H`.tgz /web/webdir
remove old files
Code:

* 0 * * * /usr/bin/bash find /backups/daily -mtime +30 -exec rm -f {} \;
now if I were only kinky enough to turn it into a single script instead of relying on multiple crons :P Thanks for you help!

chrism01 10-25-2009 08:22 PM

Here's some good scripting guides
http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/

I'd keep the mthly cron entry separate from the daily ones for timing ctrl, but call the one script with a param 'daily' or 'mthly'.

goltoof 10-25-2009 11:48 PM

Last step is to create an automated method to restore the website. In lieu of another catastrophe, instead of having to drop the current sitedir/db, then untar the chosen sitedir/db, inject the db and copy the sitedr all manually... instead have a script that'll backup the current site/db and then drop the current sitedir/db then untar and restore with the sitedir/db backups of a given date, all with one command.

Davvvvid 10-27-2009 01:22 PM

You might find these links useful:

http://www.desktoplinux.com/articles/AT2280165098.html
http://www.debianhelp.co.uk/schedulejobs.htm

I read them and learnt quite a lot :)

Disillusionist 10-27-2009 03:40 PM

Looking at the man page for cron, the syntax is:
  1. Minute
  2. Hour
  3. Day of Month
  4. Month
  5. Day of Week
  6. Command

Code:

* 0 * * * /usr/bin/bash tar --exclude=/web/webdir/images -cvpzf /backups/daily/site_`date +%Y%m%d_%H`.tgz /web/webdir
In this example, the script would run every minute from 0:00 to 0:59!

Likewise your Monthly script would run every minute of the day!

I am assuming that this is not what you are intending...


All times are GMT -5. The time now is 04:37 AM.