Proper editor code for backing up and restoring prefabs

Hi, I try to create an editor script that do the following: backup asset prefab > modify it > restore the original back. My intention is to create a build script that can strip a part of project (e.g. remove some components) to create server version and client version of our game. Below is the code I use but no luck.

[MenuItem("Build/Test")]
    static void Test()
    {
        foreach (string guid in AssetDatabase.FindAssets("t:GameObject"))
        {
            string prefabPath = AssetDatabase.GUIDToAssetPath(guid);
            GameObject go = AssetDatabase.LoadAssetAtPath<GameObject>(prefabPath);
            if (go != null && go.name == "TestPrefab")
            {
                copyFile(prefabPath, "tmp.prefab");
                copyFile(prefabPath+".meta", "tmp.prefab.meta");
               //Make change to prefab
                DestroyImmediate(go.GetComponent<UnityEngine.AI.NavMeshAgent>(), true);
                //Do something...
                copyFile("tmp.prefab", prefabPath);
                copyFile("tmp.prefab.meta", prefabPath+".meta");
                AssetDatabase.Refresh();
                return;
            }
        }

   static void copyFile(string src, string dst)
    {
        if (dst.LastIndexOf('/') >= 0)
        {
            Directory.CreateDirectory(dst.Substring(0, dst.LastIndexOf('/')));
        }
        File.Copy(src, dst, true);
    }

I successfully backup the prefab with above code but I cannot restore it. I can manually restore it by replacing the changed prefab with the backup (when Unity is closed) though. So, I’d like to know how to do this backup and restore properly.

EDIT: I also try AssetDatabase.CopyAsset. But also no luck

Thanks.

You’re reinventing the wheel, just use code versioning software. whatever you are trying to do, CVS does it better and has done so for years. Put them in different branches and you can swap to different branches to test different builds.

Sorry, forgot to mention the reason I’m doing this. I’ll add more info to the first post.
My intention is to create a build script that takes the development version of our project and strip part of it to create 2 separate build: server and client. With versioning software, we’ll have to do stripping every time we make changes to the project which, I think, is really inconvenient.