[Unity] Publish assets that automatically save backups of files.
Tags: asset, backup, auto, unity3d
Published: 2024-07-12 02:09:33

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.1.0 (latest version) from the following site.
https://drive.google.com/file/d/1nnEEoMFRQqypSJAWU4odKbQz4JK86wpr/view?usp=sharing

***

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.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hcjlkdmopf3k1faabd5t.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mqyj865iy4m7tk05b9xy.png)

***

Ver1.0.0 works with the code below.
Ver1.1.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 Ver1.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 ver1.0.0.

```Csharp
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.Today.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);
                }
            }
        }
    }
}
```

***

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

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