-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackup-plex-media-server.sh
More file actions
executable file
·66 lines (57 loc) · 2.09 KB
/
backup-plex-media-server.sh
File metadata and controls
executable file
·66 lines (57 loc) · 2.09 KB
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
#!/usr/bin/env bash
# Plex Media Server Backup Script
#
# This script creates a compressed backup of the Plex Media Server configuration
# and metadata, excluding cache, logs, and database journal files.
#
# Prerequisites:
# - Must be run with sudo/root privileges
# - BACKUP_DIR environment variable must be set to the destination backup directory
#
# Environment Variables:
# - BACKUP_DIR: Target directory for storing backups (required)
# - PLEX_SERVER_DIR: Optional custom path to Plex Media Server directory
#
# Usage:
# sudo BACKUP_DIR=/path/to/backups ./backup-plex-media-server.sh
#
# Exits:
# - 1: Not running as root
# - 2: Backup directory does not exist
# - 3: Error creating tar archive
set -e
set -u
FINAL_DIR="${BACKUP_DIR:?backup dir environment variable not set}"
PLEX_SERVER_DIR="${PLEX_SERVER_DIR:-/var/snap/plexmediaserver/common/Library/Application Support/Plex Media Server}"
TIMESTAMP=$(date +%y%m%d)
TEMP_FILE="/tmp/plexmediaserver.$TIMESTAMP.tar.gz"
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root. Please use sudo."
exit 1
fi
if [ ! -d "$FINAL_DIR" ]; then
echo "Backup directory '$FINAL_DIR' does not exist. Please create it."
exit 2
fi
tar -czf "$TEMP_FILE" \
--exclude='Plex Media Server/Cache/*' \
--exclude='Plex Media Server/Logs/*' \
--exclude='Plex Media Server/Plug-in Support/Databases/com.plexapp.plugins.library.db-shm' \
--exclude='Plex Media Server/Plug-in Support/Databases/com.plexapp.plugins.library.db-wal' \
--exclude='Plex Media Server/Plug-in Support/Databases/com.plexapp.plugins.library.db-journal' \
--exclude='Plex Media Server/Plug-in Support/Databases/com.plexapp.plugins.library.db' \
-C "$PLEX_SERVER_DIR" \
./
if [ $? -ne 0 ]; then
echo "Error creating tar archive. Please check the Plex Media Server directory."
exit 3
fi
mv "$TEMP_FILE" "$FINAL_DIR/."
COUNT_FILES=$(ls -1 "$FINAL_DIR" | wc -l)
while [ $COUNT_FILES -gt 10 ]; do
OLDEST_FILE=$(ls -1 "$FINAL_DIR" | head -1)
echo "Removing oldest backup file: $OLDEST_FILE"
rm "$FINAL_DIR/$OLDEST_FILE"
COUNT_FILES=$(ls -1 "$FINAL_DIR" | wc -l)
done
echo "Done."