-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackup.sh
More file actions
executable file
·203 lines (180 loc) · 5.1 KB
/
backup.sh
File metadata and controls
executable file
·203 lines (180 loc) · 5.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#!/bin/bash
#
# Usage: backup.sh <CONFIG_FILE>
#
# Example:
# time ./backup.sh testconfig.cfg
#
# verbose output control flag
VERBOSE=
# to enable verbose uncomment the line below
#VERBOSE=true
# verbose output can be enabled in backup config file passed on as a comman line argument.
#
# CONFIG_FILE should set the following variables.
# BACKUP_LOG_DIR
# LOG
# PERF_LOG
# LOG_CLEAN_SED
# BACKUP_TO_DIR
# BACKUP_LIST_FILE
#
# All log files will be appended to prevent loss of output. If you need a new log file, clear or set a new log file name outside this script.
#
SCRIPT_NAME=${0##*/}
if [ "x$1" != "x" ] ; then
CONFIG_FILE=$1
else
echo "Config file not specified!"
exit 1
fi
#
# weekNo is a util function to setup the ${week} variable to use in the config
# file to provide the option to backup to different directories by the week
# number of the month.
#
function weekNo {
day=`date +%_d`
month=`date +%m`
year=`date +%Y`
week=`cal $month $year | sed -n "3,$ p" | sed -n "/$day/{=;q;}"`
return $week
}
weekNo
#validate CONFIG_FILE exists
if [[ -f ${CONFIG_FILE} ]]; then
. ${CONFIG_FILE}
else
echo "Config file not found! [${CONFIG_FILE}]"
exit 2
fi
CONFIG_FILE_NAME=${CONFIG_FILE##*/}
function backup {
DATE=`date`
echo "${DATE} - backup call with [$1] [${BACKUP_TO_DIR}]"
## rsync --recursive --verbose --progress --perms --times "$1" ${BACKUP_TO_DIR}
## rsync --archive --delete --verbose --progress "$1" ${BACKUP_TO_DIR}
if [ -z ${EXCLUDE_FILE} ] ; then
rsync --archive --delete --verbose "$1" ${BACKUP_TO_DIR}
else
rsync --archive --delete --verbose --exclude-from=${EXCLUDE_FILE} "$1" ${BACKUP_TO_DIR}
fi
}
function validateBackupDir {
if [[ ${BACKUP_TO_DIR} =~ .*@.*:.* ]]; then
echo "Backup dir is remote. take it on faith and proceed. [${BACKUP_TO_DIR}]"
#It looks like the rsync settings used creates the parent
#directory structure. So the attempt below is not needed.
# IFS=':' read -r -a array <<< "$BACKUP_TO_DIR"
# USER_HOST=${array[0]}
# DEST_DIR=${array[1]}
# echo "USER_HOST=[${USER_HOST}]"
# echo "DEST_DIR=[${DEST_DIR}]"
# if ssh ${USER_HOST} '[ -d ${DEST_DIR} ]'; then
# echo "Remote dir found. proceed. [${BACKUP_TO_DIR}]"
# else
# echo "Remote dir found not found! [${BACKUP_TO_DIR}]"
# if ssh ${USER_HOST} 'mkdir -p ${DEST_DIR}'; then
# echo "Remote dir created! [${BACKUP_TO_DIR}]"
# else
# echo "Remote could not be created! [${BACKUP_TO_DIR}]"
# fi
# fi
else
if [[ -d $BACKUP_TO_DIR ]]; then
echo "Backup dir found. proceed. [${BACKUP_TO_DIR}]"
else
echo "Backup dir not found! [${BACKUP_TO_DIR}]"
exit 4
fi
fi
}
LOCK_FILE=${BACKUP_LOG_DIR}/${SCRIPT_NAME}_${CONFIG_FILE_NAME}.lock
function validateThereCanBeOnlyOne {
echo "LOCK_FILE=[${LOCK_FILE}]"
if [[ -e $LOCK_FILE ]]; then
echo "Lock file exists! [${LOCK_FILE}]"
exit 5
else
echo "Create lock file."
{
echo "PID=[$$]"
echo "LOG=[${LOG}]"
echo "DATE=[`date`]"
} > ${LOCK_FILE}
fi
}
function cleanUpThereCanBeOnlyOne {
echo "LOCK_FILE=[${LOCK_FILE}]"
if [[ -e $LOCK_FILE ]]; then
echo "Remove lock file."
rm ${LOCK_FILE}
else
echo "Lock file does not exist!"
fi
}
function validateLogDir {
if [[ -d $BACKUP_LOG_DIR ]]; then
if [ $VERBOSE ] ; then
echo "Log dir exists. proceed. [${BACKUP_LOG_DIR}]"
fi
else
echo "Log dir not found! [${BACKUP_LOG_DIR}]"
echo "Create log dir."
mkdir -p ${BACKUP_LOG_DIR}
fi
}
# cleanEmptyLogFile will use the ${LOG_CLEAN_SED} file to compare the
# current log file to a base line using sed to zero out match the log
# file. the function is almost done. Example file does match the log
# file. The full match on the file is not deleted yet.
function cleanEmptyLogFile {
if [ $VERBOSE ] ; then
echo "LOG_CLEAN_SED=[${LOG_CLEAN_SED}]"
echo "LOG=[${LOG}]"
fi
sed -f ${LOG_CLEAN_SED} ${LOG}
}
validateLogDir
if [ $VERBOSE ] ; then
echo "after validateLogDir - before logging"
fi
echo "LOG=[${LOG}]"
{
if [ $VERBOSE ] ; then
echo "inside logging"
fi
START=`date`
echo "${START} - start"
echo "BACKUP_TO_DIR=[${BACKUP_TO_DIR}]"
echo "PERF_LOG=[${PERF_LOG}]"
echo "LOG=[${LOG}]"
validateThereCanBeOnlyOne
validateBackupDir
if [[ -f ${BACKUP_LIST_FILE} ]]; then
. ${BACKUP_LIST_FILE}
else
echo "Backup list file not found! [${BACKUP_LIST_FILE}]"
exit 2
fi
END=`date`
echo "${START} - start"
echo "${END} - end"
#do not calculate free space on remote
if [[ ${BACKUP_TO_DIR} =~ .*@.*:.* ]]; then
echo "Backup dir is remote. do not calculate free space on remote. [${BACKUP_TO_DIR}]"
else
FREE_SPACE=`df -k ${BACKUP_TO_DIR} | tail -1 | sed "s/\s\+/,/g"`
fi
#the du takes too long.
#USED_SPACE=`du -sk -I .Trashes ${BACKUP_TO_DIR} | sed "s/\s\+/,/g"`
USED_SPACE="not done"
cleanUpThereCanBeOnlyOne
} >> ${LOG} 2>&1
## do not need ${BACKUP_TO_DIR}. ${FREE_SPACE} has that info too.
echo "${START},${END},${USED_SPACE},${FREE_SPACE}" >> ${PERF_LOG}
if [ $VERBOSE ] ; then
echo "#### Dump logfile: ${LOG}"
cat ${LOG}
fi
#cleanEmptyLogFile