-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmoodlefilesbackup.sh
executable file
·195 lines (161 loc) · 5.6 KB
/
moodlefilesbackup.sh
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
#!/bin/bash
######
# moodlefilesbackup.sh
# @author: Matt Gleeson <[email protected]>
# @version: 1.2.1
# @license: GPL2
######
versionno="version: 1.2.1"
# set -u
############
## Usage
usage="\
Usage: moodlefilesbackup [-h] [--help] [-m][--moodlepath=PATH] [-b][--backuppath=PATH]
[-e][--email=EMAILADDRESS] [--version]"
##########################################################################
## ERR HANDLING & GENERAL PURPOSE GOODNESS
_date=$(date +%Y-%m-%d)
##### for when stuff go wrong and we have nowhere else to go
##### ...for the night is dark and full of errors
err_exit ()
{
echo ""
echo "$@" 1>&2
echo -e "[\033[01;31m error \033[00m] at line: ${LINENO}"
exit 1
}
#### where are we?
DIR="${BASH_SOURCE%/*}"
if [[ ! -d "${DIR}" ]]; then DIR="${PWD}"; fi
## TODO: do check/download function for other dependencies also
#### include for error checker function and output colouring
load_checkerr ()
{
## is checkerr here?
if [ -e ${DIR}/checkerr.inc.sh ]; then
echo -e "[\033[01;32m OK \033[00m] checkerr found"
. "${DIR}/checkerr.inc.sh"
else
echo -e "[\033[01;31m WARNING \033[00m] checkerr not found"
echo -e "${info} don't worry, I get it for you... getting..."
wget -O checkerr.inc.sh https://gist.githubusercontent.com/mgleeson/80876fd2a7779d9f96ca4a11996681f0/raw/checkerr.inc.sh
. "${DIR}/checkerr.inc.sh"
if [[ "${checkerr_loaded}" == "true" ]]
then
echo -e "${ok} checkerr downloaded and loaded"
else
echo -e "[\033[01;31m ERROR \033[00m] dependency not downloaded and not loaded: checkerr.inc.sh"
err_exit
fi
fi
}
load_checkerr
## TODO: make checker universal and pass script name via argument
## END ERR HANDLING
##########################################################################
#### Check for previous moodle config vars file
if [ -e ~/mdl_conf.sh ]; then
rm -f ~/mdl_conf.sh || err_exit
echo "existing mdl_conf removed [OK]"
fi
################################
## PARAMETERS/ARGUMENTS PARSER
for PARAMS in "$@"
do
case $PARAMS in
-m=*|--moodlepath=*)
MOODLEPATH="${PARAMS#*=}"
MOODLEPATH=${MOODLEPATH%/}
shift
;;
-b=*|--backuppath=*)
BACKUPPATH="${PARAMS#*=}"
BACKUPPATH=${BACKUPPATH%/}
shift
;;
-d|--debug)
DEBUG="TRUE"
shift
;;
--version|-v*)
echo ${versionno}; shift ;;
-- ) # Stop option processing
shift; break ;;
- ) # Use stdin as input.
break ;;
-* )
echo "${usage}" 1>&2; exit 1 ;;
* )
break ;;
esac
done
if [ ! -n "${MOODLEPATH}" ]; then
echo "no moodle path specified?"
err_exit;
fi
if [ ! -n "${BACKUPPATH}" ]; then
echo "no backup path specified?"
err_exit;
fi
##### END PARAMETERS/ARGUMENTS PARSER
#############
## Get the config details from the Moodle config.php file and put in temp file
grep -P -o '(?<=^\$CFG->)(\w*)\s*=\s?(?:\x27)(.*)(?:\x27)(?=\;)' ${MOODLEPATH}/config.php | sed 's/\s//g' | sed 's/\x27/"/g' > ~/mdl_conf.sh || err_exit
. ~/mdl_conf.sh
#############
## vars
_sitename=`echo "SELECT shortname FROM ${prefix}course WHERE sortorder = 1" | mysql -h ${dbhost} ${dbname} -u ${dbuser} -p${dbpass} -s -N | tr '/' '-' | tr ' ' '_'`
_now=$(date +%Y-%m-%d--%H%M%S)
_files_zipfile="${BACKUPPATH}/${_sitename}_files_backup_${_now}.tar.gz"
_data_zipfile="${BACKUPPATH}/${_sitename}_moodledata_backup_${_now}.tar.gz"
## moodles such as bitnami stack moodles specify wwwroot programatically so this fails on them
# sitename="`echo ${wwwroot} | sed -e 's/^http:\/\///g' -e 's/^https:\/\///g' | tr '/' '-'`"
## end vars
#############
if [[ ${DEBUG} = "TRUE" ]] ; then
echo || echo
echo "DEBUG = ${DEBUG}"
echo && echo
echo "MOODLE PATH: ${MOODLEPATH}"
echo "BACKUP PATH: ${BACKUPPATH}"
echo "Site Name: ${_sitename}"
echo "Timestamp: ${_now}"
echo "Moodle files archive file: ${_files_zipfile}"
echo "Moodle data archive file: ${_data_zipfile}"
echo || echo
echo "mdl_conf dump:"
cat ~/mdl_conf.sh
echo || echo
echo -e "${warn} Note: backup has not occurred, omit debug parameter to execute backup"
echo || echo
else
###################
# start the backup
# backup the moodle files
echo -e "${info} Starting backup of ${MOODLEPATH} directory of site ${_sitename} timestamped at datetime $_now"
tar -vczf ${_files_zipfile} ${MOODLEPATH}
checkerr "$_" "$?"
echo || echo
# backup moodledata
echo -e "${info} Starting backup of ${dataroot} Moodle data directory of site ${_sitename} timestamped at datetime $_now"
tar -vczf ${_data_zipfile} ${dataroot}
checkerr "$_" "$?"
echo || echo
# end of backup
echo -e "${info} Ending backup of site ${_sitename} timestamped at datetime $_now"
echo -e "_________________________________________________"
fi
### Get rid of mdl_conf when finished
if [ -e ~/mdl_conf.sh ]; then
rm ~/mdl_conf.sh
if [ $? -eq 0 ]; then # if OK
echo "mdl_conf removed [OK]"
else
err_exit
fi
fi
## Clean up old backups
# echo
# echo deleting all .tar.gz files in ${BACKUPPATH} older than 5 days
# find ${BACKUPPATH} -type f -mtime +5 -exec rm *.tar.gz {} \;
# echo done.