Auto backup for Godot 4
I do think Godot has a auto back system. At least I could not find one. So I made this one. it is not perfect. but it does work. the idea is to have a backup it I made changes I later regret. this way I can simply go back and either resume from an older version or copy only the parts of the code I needed to undo.
you can set it to do a full backup each time you run your project or just back up parts of your project. just be aware of you total file and path size. this project has ran good on windows 10. not sure of other operating systems.
just copy the code into a blank gd file. ie.. autobackup.gd
fallow instructions at the bottom.
extends Node
#Version 0.04
# 1. just add it to autoload
# 2. You must include var version = "0.00" at the top of your main tree node
# allow godot process to catchup
var wait = 2
var copy_subpaths = true # this will copy all the files in all the paths. Except .godot and .Backup
# optional list
var backuplist = [
""
]
var backup_godot_folder = false
var mainsorce = "res://"
var backup = "res://.Backup"
var pathlist = []
func _process(delta):
if wait:
wait -= 1
if !wait:
run_backup()
func run_backup():
if mainsorce == "":
mainsorce = "res://"
if backup == "":
backup = "res://.Backup"
# check if the slash is at the end and add it if it's not.
if mainsorce.right(1) != "/":
mainsorce += "/"
if backup.right(1) == "/":
backup = backup.left(backup.length()-1)
# get the date and time stamp
var stamp = stamp()
var dir = DirAccess.open(mainsorce)
#make sure the Backup folder exists and create it if it does not.
if !dir.dir_exists(backup):
dir.make_dir(backup)
# create the backup folder from the stamp
backup = backup + "/" + stamp
dir.make_dir(backup)
backup = backup + "/"
# cycle through the backuplist
for path in backuplist:
# save the files
if dir.dir_exists(mainsorce + path):
# backup all the files in the path
dir_backup(path)
func dir_backup(path):
# backup all the files in the path
var dir_in
dir_in = DirAccess.open(mainsorce + path)
var dir_out = DirAccess.open(backup)
#make the destionation path
if path != "":
dir_out.make_dir(backup + path)
if dir_in:
# loop through the list of files
dir_in.list_dir_begin()
var file_name = dir_in.get_next()
while file_name != "":
if dir_in.current_is_dir():
if path != "":
if !backuplist.has(path+"/"+file_name):
backuplist.append(path + "/" + file_name)
else:
# don't backup the backup folder
if file_name != ".Backup":
if !backuplist.has(file_name):
if !backup_godot_folder:
if file_name != ".godot":
backuplist.append(file_name)
else:
backuplist.append(file_name)
else:
if path != "":
dir_out.copy(mainsorce+path+"/"+file_name, backup + path + "/" + file_name)
else:
dir_out.copy(mainsorce+path+"/"+file_name, backup + file_name)
file_name = dir_in.get_next()
pass
else:
print("An error occurred when trying to access the path.")
func stamp():
var version = ""
# scan through the nodes to find version
for node in get_node("/root").get_children(): #get_node("/root"):
if "version" in node:
version = node.version
# Stamp gets the system time and date
var stamp = Time.get_datetime_dict_from_system(false)
var filestamp = version + " - " + var_to_str(stamp["year"])
if stamp["month"]<10:
filestamp += "0"
filestamp += var_to_str(stamp["month"])
if stamp["day"]<10:
filestamp += "0"
filestamp += var_to_str(stamp["day"]) + "-"
if stamp["hour"]<10:
filestamp += "0"
filestamp += var_to_str(stamp["hour"])
if stamp["minute"]<10:
filestamp += "0"
filestamp += var_to_str(stamp["minute"])
if stamp["second"]<10:
filestamp += "0"
filestamp += var_to_str(stamp["second"])
return filestamp
How to use:
Version 0.04
quick and easy setup
1. just add it to autoload
2. You must include var version = "0.00" at the top of your main tree node.
Auto backup will save a copy all your folders except the .godot folder.
for two main reasons. one, this can consume a lot more space on your drive.
two. it can slowdown your project startup. and three. several files have vary
long names. This can push the limit of the path+name past the 256 size limit and
cause other issues.
you can tell auto backup to backup the .godot folder by simply setting
backup_godot_folder to true.
Advanced setup:
1. just add it to autoload
2. You must include var version = "0.00" at the top of your main tree node
3. Place the list of paths in the backup list located above the func _process()
Note: Paths may be case sensitive depending on the operating system.
Any subpaths need to the added after the path is created
this means, you need to have the path placed first. then
the sub path. example: "Scenes" must be placed before you
can backup path inside scenes.
ie..
var backuplist = [
"",
"Images",
"models",
"Scenes",
"Scenes/Cargo",
"Scenes/minerals",
"Scenes/Player FPS",
"Scenes/scanners",
"Scenes/Tools",
"Scenes/Trade",
"Scripts"
make sure the main tree has the variable version. This is how
auto save knows what version it is saving
ie.. var version = "1.02"
setting copy_subpaths to true will copy all the files in all the paths.
Except .godot and .Backup
setting backup_godot_folder to true will allow autobackup to copy the
.godot folder.
The version number is not required but will make finding changes easier.
The format is optional as long as it can be used in the file system.
meaning, you can not use < > : " / \ | ? * for a windows system.
This is because, Auto backup uses your version as a base for the extended
version number when backing up your files. Also note that when saving
to an online location. You may not be able to use several other Characters
such as % { } ! ' ` @ + =. Some people use a . or a - or even _. ie. 0.00,
0-00, or 0_00. The output folder will be saves as
your version format - date yyyymmdd-time hhmmss.
0.00 - 20240223-044500.
Comments
Post a Comment