forked from esimonetti/SugarDockerized
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathrestorefromfile.sh
executable file
·125 lines (108 loc) · 4.49 KB
/
restorefromfile.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
#!/bin/bash
# Rafael Fernandes
# sugarcrm.com
if [ -z $1 ]
then
echo Provide the backup suffix as script parameters
else
# check if the stack is running
running=`docker ps | grep sugar-mysql | wc -l`
if [ $running -gt 0 ]
then
# running
now="$(date)"
echo "Starting restore at $now"
# enter the repo's root directory
REPO="$( dirname ${BASH_SOURCE[0]} )/../"
cd $REPO
# locate the file
if [ ! -f $1 ]
then
echo $1 does not exist, please provide the tar.gz file path containing the backup to restore
exit 1
fi
# if it is our repo, and the source exists, and the destination does not
if [ -f '.gitignore' ] && [ -d 'data' ]
then
echo "Cleaning up previous install (./data/app/sugar) if any, please wait..."
if [ -d 'data/app/sugar' ]
then
rm -rf data/app/sugar
fi
TEMP_FOLDER=./data/app/tmp
if [ -d $TEMP_FOLDER ]
then
rm -rf $TEMP_FOLDER
fi
mkdir -p "$TEMP_FOLDER/sql"
echo Decompressing $1, please wait...
tar -xf $1 -C $TEMP_FOLDER --strip-components=1
for f in $TEMP_FOLDER/*.sql; do
mv "$f" "$TEMP_FOLDER/sql/"
done
for f in $TEMP_FOLDER/sugar*; do
mv "$f" $TEMP_FOLDER/sugar
done
if [ ! -d $TEMP_FOLDER/sugar ]
then
echo \"$TEMP_FOLDER/sugar\" cannot be empty
exit 1
fi
if [ -d $TEMP_FOLDER/sql ]
then
find $TEMP_FOLDER/sql -type f -name 'sugar*' ! -name '*triggers*' -exec sh -c 'sql=${1:-:}; x="${2:-:}"; mv "$x" "$sql/sugar.sql"' bash "$TEMP_FOLDER/sql" {} +\;
find $TEMP_FOLDER/sql -type f -name '*triggers*' -exec sh -c 'sql=${1:-:}; x="{}"; mv "${2:-:}" "$sql/sugar_triggers.sql"' bash "$TEMP_FOLDER/sql" {} +\;
fi
echo Restoring application files
SUGAR_TMP_DIR=`ls -d $TEMP_FOLDER/sugar*`
mv $SUGAR_TMP_DIR ./data/app/sugar
echo Application files restored
echo Restoring database
docker exec -it sugar-mysql mysqladmin -h localhost -f -u root -proot drop sugar | grep -v "mysqladmin: \[Warning\]"
docker exec -it sugar-mysql mysqladmin -h localhost -u root -proot create sugar | grep -v "mysqladmin: \[Warning\]"
if [ -f $TEMP_FOLDER/sql/sugar.sql.tgz ]
then
if hash tar 2>/dev/null; then
tar -zxf $TEMP_FOLDER/sql/sugar.sql.tgz
echo Database uncompressed to $TEMP_FOLDER/sql/sugar.sql
fi
fi
if [ -f $TEMP_FOLDER/sql/sugar.sql ]
then
cat $TEMP_FOLDER/sql/sugar.sql | docker exec -i sugar-mysql mysql -h localhost -u root -proot sugar
if [ -f $TEMP_FOLDER/sql/sugar_triggers.sql ]
then
cat $TEMP_FOLDER/sql/sugar_triggers.sql | docker exec -i sugar-mysql mysql -h localhost -u root -proot sugar
fi
echo Database restored
else
echo Database not found! The selected restore is corrupted
exit 1
fi
echo "Dockerizing Backup config files..."
if [ ! -f ./data/app/sugar/.htaccess ]; then
cp ./utilities/configs/.htaccess ./data/app/sugar/.htaccess
fi
sed -i.bak 's@RewriteBase /@RewriteBase /sugar@g' ./data/app/sugar/.htaccess
cat ./utilities/configs/config_override_dockerized.php >> ./data/app/sugar/config_override.php
echo "Cleaning up please wait..."
if [ -d $TEMP_FOLDER ]
then
rm -rf $TEMP_FOLDER
fi
# refresh all transient storages
./utilities/build/refreshsystem.sh
echo Repairing system
cp ./utilities/build/simpleRepair.php ./data/app/sugar
./utilities/runcli.sh "php simpleRepair.php"
echo System repaired
echo Performing Elasticsearch re-index
./utilities/runcli.sh "./bin/sugarcrm search:silent_reindex --clearData"
echo Restore completed!
fi
now="$(date)"
echo "Restore finished at $now"
else
echo The stack is not running, please start the stack first
fi
fi