This script is designed to manage map files for a CS:GO server. It checks for new map files in a specified directory, and based on their size, it either compresses them or leaves them as-is. The script also ensures it doesn't run multiple instances simultaneously.
workshop
subdirectory.workshop
folder/home/csgo/serverfiles/csgo/maps/
/home/fdl/maps
/home/csgo/last_files.txt
.bsp
.nav
#!/bin/bash
# Check if the script is already running
pidof -o %PPID -x $0 >/dev/null && echo "ERROR: Script $0 already running" && exit 1
# Define the source and destination directories
SOURCE_DIR="/home/csgo/csgo/maps/"
DEST_DIR="/home/fdl/maps"
RECORD_FILE="/home/csgo/last_files.txt"
# Define the file extensions to copy and compress
file_extensions=("*.bsp" "*.nav")
# Get the list of files from the last run
if [ -f "$RECORD_FILE" ]; then
mapfile -t last_files < "$RECORD_FILE"
else
last_files=()
fi
# Get the current list of files, excluding the workshop folder
current_files=($(find "$SOURCE_DIR" -type f \( -name "${file_extensions[0]}" -o -name "${file_extensions[1]}" \) ! -path "$SOURCE_DIR/workshop/*" -exec basename {} \;))
# Check for new files
new_files=()
for file in "${current_files[@]}"; do
if ! [[ "${last_files[@]}" =~ "$file" ]]; then
new_files+=("$file")
fi
done
if [ ${#new_files[@]} -gt 0 ]; then
# If there are new files, run the script
# Loop over all new files
for src_file in "${new_files[@]}"; do
src_file_path="$SOURCE_DIR/$src_file"
# Define the destination file
dest_file="$DEST_DIR/$src_file"
dest_file_bz2="$DEST_DIR/$src_file.bz2"
# Get the file size in MB
file_size=$(stat -c%s "$src_file_path")
file_size_mb=$((file_size / 1024 / 1024))
# If the file size is 150MB or larger, copy and compress the file while keeping the original
if [ $file_size_mb -ge 150 ]; then
cp "$src_file_path" "$dest_file"
bzip2 -k "$dest_file"
echo "$dest_file file has been compressed and left the original file in the folder."
elif [ $file_size_mb -lt 150 ] && [ ! -f "$dest_file_bz2" ]; then
# If the file size is less than 150MB and the compressed file does not exist, copy and compress the file
cp "$src_file_path" "$dest_file"
bzip2 "$dest_file"
fi
done
# Remount the /home/fdl drive
umount /home/fdl
mount -a
# Update the record file with the current list of files
printf "%s\n" "${current_files[@]}" > "$RECORD_FILE"
fi