DEV Community

Cover image for [Unity] Publish assets that automatically save backups of files.
uni928
uni928

Posted on • Edited on

[Unity] Publish assets that automatically save backups of files.

I created a Unity asset that automatically saves Unity backups.
I would like to release it to you in the hope that it will be useful to you.

Please download ver1.0.0 (latest version) from the following site.(it is free asset)
https://assetstore.unity.com/packages/tools/utilities/sephiroth-auto-back-up-monitoring-290378


Description of this asset

Automatically saves a backup every time a file is updated.
No other operations are required other than importing this asset.

The Assets_BackUP folder is automatically generated in the hierarchy where the Assets folder is located.
Every time a file in the Assets folder is updated, a backup is automatically saved there.

The backup file name includes the update date, so if you want to return to a specific point in time, you can identify it by the file name.

If you delete the Assets_BackUP folder, a new Assets_BackUP folder will be regenerated containing only a backup of the current state.
If the Assets_BackUP folder becomes too large, consider deleting it.

This asset will never overwrite any files in the Assets folder.
This asset only touches the Assets_BackUP folder.
Therefore, it is impossible for this asset to cause bugs.
Please use it with confidence.

Introducing the automatically generated Assets_BackUP folder.
Example of generating a backup.


Ver0.0.0 works with the code below.
Ver1.0.0 also has a function to partially delete backup files that are more than 4 business days old, but because the code is long, we will only post the code for Ver0.0.0.

If you create a new SephirothAutoBackUPMonitoring.cs, replace the code with the code below and put it in the Editor folder, it should work in the same state as ver0.0.0.

using System.Threading.Tasks;

namespace SephirothTools
{
    public class SephirothAutoBackUPMonitoring
    {
        private static readonly object Lock = new();
        private static bool isExec = false;
        private static string DateString;

        public const bool isCSharpFileOnly = false;

        [UnityEditor.InitializeOnLoadMethod]
        private static void Exec()
        {
            lock (Lock)
            {
                if (isExec)
                {
                    return;
                }
                isExec = true;
                Task.Run(() => TaskExec(System.DateTime.Now.ToString("yyyyMMdd-HHmmss")));
            }
        }

        private static void TaskExec(string date)
        {
            DateString = date;
            while (true)
            {
                try
                {
                    ExecOneTime();
                }
                catch
                {

                }
                System.Threading.Thread.Sleep(5000); // A thread for SephirothAutoBackUPMonitoring is generated and put into sleep, so the main thread is not stopped.
                if(date != DateString)
                {
                    break;
                }
            }
        }

        private static void ExecOneTime()
        {
            string targetPath = System.IO.Directory.GetCurrentDirectory() + "\\Assets_BackUP";
            if (!System.IO.Directory.Exists(targetPath))
            {
                System.IO.Directory.CreateDirectory(targetPath);
            }

            foreach (string onePath in System.IO.Directory.GetFiles(System.IO.Directory.GetCurrentDirectory() + "\\Assets", "*", System.IO.SearchOption.AllDirectories))
            {
                if (isCSharpFileOnly && !onePath.EndsWith(".cs"))
                {
                    continue;
                }

                string oneTargetDirectory = System.IO.Directory.GetCurrentDirectory() + "\\Assets_BackUP" + onePath.Substring((System.IO.Directory.GetCurrentDirectory() + "\\Assets").Length);
                oneTargetDirectory = oneTargetDirectory.Substring(0, oneTargetDirectory.LastIndexOf("\\"));

                string oneTargetFileName = System.IO.File.GetLastWriteTime(onePath).ToString("yyyyMMdd-HHmmss") + "_" + System.IO.Path.GetFileName(onePath);

                string target = oneTargetDirectory + "\\" + oneTargetFileName;

                if (!System.IO.File.Exists(target))
                {
                    string[] pathMove = oneTargetDirectory.Split("\\");
                    string folderPath = pathMove[0];

                    for (int i = 1; i < pathMove.Length; i++)
                    {
                        folderPath = folderPath + "\\" + pathMove[i];
                        if (!System.IO.Directory.Exists(folderPath))
                        {
                            System.IO.Directory.CreateDirectory(folderPath);
                        }
                    }

                    System.IO.File.Copy(onePath, target);
                }
            }
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

We have now released an asset that automatically saves Unity backups.

We hope this will be helpful to your development.
Thank you for reading.

Top comments (0)