[CentOS] backup script

Tue Jan 25 17:00:52 UTC 2011
Cameron Kerr <cameron at humbledown.org>

On 25/01/11 21:56, madunix at gmail.com wrote:
> Am thinking to have this in my script
> 
> #!/bin/bash
> tar -cvzf /tmp/website-$(date +%Y%m%d-%H%M).tgz /var/www/htdocs/*
> find /tmp/website/website*.tgz  -ctime +5 -exec rm {} \; # removes
> older then 5 days

That should do in your case. Though, in general, you would prefer the
following  (because, in the general case, that glob could match a _lot_
of things, though in _your_ case, it should be fine).

find /tmp/website/ -name website\*.tgz -ctime +5 -exec rm {} \;

Also, from a security standpoint (especially if your website contains
things private materials the webserver would not serve), you should use
umask to change the default permissions the archive is assigned. You can
set this temporarily as follows:

(umask 077; tar ....)

The (...) construct defines a _subshell_. A umask specifies the mode
bits to clear on a new file, so 077 causes new files to be created as
rw-------. Umask is a property inherited from parent process to child
processes, and is in effect until either changed or the parent proces
(the shell, typically) ends.

The umask _command_ (actually, _shell-internal_ command) affects the
umask of the shell process, which causes the tar child process to see
the change). To prevent subsequent processes also getting that same,
restrictive, umask, I've used a sub-shell (the round-brackets), to limit
the scope of the umask effect to just the tar command.

PS. You're not really keeping your website backups in /tmp, are you?