• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Incremental filename script

 
Kev D'Arcy
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I currently have a backup cron job running every hour to create a backup
of my code repository. The only issue at the moment is that the backup file
gets over-written each time the job runs.

Is there a way to script the appending of a number onto the file name so
that I can keep say the most recent 50 copies of the file? eg:

backup1.zip
.
.
.
backup50.zip

And when the number hits (say) 50 it'll return to 1?

Many thanks,

Kev
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18212
53
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I assume that either you're not using a version control system or else that you're really paranoid about your VCS server breaking down. If you're not using some sort of VCS, such as CVS, Subversion or even Visual SourceSafe, consider doing so. I backup my VCS's at 4:00 am every morning as a cron job, but don't keep versions of them - VCS systems are inherently cumulative. About once a week, the backup copies get burned out to DVD as long-term archives. I keep a couple of generations just in case the master VCS gets trashed, the online backup gets trashed and the latest DVD turns out to be unreadable.

I'm pretty paranoid myself.
 
Kev D'Arcy
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim,

I'm pretty paranoid too! We (will) have a nightly backup job to back up the repository, but I want my own backup jobs to run as well (very paranoid). It's more to do with being able to restore the repository quickly to another server (i.e. myself) rather than have to wait for a full restore by our server admin. I think I've managed to sort it out thought.

Kev
 
Brian Wright
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's a very basic way to do what you're looking for, I think. Note that there are a horde of ways to do this, I was just looking for something fairly easy to follow:

===========================================================
#!/usr/bin/ksh
LATEST=$(ls -1t backup*.zip | head -1 | cut -d. -f1 | sed "s+backup++g")

if [[ ${LATEST} -eq 50 ]]
then
LATEST=0
fi

NEW=$(( ${LATEST} + 1 ))


BACKUP_FILENAME="backup${NEW}.zip"
===============================================================

Hope this gives you something to play with!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic