-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfiles.sh
More file actions
executable file
·211 lines (188 loc) · 5.25 KB
/
files.sh
File metadata and controls
executable file
·211 lines (188 loc) · 5.25 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
204
205
206
207
208
#!/bin/bash
export PATH=$PATH:$(dirname $0)/aux
source config.sh
source log.sh
usage(){
cat << EOU
Usage: $0 [-d] [-c conf_file] [-h|--help|help] command
Script to handle the backup and restoration of Buscopista's site files.
Options:
help|-h|--help
Show this help.
-d
Enable debug mode
-c conf_file
Use the configuration file conf_file instead of the default
$conf_file
Commands:
backup [user|sys|all]
Do a backup of the Buscopista's user, system or all files (will
use all by default)
restore [-l|file]
Restore the last backup (the -l option) or the given backup tgz
file.
update [-r revision]
Update the files with the ones in the svn to the HEAD revision or
the one specified with -r.
EOU
exit ${1:-0}
}
do_backup() {
last_dir="$backups_dir/files/last"
[[ -d $files_dir ]] \
|| {
log::WARN "No source dir $files_dir found."
return 1
}
[[ -d $last_dir ]] \
|| {
mkdir -p $last_dir \
&& log::WARN "Last backup directory $last_dir not found, created a new one." \
|| {
log::ERROR "Failed to create the backups dir $last_dir."
return 1
}
}
log "Rsyncing from original directory $files_dir"
rsync --progress --stats -r -t -p -l --delete $files_dir $last_dir \
&& log::OK "Rsync Done." \
|| {
log::ERROR "Failed to rsync $files_dir to $last_dir. Aborting."
return 1
}
name="$(basename $files_dir)"
timestamp="$(date +%Y%m%d%H%M%S)"
log "Creating tarfile $backups_dir/files/$name.$timestamp.tgz"
tar cvzf $backups_dir/files/$name.$timestamp.tgz -C $last_dir/ $name \
&& log::OK "Tar done." \
|| {
log::ERROR "Error creating tarfile $backups_dir/files/$name.$timestamp.tgz."
return 1
}
return 0
}
restore()
{
dir="${1:?}"
name="$(basename $files_dir)"
[[ -d "$dir" ]] \
|| {
log::WARN "No backup found at $dir. Nothing to restore."
exit 1
}
log "Rsyncing $dir to $files_dir"
rsync --progress --stats -r -t -p -l --delete $dir/$name/ $files_dir \
&& log::OK "Rsync Done." \
|| {
log::ERROR "Failed to rsync $files_dir to $dir. Aborting."
return 1
}
file="$(ls -t $backups_dir/files/$name.*.tgz 2>/dev/null | head -n1)"
log "Restoring old last dir from the newer tgz $file"
[[ -f $file ]] \
|| {
log::WARN "No tar backup found, skipping the restoration of the last directory."
return 0
}
rm -Rf $dir/$name \
&& {
tar xvzf $file -C $dir \
&& log::OK "Last dir restored from $file." \
|| {
log::ERROR "Error toring tarfile $file."
return 1
}
} || log::WARN "Unable to delete old last dir. Skipping restoration from tgz."
}
do_restore() {
[[ "$1" == "-l" ]] \
|| [[ "$dir" == "" ]] \
&& dir="$backups_dir/files/last" \
|| dir="$1"
restore "$dir" \
&& return 0 \
|| return 1
}
do_update() {
dir="${1:-${src_files_dir}}"
## if a svn url was specified, handle it
[[ "${dir#svn:}" != "$dir" ]] \
&& {
url="${dir#svn:}"
## download the changes to the temporary source directory
[[ -d $tmp_src_dir/.svn ]] \
&& {
cd $tmp_src_dir
svn up ${svn_user:+--username $svn_user} \
${svn_pass:+--password $svn_pass} \
--non-interactive .
cd - &>/dev/null
} || {
svn co ${svn_user:+--username $svn_user} \
${svn_pass:+--password $svn_pass} \
--non-interactive $url $tmp_src_dir \
|| {
log::ERROR "Error getting source code from $url to $tmp_src_dir."
return 1
}
}
dir=$tmp_src_dir
}
[[ -d "$dir" ]] \
|| {
log::WARN "No files found at $dir. Nothing to update."
exit 1
}
log "Rsyncing $dir to $files_dir"
rsync --progress --stats -r -t -p -l --exclude '.svn' --delete $dir/ $files_dir \
&& log::OK "Rsync Done." \
|| {
log::ERROR "Failed to rsync $files_dir to $dir. Aborting."
return 1
}
}
while getopts 'hdc:' option; do
case $option in
h) usage 0;;
d) set -x;;
c) conf_file="$OPTARG";;
*) usage 1;;
esac
done
shift $(($OPTIND - 1))
OPTIND=1
conf::load ${conf_file:?No conf_file specified} \
|| { log::ERROR "Error parsing config file"; exit 1; }
conf::require files_dir src_files_dir backups_dir tmp_src_dir \
|| {
log::ERROR "Some required parameters are missing in the config file."
exit 1
}
log "
Loaded config:
Dirs:
files_dir: ${files_dir}
src_files_dir: ${src_files_dir}
tmp_src_dir: ${tmp_src_dir}
ugc_dirs: ${ugc_dirs:-None}
Backups:
backups_dir: ${backups_dir}
SVN:
svn_repo: ${svn_repo:-None}
svn_user: ${svn_user:-None}
svn_pass: ${svn_pass:-None}
"
case ${1} in
backup)
do_backup "${@:2}"
;;
restore)
do_restore "${@:2}"
;;
update)
do_update "${@:2}"
;;
*)
usage 1
;;
esac