Backup of files in the directory.

2011-12-01 2 min read bash Learning Linux

I was working on some scripts and the changes that I was making in the scripts was very dynamic, which I did want to keep backing up in the version control system. But for the peace of my mind, I wanted to keep a copy of the scripts, whenever it was in working state.

Since I had multiple files, so it would make more sense to have a script that could copy all the files in the current directory to “old” directory without over-writing the existing files. So, I wrote a script that would postfix the files with a number. With this approach, finally what I had was the following:

#!/bin/bash -
#===============================================================================
#
#          FILE:  backup.sh
#
#         USAGE:  ./backup.sh
#
#   DESCRIPTION:  Backup all the current files.
#
#       OPTIONS:  ---
#  REQUIREMENTS:  ---
#          BUGS:  ---
#         NOTES:  ---
#        AUTHOR: Amit Agarwal (aka), amit.agarwal@roamware.com
#       COMPANY:
#       CREATED: 08/19/2011 02:43:32 PM IST
#      REVISION:  ---
#===============================================================================

count=$(cat old/count)
if [[ ! -f old/count ]]
then
  count=$(ls -1 old |sed 's/.*\.//'|grep -vi "[a-z]"|sort|tail -1)
fi
((count++))
echo $count >old/count
[[ ! -d old ]] && mkdir old
for i in *
do
	[[ -f $i ]] && cp $i old/${i%%.*}.$count
done
echo "Backed up to $count"
Enhanced by Zemanta
comments powered by Disqus