BASH Script to Back Up Files Modified Files in Linux


Here is a BASH Script which will back up the files created in the current directory within the past 24 hours in a “.tar.gz” file. Bash, an acronym for Bourne-again Shell, is a Unix shell written for the GNU Project and is the default shell for Linux and Mac OS X. let you accomplish a group of tasks quickly and without much intervention from the user like batch files in Windows.

#!/bin/bash
FILEBACKUP=backup-$(date +%m-%d-%Y)
incrback=${1:-$ FILEBACKUP}
tar cvf—`find . -mtime -1 -type f -print` >
$incrback.tar
gzip $incrback.tar
echo “The directory $PWD has been
backed up in archive file
\”$incrback.tar.gz\”.”

Line 2 of this script embeds the date in the backup filename. Line 3 introduces the string variable incrback as the filename of the archive. Line 4 tar condenses the current directory into a single tar file, while the fifth line gzip compresses the resultant tar file to the final tar.gz output file. The last line prints to the console that the current directory has been backed up to the archive file.

Filed under: Linux
Tags: , , , , ,
June 22, 2009 by: Prasanth Chandra

Comments

Leave a Reply