Wednesday, March 29, 2017

Bash: Script - part 2: Archiving in Dynamic Directory

#!/bin/bash
#
# used in arcive directory
YEAR=$(date +%Y)
MONTH=$(date +%m)
DAY=$(date +%d)
# used in arcive file name
TIME=$(date +%H%M%S)
# read files to be backed up
CONFIG_FILE=/homeb/d54712/sandbox/scripts/data.d/Files_To_Backup
# Base destination path (augmented with archive directory
BASE_DEST=/homeb/d54712/sandbox/scripts/data.d
 # Archive File name
AR_FILE=$TIME.tar.gz
# make archive directory
mkdir -p $BASE_DEST/$YEAR/$MONTH/$DAY
# Backup Destination
DESTINATION=$BASE_DEST/$YEAR/$MONTH/$DAY/$AR_FILE
# Redirect STDIN to $CONFIG_FILE
exec < $CONFIG_FILE
# Read each line in from STDIN ($CONFIG_FILE)
while read FILE_NAME
do
        # Store each line in variable
        FILE_LIST="$FILE_LIST $FILE_NAME"
done

echo "FILE_LIST"
echo "$FILE_LIST"

echo "Year: $YEAR"
echo "Month: $MONTH"
echo "Day: $DAY"
echo "Time: $TIME"
echo "Configuration File: $CONFIG_FILE"
echo "Base Destination: $BASE_DEST"
echo "AR_FILE: $AR_FILE"
echo "Archive File: $DESTINATION"



Result:
#debug
$ ./read3.sh
FILE_LIST
 /auto/home/homeb/d54712/sandbox/scripts/data.d/file1 /auto/home/homeb/d54712/sandbox/scripts/data.d/file2 /auto/home/homeb/d54712/sandbox/scripts/data.d/file3 /auto/home/homeb/d54712/sandbox/scripts/data.d/file4 /auto/home/homeb/d54712/sandbox/scripts/data.d/file5
Year: 2017
Month: 03
Day: 29
Time: 110444
Configuration File: /homeb/d54712/sandbox/scripts/data.d/Files_To_Backup
Base Destination: /homeb/d54712/sandbox/scripts/data.d
AR_FILE: 110444.tar.gz
Archive File: /homeb/d54712/sandbox/scripts/data.d/2017/03/29/110444.tar.gz

No comments:

Post a Comment