SHOW:
|
|
- or go back to the newest paste.
| 1 | #!/bin/bash | |
| 2 | # Purpose: | |
| 3 | # * Find version file ("version").
| |
| 4 | # * Backup files in same directory. | |
| 5 | # * Create or update tar.gz archive based on version file. | |
| 6 | ||
| 7 | # Copyright (C) 2012 Savvas Radevic <[email protected]> | |
| 8 | # This program is free software: you can redistribute it and/or modify | |
| 9 | # it under the terms of the GNU General Public License as published by | |
| 10 | # the Free Software Foundation, either version 3 of the License, or | |
| 11 | # (at your option) any later version. | |
| 12 | ||
| 13 | # This program is distributed in the hope that it will be useful, | |
| 14 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 15 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 16 | # GNU General Public License for more details. | |
| 17 | ||
| 18 | # You should have received a copy of the GNU General Public License | |
| 19 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| 20 | ||
| 21 | # TEST CASE -- This command will create the required tree output of folder/files | |
| 22 | # 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
| |
| 23 | ||
| 24 | DST_DIR=~/Desktop/testme/Output/zipped | |
| 25 | SRC_DIR=~/Desktop/testme/Parent/Vol*/* | |
| 26 | VERSION_FILE=version | |
| 27 | DT=`date +%Y%m%d%H` | |
| 28 | ||
| 29 | #Create DST_DIR | |
| 30 | mkdir -p $DST_DIR | |
| 31 | ||
| 32 | echo "*** Checking if files/folders exist: $DST_DIR" | |
| 33 | ls -ld $DST_DIR | |
| 34 | echo "*** Checking if files/folders exist: $SRC_DIR" | |
| 35 | ls -ld $SRC_DIR | |
| 36 | ||
| 37 | #Search directory $d for files (-type f) that match (-name) "$VERSION_FILE" | |
| 38 | for ver in `find $SRC_DIR -type f -name "$VERSION_FILE"`; do | |
| 39 | #ver is file path of "version" | |
| 40 | echo -e "\n\n\n=== Processing version file ===" | |
| 41 | echo "*** Version file: $ver" | |
| 42 | #dirname is directory path of file "version" | |
| 43 | dirname=`dirname $ver` | |
| 44 | echo "*** dirname: $dirname" | |
| 45 | #Read first character only from $ver file | |
| 46 | version=`head --bytes=1 $ver` | |
| 47 | echo "*** version: $version" | |
| 48 | #Search directory dirname for files (-type f) that match (-name) "*_v${version}*"
| |
| 49 | for f in `find $dirname -type f -name "*_v${version}*"`; do
| |
| 50 | #dirpathname and cleanname are used for file naming the tgz file | |
| 51 | dirpathname=`echo $dirname | rev | cut -d / -f1,2 | rev | sed 's/\//-/'` | |
| 52 | echo "*** dirpathname: $dirpathname" | |
| 53 | cleanname=`echo $f | cut -d / -f 2 | cut -d _ -f 1` | |
| 54 | echo "*** cleanname: $cleanname" | |
| 55 | echo "*** Checking if files/folders exist" | |
| 56 | ls -ld $f | |
| 57 | backupfilepath="$DST_DIR/Proc_Sat-1-${DT}-S515-V${version}-OUNCL-P${dirpathname}-${cleanname}.tar.gz"
| |
| 58 | echo "*** backup file path: $backupfilepath" | |
| 59 | # If backup file exists, append new files | |
| 60 | if [ -e $backupfilepath ]; then | |
| 61 | # u = update, z = gzip, v=verbose, f=filename | |
| 62 | # Note: tar --update does not allow to update gzipped files, | |
| 63 | # we unzip and rezip them. | |
| 64 | echo "*** backup file exists" | |
| 65 | # backupfiletar is .tar file path (stripped .gz from backupfilepath) | |
| 66 | backupfiletar="`echo -n $backupfilepath | head --bytes -3`" | |
| 67 | echo "*** backupfiletar: $backupfiletar" | |
| 68 | echo "*** decompressing" | |
| 69 | gunzip -vfd $backupfilepath | |
| 70 | if [ ! -f ${backupfiletar} ]; then
| |
| 71 | echo "*** Error during backup" | |
| 72 | exit 1 | |
| 73 | fi | |
| 74 | echo "*** updating" | |
| 75 | tar uvpf ${backupfiletar} $f
| |
| 76 | if [ $? -ne 0 ]; then | |
| 77 | echo "*** Error during backup" | |
| 78 | exit 1 | |
| 79 | fi | |
| 80 | echo "*** recompressing" | |
| 81 | gzip -vf9 ${backupfiletar}
| |
| 82 | if [ ! -f ${backupfilepath} ]; then
| |
| 83 | echo "*** Error during backup" | |
| 84 | exit 1 | |
| 85 | fi | |
| 86 | else | |
| 87 | echo "*** backup file doesn't exist, creating a new backup file" | |
| 88 | # c = create | |
| 89 | tar czvpf $backupfilepath $f | |
| 90 | if [ $? -ne 0 ]; then | |
| 91 | echo "*** Error during backup" | |
| 92 | exit 1 | |
| 93 | fi | |
| 94 | fi | |
| 95 | echo "*** Checking if backup file was created" | |
| 96 | ls -ld $backupfilepath | |
| 97 | echo -e "*** Done\n" | |
| 98 | done | |
| 99 | done |