aboutsummaryrefslogtreecommitdiff
path: root/scripts/restore
blob: a0498f09a5d105a853f295c8d05c368cc6220b74 (plain)
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
#!/bin/bash
# This restore script is adapted to Yunohost >=2.4

# Exit on command errors and treat unset variables as an error
set -eu

if [ ! -e .fonctions ]; then
	# Get file fonction if not been to the current directory
	sudo cp ../settings/scripts/.fonctions ./.fonctions
	sudo chmod a+rx .fonctions
fi
# Loads the generic functions usually used in the script
source .fonctions
# Source app helpers
source /usr/share/yunohost/helpers

# The parameter $app is the id of the app instance ex: ynhexample__2
app=$YNH_APP_INSTANCE_NAME

# Get old parameter of the app
domain=$(ynh_app_setting_get $app domain)
path=$(ynh_app_setting_get $app path)
is_public=$(ynh_app_setting_get $app is_public)

# Check domain/path availability
sudo yunohost app checkurl "${domain}${path}" -a "$app" \
    || ynh_die "Path not available: ${domain}${path}"

# Check $final_path
final_path="/opt/${app}"
if [ -d $final_path ]; then
	ynh_die "There is already a directory: $final_path"
fi

# Check configuration files nginx
nginx_conf="/etc/nginx/conf.d/${domain}.d/${app}.conf"
if [ -f $nginx_conf ]; then
	ynh_die "The NGINX configuration already exists at '${nginx_conf}'. 
	You should safely delete it before restoring this app."
fi
# Check configuration files php-fpm
crontab_conf="/etc/cron.d/${app}"
if [ -f $crontab_conf ]; then 
	ynh_die "The CRONTAB configuration already exists at '${crontab_conf}'. 
	You should safely delete it before restoring this app."
fi

# Restore services
web_systemd="/etc/systemd/system/${app}-web.service"
if [ -f "${web_systemd}" ]; then
	ynh_die "The MASTODON WEB configuration already exists at '${web_systemd}'. 
	You should safely delete it before restoring this app."
fi
sidekiq_systemd="/etc/systemd/system/${app}-sidekiq.service"
if [ -f "${sidekiq_systemd}" ]; then
	ynh_die "The MASTODON SIDEKIQ configuration already exists at '${sidekiq_systemd}'. 
	You should safely delete it before restoring this app."
fi
streaming_systemd="/etc/systemd/system/${app}-streaming.service"
if [ -f "${streaming_systemd}" ]; then
	ynh_die "The MASTODON STREAMING configuration already exists at '${streaming_systemd}'. 
	You should safely delete it before restoring this app."
fi

# Create user unix
sudo adduser $app --home /opt/$app --gecos "First Last,RoomNumber,WorkPhone,HomePhone" --disabled-password --disabled-login

 # Restore sources & data
sudo cp -a ./sources "$final_path"

# Set permissions
sudo chown -R $app: "$final_path"

# Set UTF8 encoding by default
sudo su -c "psql" postgres <<< \
        "update pg_database set datistemplate='false' where datname='template1';"
sudo su -c "psql" postgres <<< \
	      "drop database template1;"
sudo su -c "psql" postgres <<< \
	      "create database template1 encoding='UTF8' template template0;"
sudo su -c "psql" postgres <<< \
	      "update pg_database set datistemplate='true' where datname='template1';"

# Acl for Postgres user dump
sudo chmod o+r $YNH_APP_BACKUP_DIR/mastodon_db.sql || true

# Restore db
ynh_psql_create_db_without_password "$app"
sudo su - postgres <<COMMANDS
pg_dump --role=mastodon -U postgres --no-password mastodon < $YNH_APP_BACKUP_DIR/mastodon_db.sql
COMMANDS

# Create symlink for ruby
sudo ln -s /opt/mastodon/.rbenv/versions/2.4.1/bin/ruby /usr/bin/ruby || true

# Restore Mastodon
sudo su - $app <<RCOMMANDS
cd ~/live
RAILS_ENV=production bin/bundle exec rails db:migrate
RAILS_ENV=production bin/bundle exec rails assets:precompile
RCOMMANDS

# Restore nginx configuration files
sudo cp -a ./nginx.conf   "$nginx_conf"
# Restore crontab
sudo cp -a ./cron.conf "$crontab_conf"

sudo cp ../conf/mastodon-web.service /etc/systemd/system/mastodon-web.service
sudo chown root: /etc/systemd/system/mastodon-web.service
sudo cp ../conf/mastodon-sidekiq.service /etc/systemd/system/mastodon-sidekiq.service
sudo chown root: /etc/systemd/system/mastodon-sidekiq.service
sudo cp ../conf/mastodon-streaming.service /etc/systemd/system/mastodon-streaming.service
sudo chown root: /etc/systemd/system/mastodon-streaming.service

sudo systemctl daemon-reload
sudo systemctl enable /etc/systemd/system/mastodon-*.service
sudo systemctl start mastodon-web.service mastodon-sidekiq.service mastodon-streaming.service
# debug
sudo systemctl status mastodon-web.service mastodon-sidekiq.service mastodon-streaming.service

# Add service YunoHost
sudo yunohost service add mastodon-web
sudo yunohost service add mastodon-sidekiq
sudo yunohost service add mastodon-streaming

# Reload services
sudo systemctl reload nginx