home · contact · privacy
363a5f6edb4c859d7e81532ee317322b3a1fc270
[config] / buster / setup_scripts / restore_app.sh
1 #!/bin/sh
2 set -e
3 set -x
4
5 if [ "$#" -lt 2 ]; then
6     echo 'Need two arguments: service name and DB name.'
7     false
8 fi
9 if [ ! "$1" = "pleroma_otp" ] && [ ! "$1" = "pleroma_sourc" ] && [ ! "$1" = "peertube" ]; then
10     echo "Need legal service name (pleroma_otp or pleroma_source or peertube)."
11     false
12 fi
13 app="$1"
14 db_name="$2"
15
16 service "${app}" stop
17
18 mv "/home/plom/${app}_backup.tar" /tmp/
19 cd /tmp
20 tar xf "${app}_backup.tar"
21
22 su postgres -c "pg_restore -c -1 -d ${db_name} ${app}_backup/${db_name}.pgdump"
23 rm "${app}_backup/${db_name}.pgdump"
24
25 cd "${app}_backup"
26 for path in $(find . -type f); do
27     if [ "${app}" = "pleroma_source" ]; then
28         if [ "${path}" = '/opt/pleroma/config/prod.secret.exs' ]; then
29             continue  # skip file that contains passwords
30         fi
31     fi
32     target_path=$(echo "${path}" | cut -c2-)
33     source_path=$(realpath "${path}")
34     dir=$(dirname "${target_path}")
35     mkdir -p "${dir}"
36     cp -a "${source_path}" "${target_path}"
37 done
38
39 # TODO: Horrible hack, improve.
40 if [ "${app}" = "pleroma_otp" ]; then
41     db_pw=$(cat /etc/pleroma/config.exs | grep password | sed 's/[ ]*password\: *//g' | sed 's/,//g' | sed 's/"//g')
42 elif [ "${app}" = "peertube" ]; then
43     db_pw=$(cat /var/www/peertube/config/production.yaml | grep password | head -1 | sed "s/[ ]*password\: *//g" | sed "s/'//g")
44 fi
45 su postgres -lc "psql -c \"ALTER USER ${app} WITH PASSWORD '${db_pw}';\""
46
47 service "${app}" start