A small script: taritdate.sh tars directories with dates
This is a very simple script which I use often to create temporary backups of working directories. As complexity grows, I needed to have a script to save locally the state of my work, so that I can easily revert to a previous state. It came out that I also use it for backups. See it:
#! /bin/sh # # F.Mariotti: taritdate.sh # # Simple help text if [ -z $1 ]; then echo "please give a dir name." exit 1; fi; # # Make sure we have only one argument if ! [ -z $2 ]; then echo "Sorry but we only accept ONE directory only." exit 1; fi; # #Check that it is a directory mydirname=$1 if ! [ -d $mydirname ]; then echo "At present you can only tar a directory." exit 1; fi; # #Define the date format # mydate=`date +%d%b%Y` #01May2013 mydate=`date +%Y%m%d` #20130501 # #Remove slash at end if present thisdirname=`dirname $mydirname`/`basename $mydirname` # #Build first part of the tar name tarprename=${thisdirname}.${mydate}. # #Start trying with a plain name tarname=${thisdirname}.${mydate}.tar.gz # #If it exists add trailing decimals if [ -f $tarname ]; then i=0 while [ 1 ]; do i=$(($i+1)) tarname=${tarprename}${i}.tar.gz echo "WARNING: Tar file exists.. trying: $tarname" if ! [ -f $tarname ]; then break 1; fi; done fi; # #Create the tar archive tar -czvf $tarname $mydirname #
In my ./bin directory is called taritdate.sh and the use is pretty simple:
[mariotti@localhost tmp]$ ll drwxrwxr-x. 2 mariotti mariotti 4096 May 1 08:41 tmp-work [mariotti@localhost tmp]$ ll tmp-work total 0 -rw-rw-r--. 1 mariotti mariotti 0 May 1 08:41 file1 -rw-rw-r--. 1 mariotti mariotti 0 May 1 08:41 file2 -rw-rw-r--. 1 mariotti mariotti 0 May 1 08:41 file3 [mariotti@localhost tmp]$ taritdate.sh tmp-work tmp-work/ tmp-work/file1 tmp-work/file3 tmp-work/file2 [mariotti@localhost tmp]$ taritdate.sh tmp-work WARNING: Tar file exists.. trying: ./tmp-work.20130501.1.tar.gz tmp-work/ tmp-work/file1 tmp-work/file3 tmp-work/file2 [mariotti@localhost tmp]$ ll drwxrwxr-x. 2 mariotti mariotti 4096 May 1 08:41 tmp-work -rw-rw-r--. 1 mariotti mariotti 176 May 1 08:43 tmp-work.20130501.1.tar.gz -rw-rw-r--. 1 mariotti mariotti 176 May 1 08:43 tmp-work.20130501.tar.gz
Very simple but I still use it after years… feel free to adapt to your own use.
One comment