Creating File Backups in UNIX

Published by Utkarsh Patel on 08/02/09 16:32:31
Last edited on 18/04/09 00:17:11

An important process for any company is to make back up of it's data. This helps during the debugging process of programs that make use or create the data. The following code will take in 4 parameters, create a back up of the file by adding a date time extension to it and scans the back up folder for any files that are older than a specified number of days and deletes them

#!/bin/ksh
fileName=$1
filePath=$2
backupDir=$3
olderThan=$4
datetime=date +"%C%y%m%d_%H%M%S"
cp $filePath"/"$fileName $backupDir"/"$fileName_$datetime
find $backupDir"/"$fileName* -mtime +7 -exec rm -f {} \;

The first parameter will be the file you want to create a backup of. The second parameter the path to that file, the third parameter is the path to the backup directory and the fourth paramet is the number of days you want the file to be kept for before it is deleted. I saved the file as mkbkp.sh and called it in my program like this

fileName=testFile.php
fileDirectory=/tmp
backupDir=/backup/folder
numOfDays=60
[program code]
mkbkp.sh $fileName $fileDirectory $backupDir $numOfDays

About the Author

Utkarsh Patel is one of our newest members, and he could not have come at a better time. He has helped bring back stability and control to Techlicity Ventures during our rapid growth. If he isn't marvelling at a recently discovered Oracle feature or technique he is bothering the rest of the team on why we should move everything over to Oracle databases. Utkarsh complements the team well with his determination and sound research.

Bookmark and Share
Blog Widget by LinkWithin
blog comments powered by Disqus

Valid XHTML 1.0!