Skip to content

Instantly share code, notes, and snippets.

@Crozzers
Created January 1, 2026 23:32
Show Gist options
  • Select an option

  • Save Crozzers/1364e6a7faf3fc0e6d23f1bad117ece2 to your computer and use it in GitHub Desktop.

Select an option

Save Crozzers/1364e6a7faf3fc0e6d23f1bad117ece2 to your computer and use it in GitHub Desktop.
Bash script for managing local save files
#!/bin/bash
saveFile=~/.localsave.cfg
usage="
A small script that backs up save files to a directory, and update the local copies with the latest version.
Configuration is handled via file at $saveFile and the format is:
localsave_dir=/path/to/back/saves/up/in
GAME1=/path/to/savefiles/for/game1
GAME2=/path/to/savefiles/for/game2
...
Usage:
./localsave.sh ( backs up all games in cfg file )
./localsave.sh GAME1 ( backs up just GAME1 )
./localsave.sh GAME1 /backups/ ( backs up just GAME1 to /backups instead of value in cfg file )
"
if [[ $1 == "help" || $1 == "--help" || $1 == "?" ]]; then
printf "$usage"
exit 0
fi
if [ ! -f $saveFile ]; then
echo "No savefile configured"
exit 1
fi
if [[ -z "$1" ]]; then
for game in $(grep -v localsave_dir= $saveFile | cut -d = -f 1); do
if [[ ! -z "$game" ]]; then
bash "${BASH_SOURCE[0]}" $game
echo ""
echo "==========="
echo ""
fi
done
exit 0
fi
echo "Game: $1"
localFiles=$(grep "$1=" $saveFile | cut -d = -f 2)
if [[ -z "$localFiles" ]]; then
echo "No config for game detected"
exit 1
fi
echo "Local saves location: $localFiles"
if [ ! -d "$localFiles" ]; then
echo "Local save dir doesn't exist"
exit 1
fi
localMTime=0
if [[ -z $(ls "$localFiles") ]]; then
echo "No local saves detected"
else
localMTime=$(stat -c %Y "$localFiles")
fi
backupDir=$(grep "localsave_dir=" $saveFile | cut -d = -f 2)
if [[ ! -z "$2" ]]; then
backupDir=$2
fi
echo "Backup dir: $backupDir"
if [ ! -d "$backupDir" ]; then
echo "Backup dir doesn't exist"
exit 1
fi
backupMTime=0
if [[ -z $(ls "$backupDir") ]]; then
echo "No backup savefiles detected"
else
latestBackup=$backupDir/$1/$(ls "$backupDir/$1" | grep -E '[0-9]+-.+' | tail -n 1)
echo "Latest backup detected: $latestBackup"
backupMTime=$(stat -c %Y "$latestBackup")
fi
echo "Local MTime: $localMTime"
echo "Backup MTime: $backupMTime"
tmpBackup="$backupDir/$1/$localMTime-$(hostname)/"
echo "Backing up local files to $tmpBackup"
mkdir -p "$tmpBackup"
cp -r "$localFiles/." "$tmpBackup/"
if [[ $backupMTime -gt $localMTime ]]; then
echo "Backup MTime is greater than local MTime. Pulling..."
cp -r "$latestBackup/." "$localFiles/"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment