by Dargor
Version 1.0
Introduction
I did this script for my FF6 SDK because I was tired of loosing important data. It's really frustrating!
This script will create backups of all your data directory, in case something terrible happens to your project.
Each time you'll test your game, the script will automatically create a backup directory and another directory within it named using the following format:
year_month_day_hour_min_sec
The script will also automatically delete the oldest backup directories if the number of directories exceeds the maximum you have specified.
To use the script, simply copy/paste it above main. You also need to go in Main and add Kernel.make_backup, right under begin.
Don't forget to remove it in the final version of your game!
Features
- You can choose the path and folder name of the backup directory
- You can set the maximum number of backup directory to create
- Compatible with XP and VX!
Script
Code:
#==============================================================================
# ** [XP/VX] Backup System
#------------------------------------------------------------------------------
# © Dargor, 2008
#Â 01/11/08
#Â Version 1.0
#------------------------------------------------------------------------------
#Â VERSION HISTORY:
#Â Â - 1.0 (01/11/08), Initial release
#------------------------------------------------------------------------------
#Â INSTRUCTIONS:
#Â Â - Paste this above Main
#Â Â - In Main, below begin, add: Kernel.make_backup
#==============================================================================
#==============================================================================
# ** Backup Configuration Module  Â
#==============================================================================
module Backup
 # XP/VX, Set to true when using XP, set to false when using VX
 XP = true
 # Path where all backup folders will be created
 Path = "Data/Backup"
 # Maximum number of backups allowed
 Maximum = 10
