j
File size: 3,306 Bytes
79421ed
69f97f6
79421ed
69f97f6
79421ed
 
 
 
69f97f6
79421ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69f97f6
 
 
79421ed
 
 
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
FROM jellyfin/jellyfin:latest

USER root

RUN apt-get update \
    && apt-get install --yes --no-install-recommends nginx sqlite3 \
    && rm -rf /var/lib/apt/lists/* \
    && mkdir -p /cache /config /config-backup /data /tmp/nginx-client-body

RUN cat >/etc/nginx/nginx.conf <<'EOF'
events {}

http {
  client_max_body_size 0;

  access_log /dev/stdout;
  error_log /dev/stderr warn;

  map $http_upgrade $connection_upgrade {
    default upgrade;
    '' close;
  }

  server {
    listen 7860;

    location / {
      proxy_pass http://127.0.0.1:8096;

      proxy_buffering off;
      proxy_connect_timeout 300s;
      proxy_http_version 1.1;
      proxy_read_timeout 300s;
      proxy_request_buffering off;
      proxy_send_timeout 300s;
      send_timeout 300s;

      proxy_set_header Connection $connection_upgrade;
      proxy_set_header Host $host;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header X-Forwarded-Host $host;
      proxy_set_header X-Forwarded-Port 443;
      proxy_set_header X-Forwarded-Proto https;
      proxy_set_header X-Real-IP $remote_addr;
    }
  }
}
EOF

RUN cat >/start.sh <<'EOF'
#!/bin/sh
set -eu

backup_config() {
    echo "[backup] starting config backup"

    if [ ! -f /config/data/jellyfin.db ]; then
        echo "[backup] skip: database not found yet"
        return 0
    fi

    TS="$(date -u +%Y%m%d-%H%M%S)"
    STAGE="/tmp/jellyfin-config-backup-$TS"
    OUT="/config-backup/jellyfin-config-$TS.tar.gz"

    rm -rf "$STAGE"
    mkdir -p "$STAGE"

    cp -a /config/. "$STAGE/" 2>/dev/null || true

    if [ -d /config/data ]; then
        find /config/data -type f -name "*.db" | while read -r DB; do
            REL="${DB#/config/}"
            mkdir -p "$STAGE/$(dirname "$REL")"
            echo "[backup] sqlite backup: $REL"
            sqlite3 "$DB" ".backup '$STAGE/$REL'" || echo "[backup] warning: failed to backup $DB"
        done
    fi

    tar -czf "$OUT" -C "$STAGE" .
    cp "$OUT" /config-backup/latest.tar.gz

    rm -rf "$STAGE"

    echo "[backup] done: $OUT"

    ls -1t /config-backup/jellyfin-config-*.tar.gz 2>/dev/null | tail -n +8 | xargs -r rm -f
}

restore_config() {
    if [ ! -f /config/data/jellyfin.db ] && [ -f /config-backup/latest.tar.gz ]; then
        echo "[restore] restoring config from /config-backup/latest.tar.gz"
        mkdir -p /config
        tar -xzf /config-backup/latest.tar.gz -C /config
        echo "[restore] done"
    else
        echo "[restore] no restore needed"
    fi
}

backup_loop() {
    sleep 600

    while true; do
        backup_config || echo "[backup] backup failed"
        sleep 3600
    done
}

restore_config

/jellyfin/jellyfin \
    --datadir /config \
    --cachedir /cache \
    --ffmpeg /usr/lib/jellyfin-ffmpeg/ffmpeg \
    --service &
JELLYFIN_PID=$!

nginx -g 'daemon off;' &
NGINX_PID=$!

backup_loop &
BACKUP_PID=$!

trap 'echo "[shutdown] stopping"; backup_config || true; kill $JELLYFIN_PID $NGINX_PID $BACKUP_PID 2>/dev/null || true' TERM INT

while kill -0 $JELLYFIN_PID 2>/dev/null && kill -0 $NGINX_PID 2>/dev/null; do
    sleep 2
done

kill $JELLYFIN_PID $NGINX_PID $BACKUP_PID 2>/dev/null || true
wait

EOF

RUN chmod +x /start.sh

EXPOSE 7860

ENTRYPOINT []

CMD ["/start.sh"]