-
Notifications
You must be signed in to change notification settings - Fork 0
/
backup_cms.sh
executable file
·166 lines (130 loc) · 4.94 KB
/
backup_cms.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
set -e
action=$1
email=$2
password=$3
schema_host=$4
target_dir=$5
# count arguments and print usage if not enough
if [ $# -lt 5 ]; then
echo "Usage: $0 action email password schema_host target_dir"
echo "Example: $0 backup [email protected] test1234 http://localhost:8000 ./export_tmp"
exit 1
fi
# check if jq is installed
if ! [ -x "$(command -v jq)" ]; then
echo 'Error: jq is not installed.' >&2
exit 1
fi
# check if curl is installed
if ! [ -x "$(command -v curl)" ]; then
echo 'Error: curl is not installed.' >&2
exit 1
fi
# check if action is backup or restore
if [ "$action" != "backup" ] && [ "$action" != "restore" ]; then
echo "Error: action must be backup or restore." >&2
exit 1
fi
# check if email is provided
if [ -z "$email" ]; then
echo "Error: email is not provided." >&2
exit 1
fi
# check if password is provided
if [ -z "$password" ]; then
echo "Error: password is not provided." >&2
exit 1
fi
# check if schema_host is provided
if [ -z "$schema_host" ]; then
echo "Error: schema_host is not provided." >&2
exit 1
fi
# check if target_dir is provided
if [ -z "$target_dir" ]; then
echo "Error: target_dir is not provided." >&2
exit 1
fi
# check if target_dir exists
if [ ! -d "$target_dir" ]; then
echo "Error: target_dir does not exist." >&2
exit 1
fi
# check if target_dir is writable
if [ ! -w "$target_dir" ]; then
echo "Error: target_dir is not writable." >&2
exit 1
fi
# login and get access token
login_result=$(curl -X POST -d "{\"email\": \"${email}\", \"password\": \"$password\"}" -H 'Content-Type: application/json' "${schema_host}/auth/login/");
token=$(echo "$login_result" | jq -r '.data.access_token')
# check if token is empty
if [ -z "$token" ]; then
echo "Error: token is empty." >&2
exit 1
fi
# check if action is backup
if [ "$action" = "backup" ]; then
echo "Exporting schemas..."
# get all schemas
curl -X GET -H "Authorization: Bearer ${token}" "${schema_host}/schema/snapshot?export=json" > "${target_dir}/schemas.json"
echo "Exporting collections..."
echo "Exporting page translations..."
curl -X GET -H "Authorization: Bearer ${token}" "${schema_host}/items/Page_translations?fields=Page_id,content,id,languages_code" | jq ".data" > "${target_dir}/Page_translations.json"
echo "Exporting pages..."
curl -X GET -H "Authorization: Bearer ${token}" "${schema_host}/items/Page?fields=id,status,slug,project" | jq ".data" > "${target_dir}/Page.json"
echo "Exporting languages..."
curl -X GET -H "Authorization: Bearer ${token}" "${schema_host}/items/languages?fields=code,name,direction" | jq ".data" > "${target_dir}/languages.json"
echo "Exporting collections..."
# loop over all collections
while read collection; do
echo "Exporting ${collection}..."
curl -X GET -H "Authorization: Bearer ${token}" "${schema_host}/items/${collection}" | jq ".data" > "${target_dir}/${collection}.json"
done <<< "$collections"
echo "Exporting files..."
# get all files
file_result=$(curl -X GET -H "Authorization: Bearer ${token}" "${schema_host}/files/")
# extract file ids and names
file_ids=$(echo "$file_result" | jq -r ".data[] | .id + \"_splitme_\" + .filename_download" | grep -v "^$")
mkdir -p "${target_dir}/files"
# loop over all files
while read file; do
# get file id and name
file_id=$(echo "$file" | sed -r 's/(.+)_splitme_.*/\\1/')
# download file
curl -X GET -H "Authorization: Bearer ${token}" "${schema_host}/assets/${file_id}" > "${target_dir}/files/${file}"
done <<< "$file_ids"
echo "Done."
fi
# check if action is restore
if [ "$action" = "restore" ]; then
echo "Importing schemas..."
# get schema difference
diff=$(curl -X POST -H "Authorization: Bearer ${token}" -H 'Content-Type: application/json' -d @${target_dir}/schemas.json "${schema_host}/schema/diff")
# check if diff is empty or if update is needed
if [ -z "$diff" ] || [ "$diff" = "[]" ]; then
echo "No schema update needed."
else
echo "Updating schemas..."
curl -X POST -H "Authorization: Bearer ${token}" -H 'Content-Type: application/json' -d "$(echo "$diff"|jq ".data")" "${schema_host}/schema/apply"
fi
# import all collections
collection_names=("languages" "Page" "Page_translations")
echo "Importing collections..."
# loop over collections_names
for collection in "${collection_names[@]}"; do
echo "Importing ${collection}..."
echo "------------------"
curl -X POST -H "Authorization: Bearer ${token}" -H 'Content-Type: application/json' -d @${target_dir}/${collection}.json "${schema_host}/items/${collection}"
done
echo "Importing files..."
# get all files
file_id_names=$(ls -1 ${target_dir}/files)
# loop over all files
while read file_id_name; do
file_id=$(echo "$file_id_name" | sed -r 's/(.+)_splitme_.*/\1/')
file_name=$(echo "$file_id_name" | sed -r 's/.+_splitme_(.+)/\1/')
echo "Importing file ${file_name} with id ${file_id}..."
curl -X POST -H "Authorization: Bearer ${token}" -F "title=${file_name}" -F "id=${file_id}" -F "file=@${target_dir}/files/${file_id_name}" "${schema_host}/files"
done <<< "$file_id_names"
fi