Jump to content

Linux quicksave copy script.


Recommended Posts

I see some threads about not having multiple quicksaves, and how they would be helpful. In this thread:

http://forum.kerbalspaceprogram.com/threads/55258-A-real-save-feature%21%21%21%21%21%21%21%21%21%21%21

I mentioned that it would take a simple script on a linux machine to copy over the quicksave files. Here is what I have come up with so far. I can't attach files to I will just post out the script.

This script searches your home subdir for any quicksave.sfs files, notes the parent subdir, date and time, and stores them in a folder under your home subdir called ~/kspsaves. Note, I put this in a variable so that you can change the stored location if you want.

You will end up with files like this in the kspsaves subdir:

-rw-rw-r-- 1 stranded stranded 16662 Nov 4 10:59 quicksavelog.txt

-rw-rw-r-- 1 stranded stranded 108868 Nov 4 10:59 quicksave.sfs.Demo.2013_1104-10:59:19

A couple of things to note. The first thing the script does is grep for KSP.x86. If Kerbal Space Program isn't running, then don't waste time. Then if KSP is running, then I have the script do a loop looking for all of the quicksave.sfs files, and they get a unique name in the kspsaves subdir. Now I also do a check using md5sums to keep from having duplicate files taking up space. If a file that is going to be copied over already has a matching file, the temporarily copied file is removed (ie: not copied). Your original quicksave.sfs file is never touched. I also set my cron up to do this search and copy every 10 minutes (see example at the bottom). You can set this to run at any interval you want. The theory here is that when you make a particular quicksave that you might want to go to note the date and time. Now if you want it copied over before the cron job rolls around, feel free to just run the script at anytime and your quicksave wil be copied over. I placed the saveKSP.sh script in my home subdir just to make it easy.

This is my saveKSP.sh script:



#!/bin/bash
# This script will find any Kerbal Space Program quicksave file and copy it
# to a kspsaves subdir that it creates in your home subdir.
# Once a file is found, it is copied and timestamped like so:
# quicksave.sfs.{parent dir}.YEAR_MMDD-HH:MM:SS
#
# Written by Stranded - 11/04/2013

#If you want to store the files in a different subdir, here's where you can specify
SAVEDIR=~/kspsaves

# Set the loglevel to 0 for low, or 1 for higher logging. This will make the quicksavelog.txt
# file larger though. Hint, I set up a cron job to clear this file out monthly, and also
# wrote this so the script doesn't log when Kerbal Space Program isn't running
LOGLEVEL=0

# First, check and see if our save subdir exists. If it doesn’t, then create it.
if [ ! -d $SAVEDIR ]; then
mkdir $SAVEDIR
fi

# Making a log file to store output
if [ ! -f $SAVEDIR/quicksavelog.txt ]; then
touch $SAVEDIR/quicksavelog.txt
fi

# If Kerbal Space Program is running then we search for the quicksave file
# and then back it up. Since both the 32 and 64 bit versions start with KSP.x86 we can
# grep for that and get either version.
if ps -ef | grep KSP.x86 | grep -v grep >/dev/null
then
#Note the beginning of the loop in the log
if [ $LOGLEVEL -eq 1 ]; then
echo "" >>$SAVEDIR/quicksavelog.txt
echo "Beginning saveKSP.sh loop at `date +"%Y_%m%d-%H:%M:%S"`">>$SAVEDIR/quicksavelog.txt
fi

#In the case of more than one quicksave file, this will loop through all files found
for FOUNDFILE in `find ~ -name quicksave.sfs -print`
do
# Store the parent dir in a variable
PARENTDIR=`echo $FOUNDFILE | awk -F"/" {'print $(NF-1)'}`
# Create the new filename based on the parent dir, date and time.
FILETOSAVE="$SAVEDIR/quicksave.sfs.$PARENTDIR.`date +"%Y_%m%d-%H:%M:%S"`"
# This is our very non glamorous temp file name
TEMPSAVE="$SAVEDIR/tempsave.sfs"

# Copy the found quicksave.sfs file and copy it as a temp file to the kspsaves subdir.
cp $FOUNDFILE $TEMPSAVE

# Lets check and see if the file is the same as the last saved file.
FILEMD5=`md5sum $TEMPSAVE | cut -d ' ' -f 1`

# Time to loop through the md5sums.txt file and see if this file already exists
if md5sum $SAVEDIR/quicksave* | cut -d ' ' -f 1 | grep $FILEMD5 >/dev/null
then
# A file with the same Md5 exists so don't save it.
rm $TEMPSAVE
echo "File $FILETOSAVE has a checksum $FILEMD5 that already exists - removing." >> $SAVEDIR/quicksavelog.txt
else

# The file is orginal
# Now that we have tested to see if it exists, save the file and the md5sum
mv $TEMPSAVE $FILETOSAVE
echo "File $FILETOSAVE checksum $FILEMD5 saved." >> $SAVEDIR/quicksavelog.txt
fi
done
# End of loop, making note in the log file.
if [ $LOGLEVEL -eq 1 ]; then
echo "Ending saveKSP.sh loop at `date +"%Y_%m%d-%H:%M:%S"`">>$SAVEDIR/quicksavelog.txt
fi
fi

END of saveKSP.sh script

From there, I have the script run in a crontab entry every 10 minutes as follows:

0,10,20,30,40,50 * * * * ~/saveKSP.sh 1>/dev/null 2>/dev/null

I also decided to put in a monthly cron entry to clear out the log file:

0 0 1 * * >~/kspsaves/quicksavelog.txt 1>/dev/null 2>/dev/null

Edited by Stranded
Fixing the comments in the script
Link to comment
Share on other sites

This thread is quite old. Please consider starting a new thread rather than reviving this one.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...