Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # Purpose:
- # * Find version file ("version").
- # * Backup files in same directory.
- # * Create or update tar.gz archive based on version file.
- # Copyright (C) 2012 Savvas Radevic <vicedar@gmail.com>
- # This program is free software: you can redistribute it and/or modify
- # it under the terms of the GNU General Public License as published by
- # the Free Software Foundation, either version 3 of the License, or
- # (at your option) any later version.
- # This program is distributed in the hope that it will be useful,
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- # GNU General Public License for more details.
- # You should have received a copy of the GNU General Public License
- # along with this program. If not, see <http://www.gnu.org/licenses/>.
- # TEST CASE -- This command will create the required tree output of folder/files
- # dir=Parent; mkdir -p $dir; for z in {Vol1,Vol2,Vol3}; do for x in {D009,D010,S060,T020}; do echo $dir/$z/$x; mkdir -p $dir/$z/$x; for i in {xls,gif,vsd,mpp,mdb,html}; do echo $dir/$z/$x/${x}_v1.$i; echo $dir/$z/$x/${x}_v2.$i; touch $dir/$z/$x/${x}_v1.$i; touch $dir/$z/$x/${x}_v2.$i; done; echo $dir/$z/$x/version; echo 1 > $dir/$z/$x/version; done; done
- DST_DIR=~/Desktop/testme/Output/zipped
- SRC_DIR=~/Desktop/testme/Parent/Vol*/*
- VERSION_FILE=version
- DT=`date +%Y%m%d%H`
- #Create DST_DIR
- mkdir -p $DST_DIR
- echo "*** Checking if files/folders exist: $DST_DIR"
- ls -ld $DST_DIR
- echo "*** Checking if files/folders exist: $SRC_DIR"
- ls -ld $SRC_DIR
- #Search directory $d for files (-type f) that match (-name) "$VERSION_FILE"
- for ver in `find $SRC_DIR -type f -name "$VERSION_FILE"`; do
- #ver is file path of "version"
- echo -e "\n\n\n=== Processing version file ==="
- echo "*** Version file: $ver"
- #dirname is directory path of file "version"
- dirname=`dirname $ver`
- echo "*** dirname: $dirname"
- #Read first character only from $ver file
- version=`head --bytes=1 $ver`
- echo "*** version: $version"
- #Search directory dirname for files (-type f) that match (-name) "*_v${version}*"
- for f in `find $dirname -type f -name "*_v${version}*"`; do
- #dirpathname and cleanname are used for file naming the tgz file
- dirpathname=`echo $dirname | rev | cut -d / -f1,2 | rev | sed 's/\//-/'`
- echo "*** dirpathname: $dirpathname"
- cleanname=`echo $f | cut -d / -f 2 | cut -d _ -f 1`
- echo "*** cleanname: $cleanname"
- echo "*** Checking if files/folders exist"
- ls -ld $f
- backupfilepath="$DST_DIR/Proc_Sat-1-${DT}-S515-V${version}-OUNCL-P${dirpathname}-${cleanname}.tar.gz"
- echo "*** backup file path: $backupfilepath"
- # If backup file exists, append new files
- if [ -e $backupfilepath ]; then
- # u = update, z = gzip, v=verbose, f=filename
- # Note: tar --update does not allow to update gzipped files,
- # we unzip and rezip them.
- echo "*** backup file exists"
- # backupfiletar is .tar file path (stripped .gz from backupfilepath)
- backupfiletar="`echo -n $backupfilepath | head --bytes -3`"
- echo "*** backupfiletar: $backupfiletar"
- echo "*** decompressing"
- gunzip -vfd $backupfilepath
- if [ ! -f ${backupfiletar} ]; then
- echo "*** Error during backup"
- exit 1
- fi
- echo "*** updating"
- tar uvpf ${backupfiletar} $f
- if [ $? -ne 0 ]; then
- echo "*** Error during backup"
- exit 1
- fi
- echo "*** recompressing"
- gzip -vf9 ${backupfiletar}
- if [ ! -f ${backupfilepath} ]; then
- echo "*** Error during backup"
- exit 1
- fi
- else
- echo "*** backup file doesn't exist, creating a new backup file"
- # c = create
- tar czvpf $backupfilepath $f
- if [ $? -ne 0 ]; then
- echo "*** Error during backup"
- exit 1
- fi
- fi
- echo "*** Checking if backup file was created"
- ls -ld $backupfilepath
- echo -e "*** Done\n"
- done
- done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement