Backup of files in the directory.

2011-12-01 233 words 2 mins read

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

author

Authored By Amit Agarwal

Amit Agarwal, Linux and Photography are my hobbies.Creative Commons Attribution 4.0 International License.

We notice you're using an adblocker. If you like our webite please keep us running by whitelisting this site in your ad blocker. We’re serving quality, related ads only. Thank you!

I've whitelisted your website.

Not now
This website uses cookies to ensure you get the best experience on our website. Learn more Got it