The International Simutrans Forum

 

Author Topic: Question about shell scripts for backing up of server saved games  (Read 2766 times)

0 Members and 1 Guest are viewing this topic.

Offline jamespetts gb

  • Simutrans-Extended project coordinator
  • Devotee
  • *
  • Posts: 18617
  • Cake baker
    • Bridgewater-Brunel
  • Languages: EN
Does anyone here know enough about shell scripting to assist me with a matter relating to the maintenance of my Simutrans-Experimental server?

I have set a cron job to force-sync every hour, and a script to rotate the saved games to up to five backup files every half hour. However, ideally, I should like to be able to check whether either thing needs to be run, and run it only if it does: in other words, check whether the server's saved game has in fact been modified in the last hour, and omit to force-sync if it has, and check whether the server's saved game has been modified in the last half hour (I might change this to hour) and omit to rotate if it has not. I could not find the answer to this question when I looked at the basic shell scripting tutorials, but perhaps I am looking in the wrong place; but I wondered whether anyone here had experience of running this sort of thing and might be able to assist with some example scripts or the like.

Thank you in advance!

Offline Ashley

  • Coder/Patcher
  • Devotee
  • *
  • Posts: 1288
    • entropy.me.uk
Re: Question about shell scripts for backing up of server saved games
« Reply #1 on: December 31, 2012, 01:11:58 PM »
Store the file modification time whenever you run the backup action (use stat to get it), then have your backup script check if the modification time is newer than the stored file modification time and only run if it is.

Determining if the file has actually changed could be done using a checksum, e.g. "sha1sum <savefile>". Run this and store the checksum output along with the last modification time. If both differ you should do a backup, if only the modification time differs then the save hasn't been updated since the last backup.

Simples.

Offline jamespetts gb

  • Simutrans-Extended project coordinator
  • Devotee
  • *
  • Posts: 18617
  • Cake baker
    • Bridgewater-Brunel
  • Languages: EN
Re: Question about shell scripts for backing up of server saved games
« Reply #2 on: December 31, 2012, 03:30:38 PM »
I think that I have managed to put together a sensible script. I have the following script:

Code: [Select]
# Rotation and backup script for the Simutrans-Experimental server
# Written by James E. Petts, December 2012

# This script is designed to:
# (1) check whether the saved game has been modified in the last hour;
# (2) if it has not, run the force-sync command;
# (3) check again whether the saved game has been modified in the last hour
# (the force-sync command might or might not modify it depending on whether -
# (a) Simutrans-Experimental is running; and
# (b) there are any players connected); and
# (4) if it has, rotate the saved games using the rotate.sh script.

echo
echo "******"
echo
echo "Running the backup and rotation script..."
echo
SAVEGAME="/usr/share/games/simutrans-experimental/simutrans/server13353-network.sve"

# Check that the server saved game exists at all
if [ ! -e $SAVEGAME ]
then
echo "Error: saved game not found at $SAVEGAME"
echo
exit 1
fi

# Check the current time and the last modification time of the saved game file
# Use Unix time (seconds since 1 Jan. 1970)
NOWTIME=$(date +%s)
NOWTIME_FRIENDLY=date

SAVEMODTIME=$(stat -c %Y $SAVEGAME)
SAVEMODTIME_FRIENDLY="stat -c %y $SAVEGAME"

HOURAGO=$(expr $NOWTIME - 3600)

echo "Current time:"
$NOWTIME_FRIENDLY
echo
echo "Game last saved:"
$SAVEMODTIME_FRIENDLY
echo

# Check whether the saved game was modified more than an hour ago
if [ $HOURAGO -gt $SAVEMODTIME ]
then
echo "The game was last saved more than an hour ago. Run a force sync."
echo
/root/force-sync.sh

# Check again whether the saved game was modified more than an hour ago.
# But first, wait two minutes, as it might take a while to execute the
# force-sync command.
sleep 120
else
echo "The game was last saved within the hour - do not run a force sync."
echo
fi

NEWSAVEMODTIME=$(stat -c %Y $SAVEGAME)

if [ $HOURAGO -gt $NEWSAVEMODTIME ]
then
echo "The game was not updated with a force-sync. Either the server is not running, or no clients are connected."
echo
exit 2
else
echo "The saved game has been updated in the last hour. Rotate the backups."
echo
/root/rotate.sh
fi

Rotate.sh is the following:

Code: [Select]
# Backup rotation script for the Simutrans-Experimental server
# Written by James E. Petts, December 2012

echo "Rotating save backups"
cp /usr/share/games/simutrans-experimental/simutrans/backup4.sve /usr/share/games/simutrans-experimental/simutrans/backup5.sve
cp /usr/share/games/simutrans-experimental/simutrans/backup3.sve /usr/share/games/simutrans-experimental/simutrans/backup4.sve
cp /usr/share/games/simutrans-experimental/simutrans/backup2.sve /usr/share/games/simutrans-experimental/simutrans/backup3.sve
cp /usr/share/games/simutrans-experimental/simutrans/backup1.sve /usr/share/games/simutrans-experimental/simutrans/backup2.sve
cp /usr/share/games/simutrans-experimental/simutrans/server13353-network.sve /usr/share/games/simutrans-experimental/simutrans/backup1.sve

echo
echo "Rotating password hash backups"
cp /usr/share/games/simutrans-experimental/simutrans/pwdhash-backup4.sve /usr/share/games/simutrans-experimental/simutrans/pwdhash-backup5.sve
cp /usr/share/games/simutrans-experimental/simutrans/pwdhash-backup3.sve /usr/share/games/simutrans-experimental/simutrans/pwdhash-backup4.sve
cp /usr/share/games/simutrans-experimental/simutrans/pwdhash-backup2.sve /usr/share/games/simutrans-experimental/simutrans/pwdhash-backup3.sve
cp /usr/share/games/simutrans-experimental/simutrans/pwdhash-backup1.sve /usr/share/games/simutrans-experimental/simutrans/pwdhash-backup2.sve
cp /usr/share/games/simutrans-experimental/simutrans/server13353-pwdhash.sve /usr/share/games/simutrans-experimental/simutrans/pwdhash-backup1.sve

And force-sync.sh is the following:

Code: [Select]
# Shell script to run a force-sync on the running Simutrans-Experimental server
# Written by James E. Petts, December 2012

echo "Running a force-sync..."

/usr/share/games/simutrans-experimental/simutrans/nettool -s bridgewater-brunel.me.uk -p [**PASSWORD REDACTED**] force-sync

My cron tab is set to run the first shell script every hour and output the results to /var/log/simutrans/backup-rotate.log . I have a 7 day daily log rotation in place using logrotate. Hopefully, this should keep things properly backed up.

Feel free, anyone, to copy or modify the scripts - I am happy to make them available under the GNU GPL v. 3.