backup a file and keep every version of the backup

I am trying to backup my .bash_history and I want to keep every version of the backup. I am thinking to put one of these in my crontab.

0 0 * * 0,3 cat .bash_history > boo
0 0 * * 0,3 cp .bash_history boo

I would like the backups to be called boo1, boo2, boo3, etc. I would like to keep every version of the backup. So how would I go about changing this?

Instead of making boo1, boo2 etc. I'd suggest you to create file names with boo_<date> ; eg. today's file would be boo_20110119. It would be easier to identify also.

Try this:

0 0 * * 0,3 cp .bash_history boo_$(date +%Y%m%d)

But if you specifically want 1, 2 etc. you can write a small script to get the latest boo* file, then cut the string after '' , add 1 to that cut string as it would be a number, and create file with that name...

Put that shell script in cron

Thx. Could you also explain how to write that script?

a=`ls -rt ~/boo* 2>/dev/null | tail -1 | awk -F"_" {'print $NF'}`
a=`expr ${a:-0} + 1`
cp ~/.bash_history ~/boo_$a

This line will not work for the following reasons:
A percentage sign must be escaped in a crontab because "%" has special meaning in a crontab.
The filenames need to be full hierarchial names in this context.

---------- Post updated at 16:25 ---------- Previous update was at 16:18 ----------

What are the settings of all the HIST* environment variables in your .bashrc file? If they are not correct you will not retain all your history.

Imho the best way to save history is to change the value of $HISTFILE to make the filename unique (i.e. include date, time and terminal id).

Is this bad? Thats whats in my .bashrc.

HISTSIZE=9000 
HISTCONTROL=ignoreboth
source hist

To reiterate.
Imho the best way to save history is to change the value of $HISTFILE to make the filename unique (i.e. include date, time and terminal id).

If your intent is to record all commands entered by you from the shell prompt, this is a poor approach to take. With a HISTSIZE of 9000, I seen to recall the bash (and other shells) slow down significantly as the actual history count increases.

Methyl's approach is better. An even better approach is to send your command history to a logging server as the korn shell can do. There is a patch out there for bash. Just do a web search for it.