将 Google 驱动器目录中的所有文件恢复为旧版本

Revert all files in a Google Drive directory to old version

病毒最近加密了我的所有文件,不幸的是 google 备份和同步立即将新版本的文件(加密)上传到我的驱动器。 我知道我可以将单个文件恢复到以前的版本,但我的驱动器上大约有 30,000 个文件 - 这意味着我无法手动恢复所有这些文件...... 我尝试使用 Apps 脚本,但我无法真正找到解决它的方法。 有人知道吗?

提前感谢任何愿意回答的人:)

*编辑 - 使用 Apps 脚本解决了它! - 答案可以在下面找到

*编辑 - 使用 Apps 脚本解决了它!

在 Ruben 的帮助下,我使用了 Whosebug 中建议的代码。com/q/42402713/1595451(小改动 - 我的代码 运行 遍历了文件夹中的所有文件,包括子文件夹)删除了我的所有版本在病毒攻击当天创建的文件。现在每个文件的版本都是病毒攻击前的版本。

/** A function that delete all version of files in folder and subfolders that were made at the day of the virus attack and afterwards*/
function fixAllFilesInFolder(){ 
  var sh = SpreadsheetApp.getActiveSheet();
  var folderId = #enter folder Id here
  var folder = DriveApp.getFolderById(folderId); // I change the folder ID  here 
  handleFolder(folder, 0)   
}

/** A recursive function - delete all 'bad' versions of files in folder and calls itself with each of the subfolders to do the same*/
function handleFolder(folder, treeRank){
  Logger.log(String(treeRank) + ': ' + folder.getName());
  fixFolderFiles(folder)
  var subFolders = folder.getFolders(); 
  while (subFolders.hasNext()){
    subFolder = subFolders.next();
    handleFolder(subFolder, treeRank + 1)
  }

}

/** Delete all 'bad' versions of files in folder*/
function fixFolderFiles(folder){
  var files = folder.getFiles(); 
  while (files.hasNext()){
    file = files.next();
    deleteRevisions(file)
  }
}

/** Delete 'bad' version of a file*/
function deleteRevisions(file){  
  var fileId = file.getId();  
  var revisions = Drive.Revisions.list(fileId);
  var virusDate = new Date(2021, 3, 30) /** Put your attack date here!*/
  if (revisions.items && revisions.items.length > 1) 
    {    
    for (var i = 0; i < revisions.items.length; i++) 
      {
        if (i > 0){
          var revision = revisions.items[i];      
          var date = new Date(revision.modifiedDate);
          if(date.getTime() > virusDate.getTime()){
            return Drive.Revisions.remove(fileId, revision.id); 
          }
        }       
      }  
    }
  
  }

两个注意事项:1) 不幸的是,Apps 脚本一次只能 运行 约 5 分钟,所以我不得不 运行 代码多次(数十次)从不同的文件夹到获取我所有的文件。 2) 即使您将文件恢复到之前的版本,我的文件名仍然以加密格式“.wrui”结尾。为了修复它,我下载了所有文件和 运行 一个 python 脚本(可能可以在 Apps 脚本中完成,但我对 python 感觉更舒服)更改了所有名称。

from os import rename, listdir, walk
from os.path import join, isfile

ENCRYPTION = 'wrui' #Here should be the encryption format

#A function to rename a file without the encryption format - it makes sure that no file is named with the same name exactly. In that case, it adds '2' to the files name
def renameFile(address, origin_name, new_name):
    final_name = new_name
    
    if isfile(join(address, new_name)):
        name_list = new_name.split('.')
        name_list[-2] += '2'
        final_name = '.'.join(name_list)
        
    rename(join(address, origin_name), join(address, final_name))

#A function to rename all files in a specific folder
def purifyFolderFiles(folder_address):
    for file in listdir(folder_address):
        f_list = file.split('.')
        if len(f_list) > 1:
            if f_list[-1] == ENCRYPTION:
                renameFile(folder_address, file, '.'.join(f_list[:-1]))
                
            elif f_list[-2] == ENCRYPTION:
                if f_list[-3] == f_list[-1]:
                    renameFile(folder_address, file, '.'.join(f_list[:-2]))                
                else:
                    del f_list[-2]
                    renameFile(folder_address, file, '.'.join(f_list))

#This next few lines iterate through all the subfolders of the specific folder you put and use purifyFolderFiles to rename every file there
folder = #enter you folder address here
for path, subdirs, files in walk(folder):
    for name in subdirs:
        subdir_address = join(path, name)
        purifyFolderFiles(subdir_address)

希望有人会觉得这有用:) 非常感谢 Ruben 将我与解决方案联系起来

由于我无法发表评论,因此将其添加为答案。

我在尝试接受答案时学到的一些东西。

  1. 你在new Date(Year, Month, Day)中输入的日期,需要正确格式化。主要问题是月份值的范围是从 0(一月)到 11(十二月),而不是 1-12。
  2. 在您的编码区域中,您需要添加服务“Drive”。为此,请按库部分中的加号按钮,找到 Drive 服务并添加它。然后授予所需的权限。
  3. 文件夹 ID 是浏览器中的 link,而不是文件夹本身的名称。

这就是我需要做的一切才能让它发挥作用。 如果您希望程序告诉您它正在更改哪些文件,请将 deleteRevisions 函数替换为以下代码:

/** Delete 'bad' version of a file*/
function deleteRevisions(file){  
  var fileId = file.getId();  
  var revisions = Drive.Revisions.list(fileId);
  var virusDate = new Date(2021, 09, 05) /** Put your attack date here! Year, Month (0-11), Day*/
  if (revisions.items && revisions.items.length > 1) 
    {
    Logger.log(file);    
    for (var i = 0; i < revisions.items.length; i++) 
      {
        if (i > 0){
          var revision = revisions.items[i];      
          var date = new Date(revision.modifiedDate);
          if(date.getTime() > virusDate.getTime()){
            return Drive.Revisions.remove(fileId, revision.id); 
          }
        }       
      }  
    }
  
  }