forked from SenexCrenshaw/StreamMaster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backup.sh
77 lines (67 loc) · 2.36 KB
/
backup.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
#!/bin/bash
# Source environment variables
. /env.sh
# Configuration
backup_dirs="/config/PlayLists /config/Logs /config/Cache/SDJson" # Space-separated list of directories
backup_files="/config/settings.json /config/logsettings.json" # Space-separated list of files
backup_path="/config/backups"
versions_to_keep=5
timestamp=$(date +"%Y-%m-%d_%H-%M-%S")
db_backup_file="db_$timestamp.gz"
files_backup_file="files_$timestamp.tar.gz"
backup_file="$backup_path/backup_$timestamp.tar.gz"
# Determine versions to keep from command line argument, environment variable, or default
if [ ! -z "$1" ]; then
versions_to_keep=$1
elif [ ! -z "$BACKUP_VERSIONS_TO_KEEP" ]; then
versions_to_keep=$BACKUP_VERSIONS_TO_KEEP
else
versions_to_keep=5
fi
# Check if backup directory exists, if not, create it
if [ ! -d "$backup_path" ]; then
mkdir -p "$backup_path"
fi
# Backup PostgreSQL database
backup_database() {
pg_dump -U $POSTGRES_USER $POSTGRES_DB | gzip > "$backup_path/$db_backup_file"
echo "Database backup completed: $db_backup_file"
}
# Backup files and directories
backup_files_and_dirs() {
local items_to_backup=()
for item in $backup_dirs $backup_files; do
if [ -e "$item" ]; then
items_to_backup+=("$item")
else
echo "Warning: $item does not exist and will not be included in the backup."
fi
done
if [ ${#items_to_backup[@]} -gt 0 ]; then
tar -czf "$backup_path/$files_backup_file" "${items_to_backup[@]}" 2>/dev/null
echo "Directories and files backup completed: $files_backup_file"
else
echo "No files or directories exist for backup."
fi
}
# Create one backup file
create_backup() {
tar -czf "$backup_file" -C "$backup_path" $db_backup_file $files_backup_file 2>/dev/null
echo "Backup file created: $backup_file"
}
# Function to limit the number of backups
limit_backups() {
cd $backup_path
# Remove individual db and files backups to clean up
rm -f $db_backup_file $files_backup_file
# Keep only the specified number of backup versions
(ls -t backup_*.tar.gz | head -n $versions_to_keep; ls backup_*.tar.gz) | sort | uniq -u | xargs --no-run-if-empty rm
echo "Old backups cleanup completed."
}
# Main script execution
echo "Starting backup process..."
backup_database
backup_files_and_dirs
create_backup
limit_backups
echo "Backup process finished."