end
#==============================================================================
# ** Kernel  Â
#==============================================================================
module Kernel
 #--------------------------------------------------------------------------
 # * Get RPG Maker data extension
 #--------------------------------------------------------------------------
 def self.data_extension
  if Backup::XP
   return 'rxdata'
  else
   return 'rvdata'
  end
 end
 #--------------------------------------------------------------------------
 # * Make Database
 #--------------------------------------------------------------------------
 def self.make_database
  # Load database
  $data_actors    = load_data("Data/Actors.#{self.data_extension}")
  $data_classes    = load_data("Data/Classes.#{self.data_extension}")
  $data_skills    = load_data("Data/Skills.#{self.data_extension}")
  $data_items     = load_data("Data/Items.#{self.data_extension}")
  $data_weapons    = load_data("Data/Weapons.#{self.data_extension}")
  $data_armors    = load_data("Data/Armors.#{self.data_extension}")
  $data_enemies    = load_data("Data/Enemies.#{self.data_extension}")
  $data_troops    = load_data("Data/Troops.#{self.data_extension}")
  $data_states    = load_data("Data/States.#{self.data_extension}")
  $data_animations  = load_data("Data/Animations.#{self.data_extension}")
  # If using XP, load tilesets data
  if Backup::XP
   $data_tilesets   = load_data("Data/Tilesets.#{self.data_extension}")
  end
  $data_common_events = load_data("Data/CommonEvents.#{self.data_extension}")
  $data_system    = load_data("Data/System.#{self.data_extension}")
  $data_scripts    = load_data("Data/Scripts.#{self.data_extension}")
  $data_mapinfos   = load_data("Data/MapInfos.#{self.data_extension}")
  # If using VX, load areas data
  if !Backup::XP
   $data_areas    = load_data("Data/Areas.#{self.data_extension}")
  end
  # Load maps
  self.load_maps
 end
 #--------------------------------------------------------------------------
 # * Load Maps
 #--------------------------------------------------------------------------
 def self.load_maps
  entries = Dir.entries('Data')
  entries.delete('.')
  entries.delete('..')
  $map_list = []
  for entry in entries
   for i in 1..999
    map_id = sprintf("%03d", i)
    $map_list << entry if entry == "Map#{map_id}.#{self.data_extension}"
   end
  end
  $last_data_maps = $data_maps if $data_maps != nil
  $data_maps = {}
  for i in 0...$map_list.size
   path = "Data/" + $map_list[i]
   $data_maps[i + 1] = load_data(path)
  end
 end
 #--------------------------------------------------------------------------
 # * Get RPG Maker data extension
 #--------------------------------------------------------------------------
 def self.save_game_data
  # Create backup directory
  time = Time.new
  year = time.strftime('%Y')
  month = time.strftime('%b')
  day = time.strftime('%d')
  hour = time.strftime('%H')
  min = time.strftime('%M')
  sec = time.strftime('%S')
  folder = "#{year}_#{month}_#{day}_#{hour}_#{min}_#{sec}"
  unless FileTest.directory?("#{Backup::Path}/#{folder}")
   Dir.mkdir("#{Backup::Path}/#{folder}")
  end
  # Save game data
  Kernel.save_data($data_actors,    "#{Backup::Path}/#{folder}/Actors.#{self.data_extension}")
  Kernel.save_data($data_classes,    "#{Backup::Path}/#{folder}/Classes.#{self.data_extension}")
  Kernel.save_data($data_skills,    "#{Backup::Path}/#{folder}/Skills.#{self.data_extension}")
  Kernel.save_data($data_items,     "#{Backup::Path}/#{folder}/Items.#{self.data_extension}")
  Kernel.save_data($data_weapons,    "#{Backup::Path}/#{folder}/Weapons.#{self.data_extension}")
  Kernel.save_data($data_armors,    "#{Backup::Path}/#{folder}/Armors.#{self.data_extension}")
  Kernel.save_data($data_enemies,    "#{Backup::Path}/#{folder}/Enemies.#{self.data_extension}")
  Kernel.save_data($data_troops,    "#{Backup::Path}/#{folder}/Troops.#{self.data_extension}")
  Kernel.save_data($data_states,    "#{Backup::Path}/#{folder}/States.#{self.data_extension}")
  Kernel.save_data($data_animations,  "#{Backup::Path}/#{folder}/Animations.#{self.data_extension}")
  # If using XP, save tilesets data
  if Backup::XP
   Kernel.save_data($data_tilesets,   "#{Backup::Path}/#{folder}/Tilesets.#{self.data_extension}")
  end
  Kernel.save_data($data_common_events, "#{Backup::Path}/#{folder}/CommonEvents.#{self.data_extension}")
  Kernel.save_data($data_system,    "#{Backup::Path}/#{folder}/System.#{self.data_extension}")
  Kernel.save_data($data_scripts,    "#{Backup::Path}/#{folder}/Scripts.#{self.data_extension}")
  Kernel.save_data($data_mapinfos,   "#{Backup::Path}/#{folder}/MapInfos.#{self.data_extension}")
  # If using VX, save areas data
  if !Backup::XP
   Kernel.save_data($data_areas,  "#{Backup::Path}/#{folder}/Areas.#{self.data_extension}")
  end
  # Save maps
  for i in 0...$data_maps.size
   map = $data_maps[i + 1]
   filename = $map_list[i]
   path = "#{Backup::Path}/#{folder}/" + filename
   Kernel.save_data(map, path)
  end
 end
 #--------------------------------------------------------------------------
 # * Make data backup
 #--------------------------------------------------------------------------
 def self.make_backup
  unless FileTest.directory?("#{Backup::Path}")
   Dir.mkdir("#{Backup::Path}")
  end
  self.clear_backup
  self.make_database
  self.save_game_data
  $data_maps = $last_data_maps
 end
 #--------------------------------------------------------------------------
 # * Clear backup
 #--------------------------------------------------------------------------
 def self.clear_backup
  entries = Dir.entries("#{Backup::Path}")
  entries.delete('.')
  entries.delete('..')
  return if entries.size < Backup::Maximum
  for i in 0...(entries.size - Backup::Maximum)
   files = Dir.entries("#{Backup::Path}/#{entries[i]}")
   files.delete('.')
   files.delete('..')
   for file in files
    File.delete("#{Backup::Path}/#{entries[i]}/#{file}")
   end
   Dir.delete("#{Backup::Path}/#{entries[i]}")
  end
 end
end
Hope you like it!
-Dargor