Advertisement
esteband

CaveStorySavegameBackup.sh

Jul 10th, 2014
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.14 KB | None | 0 0
  1. # CaveStorySavegameBackup.sh
  2. # by Steve Duell
  3.  
  4. # FOR THIS SCRIPT TO WORK CORRECTLY,
  5. # IT MUST BE PLACED IN THE SAME DIRECTORY
  6. # AS THE CAVE STORY EXECUTABLE (doukutsu).
  7. # QUESTIONS & COMMENTS ARE WELCOME:
  8. # steveduell (at) gmail (dot) com
  9.  
  10.  
  11. #!/bin/bash
  12.  
  13.  
  14. # Set backup limits.
  15. LIMIT=N     # Y to limit, N for unlimited
  16. MAX=30      # Ignored if LIMIT is set to N.
  17.  
  18.  
  19. # Make a directory for backed up savegames if it's not already there:
  20. mkdir -p ./Savegame_Backups
  21.  
  22.  
  23. # Make a hidden timestamp file for comparing with your savegame:
  24. touch ./.timestamp
  25.  
  26.  
  27. # Start cave story & run it in the background:
  28. ./doukutsu &
  29.  
  30.  
  31. # Get the process ID & report it to the terminal:
  32. PID=$!
  33.  
  34.  
  35. # Title
  36. echo "***** The CAVE STORY AUTOMATIC SAVEGAME BACKER-UPPER by Steve Duell *****"
  37. echo ""
  38.  
  39.  
  40. # Inform user of the backup storage configuration:
  41. if [ $LIMIT = "N" ]
  42.     then
  43.         echo "The script is configured to store an unlimited number of backups."
  44.         echo "If you'd like to change this, set the LIMIT switch to \"Y\"."
  45.         echo ""
  46.     else
  47.         echo "The script is configured to store "$MAX" backups."
  48.         echo "You can increase the maximum by editing the \"MAX\" value."
  49.         echo "You can also set it to unlimited by setting the LIMIT switch to \"N\"."
  50.         echo ""
  51. fi
  52.  
  53.  
  54. # Loop while cave story is running:
  55. while kill -0 $PID 2> /dev/null; do
  56.  
  57.     # If the save file (Profile.dat) is new, then back it up & rename it:      
  58.     while test ./Profile.dat -nt ./.timestamp; do
  59.        
  60.         # Get the current time:
  61.         NOW=$(date "+%y%m%d_%H:%M:%S")
  62.         # Backup the savegame and rename with current time:                
  63.         cp ./Profile.dat ./Savegame_Backups/Profile_$NOW.dat
  64.         echo "**"
  65.         echo "Backed up new savegame as Profile_"$NOW".dat"
  66.        
  67.         # Count the number of backups living in the backups folder:
  68.         COUNT=$(ls ./Savegame_Backups -1 | wc -l)
  69.         # If the number exceeds the maximum allowed, remove the oldest one:    
  70.         if [ $LIMIT == Y ]
  71.            then        
  72.             if [ $COUNT -gt $MAX ]
  73.                then
  74.                    OLD=$(ls ./Savegame_Backups | sort -t _ -k 2,2 | head -n 1)
  75.                    rm ./Savegame_Backups/$OLD
  76.                    echo "** Maximum backup limit of "$MAX" exceeded. Removing oldest backup."
  77.                    echo "** To increase the maximum, set the MAX variable,"
  78.                    echo "** or set the LIMIT switch to N for unlimited backups."
  79.                    echo ""
  80.                else
  81.                    REMAIN=$((MAX - COUNT))
  82.                    echo "You have "$REMAIN" backup(s) remaining before reaching your limit."
  83.                    echo ""
  84.             fi
  85.            fi
  86.    
  87.         # Reset timestamp file:
  88.         touch ./.timestamp
  89.     done
  90.  
  91.     # Wait 1 second before checking for a new savegame:
  92.     sleep 1    
  93.                            
  94. done
  95.  
  96. # Countdown to allow users time to view the output of the script before it close automatically:
  97. timecount(){
  98.         min=0
  99.         while [ $min -ge 0 ]; do
  100.                 sec=59
  101.         echo "..."     
  102.         echo "Cave Story has shut down."
  103.         echo "The script will continue to run until the timer runs out."
  104.         echo "Press Ctrl^C to exit."
  105.                 while [ $sec -ge 0 ]; do
  106.                         echo -ne "00:0$min:$sec\033[0K\r"
  107.                         sec=$((sec-1))
  108.                         sleep 1
  109.                 done
  110.                 min=$((min-1))
  111.         done
  112. }
  113. timecount
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement