Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I use simple set of shell commands to in order to create mysqldumps daily. The script is called by cron daemon. It looks like:

presentdate="`date +%d-%m-%Y_%H:%M.%S`"
basedir="/var/db_my_backup"
mysqldump -u username -ppassword --all-databases |
  gzip -9 -c > $basedir/mysqlbackup.$presentdate.sql.gz | 
  find -L $basedir/'*' -type f -mtime +7 exec rm -f {} \; |
  chown -R user:group $basedir | (
    cat <<EOF
Copy of MySQL successfully placed in $basedir directory as file named mysqlbackup.$presentdate.sql.gz. 
Now it is ready to be moved to QNAP backup device as scheduled.
EOF
    cat
  ) | /bin/mail -s "Report from scheduled job for mysqldumps" root

My concern is: how can I possibly make it more elegant? Would it be possible to build a function for selecting and renaming files that are older than 7 days? I don't like using find as it produces strange warnings like for example:

find: paths must precede expression
Usage: find [-H] [-L] [-P] [path...] [expression]

that are hard to deal with (at least for me) in such a script.

share|improve this question
    
find doesn't produce strange warnings if you use it correctly –  scai Aug 8 '12 at 11:54
    
Not having looked at any details (beware, there's an rm -f), it seems to me there's a dash missing in -exec, right? (backupninja does MySQL backups, too.) –  sr_ Aug 8 '12 at 11:58
add comment

1 Answer 1

up vote 1 down vote accepted

Your method of deleting old backups is fine, and in my opinion better than renaming files. The ugliest thing about it is the long chain of pipes, which serve little purpose in your example but make the line too long. I would write it using a block of { ... } like this:

presentdate="`date +%d-%m-%Y_%H:%M.%S`"
basedir="/var/db_my_backup"
{
mysqldump -u username -ppassword --all-databases | gzip -9 -c > $basedir/mysqlbackup.$presentdate.sql.gz
find -L $basedir/'*' -type f -mtime +7 -exec rm -v {} \;
chown -vR user:group $basedir
cat <<EOF
Copy of MySQL successfully placed in $basedir directory as file named mysqlbackup.$presentdate.sql.gz. 
Now it is ready to be moved to QNAP backup device as scheduled.
EOF
} 2>&1 | /bin/mail -s "Report from scheduled job for mysqldumps" root

The 2>&1 near the end is to include error output in the email, you can remove it if you don't want that. I also added some -v flags for more verbosity.

As a commentor put it, find does not give strange errors if you use it correctly. There was a mistake in your parameters to find, using exec instead of -exec. To get the parameters right play with it for a bit, while reading up on man find.

share|improve this answer
    
logwatch returns /etc/cron.daily/db_rename_mysqldumps: line 4: 5: command not found. The script creates backup files and names it. It just doesn't delete old entries (older than 7 days). Any idea what is going on there? –  domaniqs Aug 15 '12 at 11:50
    
I got the source of the problem with deleting files: '*' after path to $basedir/ shouldn't have been there. Still trying to figure out what's wrong with the error in lines 4 and 5 though. –  domaniqs Aug 15 '12 at 14:36
    
Please pastebin your script! From the error message it looks like there is a syntax error in your script, trying to execute the command "5" which does not exist. The "5" could come from a variable, for example you get the same error message if you do something like this: a=5; $a hello there –  janos Aug 16 '12 at 5:56
add comment

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.