-
-
Notifications
You must be signed in to change notification settings - Fork 117
/
Copy pathvalheim_backup.sh
212 lines (173 loc) · 6.71 KB
/
valheim_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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#!/bin/bash
###############################################################################
# Valheim Server Backup and Management Script
#
# Purpose: Automatically backup Valheim game servers and manage server shutdown
# Author: GingerSwede
# Last Updated: 2025-01-07
#
# This script performs the following operations:
# 1. Reads world names from a configuration file
# 2. Safely stops each game server
# 3. Ensures complete process termination
# 4. Clears system memory cache
# 5. Creates dated backups
# 6. Removes old backups
# 7. Logs all operations
# 8. Reboots server if all operations successful
###############################################################################
#------------------------------------------------------------------------------
# Configuration Variables
#------------------------------------------------------------------------------
# File containing list of world names to backup
FILENAME="/home/steam/worlds.txt"
# Directory where backups will be stored
BACKUP_PATH="/home/steam/backups"
# Path for logging operations
LOGFILE="/var/log/valheim-backup"
# Base directory where Valheim stores world data
VALHEIM_BASE_PATH="/home/steam/.config/unity3d/IronGate/Valheim"
# Number of days to keep backups before deletion
BACKUP_RETENTION_DAYS=14
# Maximum time in seconds to wait for server shutdown
MAX_WAIT_TIME=60
#------------------------------------------------------------------------------
# Error Handling Configuration
#------------------------------------------------------------------------------
# Exit script immediately if any command exits with non-zero status
set -e
#------------------------------------------------------------------------------
# Utility Functions
#------------------------------------------------------------------------------
# Function: log
# Purpose: Write timestamped messages to log file
# Parameters: $1 - Message to log
log() {
local timestamp=$(date +"%Y-%m-%d %T")
echo "[$timestamp] $1" >> "$LOGFILE"
}
# Function: wait_for_server_shutdown
# Purpose: Ensure server processes are completely terminated
# Parameters: $1 - World name to check
# Returns: 0 on success, 1 on failure
wait_for_server_shutdown() {
local world_name="$1"
local wait_time=0
log "Waiting for $world_name processes to fully terminate"
while true; do
# Check for both service process and game server process
# Returns true if NO processes are found (desired state)
if ! pgrep -f "valheimserver_$world_name" > /dev/null && \
! pgrep -f "valheim_server.x86_64.*$world_name" > /dev/null; then
log "Server $world_name processes have terminated"
return 0
fi
# Increment wait counter and check timeout
((wait_time++))
if [[ $wait_time -ge $MAX_WAIT_TIME ]]; then
log "ERROR: Server $world_name failed to shut down completely after $MAX_WAIT_TIME seconds"
# Force kill any remaining processes
pkill -9 -f "valheimserver_$world_name" || true
pkill -9 -f "valheim_server.x86_64.*$world_name" || true
sleep 2
return 1
fi
sleep 1
done
}
# Function: clear_system_cache
# Purpose: Flush filesystem buffers and clear system memory cache
clear_system_cache() {
log "Clearing system cache"
# Sync forces cached writes to disk
sync
# Write '3' to drop_caches clears both page cache and slab objects/dentries
echo 3 > /proc/sys/vm/drop_caches || log "WARNING: Failed to clear system cache"
}
# Function: check_paths
# Purpose: Verify all required paths exist before starting operations
check_paths() {
if [[ ! -f "$FILENAME" ]]; then
log "ERROR: Worlds file not found at $FILENAME"
exit 1
fi
if [[ ! -d "$BACKUP_PATH" ]]; then
log "ERROR: Backup directory not found at $BACKUP_PATH"
exit 1
fi
}
# Function: backup_world
# Purpose: Perform complete backup operation for a single world
# Parameters: $1 - World name to backup
# Returns: 0 on success, 1 on failure
backup_world() {
local world_name="$1"
local today=$(date +%Y-%m-%d-%T)
local world_backup_path="$BACKUP_PATH/$world_name"
# Ensure backup directory exists
mkdir -p "$world_backup_path"
# Stop the server service
log "Stopping server $world_name"
if ! systemctl stop "valheimserver_$world_name.service"; then
log "ERROR: Failed to stop server $world_name"
exit 1
fi
# Ensure complete shutdown
if ! wait_for_server_shutdown "$world_name"; then
log "WARNING: Server shutdown may not be complete for $world_name"
fi
# Clear memory cache to ensure all data is written to disk
clear_system_cache
# Remove backups older than retention period
log "Cleaning old backups for $world_name"
find "$world_backup_path/" -name "valheim-backup-*.tgz" -mtime +"$BACKUP_RETENTION_DAYS" -type f -delete
# Create new backup
log "Creating backup for $world_name"
# backupresult
if ! tar czf "$world_backup_path/valheim-backup-$today.tgz" "$VALHEIM_BASE_PATH/$world_name/"*; then
log "ERROR: Backup failed for $world_name"
exit 1
fi
exit 1
}
#------------------------------------------------------------------------------
# Main Program Function
#------------------------------------------------------------------------------
main() {
local success_count=0
local total_worlds=0
log "Starting automated backup process"
check_paths
# Read world names into array using mapfile for better handling of special characters
mapfile -t world_names < "$FILENAME"
total_worlds=${#world_names[@]}
# Verify we have worlds to process
if [[ $total_worlds -eq 0 ]]; then
log "ERROR: No worlds found in $FILENAME"
exit 1
fi
# Process each world
for world in "${world_names[@]}"; do
local backup_success=$(backup_world "$world")
if $backup_success; then
log "Successfully backed up $world"
success_count=$((success_count+1))
fi
done
# Log final results
log "Backup complete: $success_count/$total_worlds servers backed up successfully"
# Only reboot if all backups were successful
if [[ $success_count -eq $total_worlds ]]; then
log "All backups successful, initiating reboot"
sync # Final filesystem sync before reboot
reboot
else
log "WARNING: Some backups failed, manual intervention required"
exit 1
fi
}
#------------------------------------------------------------------------------
# Script Execution
#------------------------------------------------------------------------------
# Execute main program
main