0
votes

I have enabled sync options on my NAS drive. Every hour the content of the NAS drive is synchronized with my google drive. Is it possible for google drive to send an email when a file on a given day does not appear?

I'm interested in a script that will check - if no file appears within 24 hours - it sends an email.

or

There are 24 files uploaded per day on Google Drive. I would need a script that will send an email if these files are less.

or

If no file is uploaded within 2 hours - send an email.

The best option would be the latest version, as I could keep track of the state of the google drive. The files on google drive are in the "backup database" folder.

1

1 Answers

0
votes

If I understand correctly what you are asking is:

  1. query for file on a folder in Google Drive
  2. check the creation date of all files
  3. if the most recent file was uploaded more than X hours ago send an email

I think you can do everything with Google App script. You need to create a new script file in your Drive account and add inside it this function.

function isBackuped() {
  var delay_h = 0; // or 2 or 24
  var delay_m = 70;
  var current = new Date();
  current.setTime(current.getTime() - (delay_h * 60 * 60  + delay_m * 60) * 1000 );

  var recent = false;

  var folders = DriveApp.getFoldersByName("backup database");
  while (folders.hasNext()) {
    var backup_folder = folders.next();

    var files = backup_folder.getFiles()
    while (files.hasNext()) {
      var backup = files.next();
      var created = backup.getDateCreated();
      if (created > current)
        recent |= true;
    }
  }

  if (!recent) {
    GmailApp.sendEmail(
      "[email protected]", 
      "Mail Subject: Backup issue",
      "Mail Body: There is no recent upload!");
  }
}

Then you have to create a trigger that runs the function automatically every X hours: click on Edit > Current project's triggers and on the link "No triggers set up. Click here to add one now", and set a trigger to run isBackuped based upon time every X hours.

Warning: You will need to allow permission to this script for access your Drive and your Gmail account. It will be asked the first time you run the function, so run it at least once using the play button in the instruments bar.