Thursday, March 16, 2017
Bash Scripting: Dynamically created Destination Directory with Archiving Script
1 #!/bin/bash
2 #
3 # Back up certain files in t3psolarwebl2:/prd_webroot/docs/i-net_dev
4 #
5 # Gather Current Day, Month & Time
6 #
7 DAY=$( date +%d )
8 MONTH=$( date +%m )
9 TIME=$( date +%H%M )
10 #
11 # Set up Configuration File and Destination/Source
12 #
13 CONFIG_FILE=/auto/home/homeb/d54712/sandbox/scripts/data.d/backup_i-net_dev
14 FILE=select_i-net_dev_archive.tar.
15 BASEDEST=/auto/home/homeb/d54712/sandbox/scripts/data.d/archive/hourly
16 #
17 # User Variables
18 FILE_NO=1 # Start reading file from line 1
19 #
20 ###################################################################
21 # Read Configuration File and cache it
22 #
23 exec < $CONFIG_FILE # STDIN is Config File
24
25 while read FILE_NAME
26 do
27 if [ -f $FILE_NAME -o -d $FILE_NAME ]
28 then
29 echo
30 FILE_LIST="$FILE_LIST $FILE_NAME" # Cache input from Config File
31 else
32 echo "File: $FILE_NAME, does not exist"
33 echo "$FILE_NAME is located in configuration file: $CONFIG_FILE on line: $FILE_NO"
34 fi
35
36 FILE_NO=$[ $FILE_NO + 1 ] # Advance number line count
37 done
38
39 # echo $FILE_LIST
40 #
41 # Create Archive Destination Directory
42 #
43 mkdir -p $BASEDEST/$MONTH/$DAY
44 #
45 # Build Archive Destination File Name
46 #
47 DESTINATION=$BASEDEST/$MONTH/$DAY/archive.$TIME.tar.gz
48 #
49 ##################################################################
50 # Backup Directories/Files in Configuration File
51 #
52 #
53
54 tar -cvzf $DESTINATION $FILE_LIST 2> /dev/null
55
56 echo
57 echo
58
59 for (( x=1; x<=10; x++ ))
60 do
61 echo -n "$x..."
62 sleep .25
63 done
64 echo
65 echo
66 echo "Backup complete: $DESTINATION"
67 echo
68 exit
Result:
ls -lR archive
archive:
total 4
drwxr-xr-x 3 d54712 kmuser 4096 Mar 16 14:18 hourly
archive/hourly:
total 4
drwxr-xr-x 3 d54712 kmuser 4096 Mar 16 14:18 03
archive/hourly/03:
total 4
drwxr-xr-x 2 d54712 kmuser 4096 Mar 16 14:30 16
archive/hourly/03/16:
total 11264
-rw-r--r-- 1 d54712 kmuser 5735953 Mar 16 14:23 archive.1423.tar.gz
-rw-r--r-- 1 d54712 kmuser 5735953 Mar 16 14:30 archive.1430.tar.gz
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment