How to backup Google Photos to Google Drive automatically after July 2019 with Apps Script

Updated on Saturday, September 3, 2022

Comments Page 2

Return to post.

Comments

Robert Ellison

Tim - One more thing. I had an email exchange with John and he suggests checking that you modified both the date code an the section that creates the file in drive. I'll add the full snippet below. This prevents duplicate filenames on the upload to drive.

John's full code

Robert Ellison

Tim - I haven't tried or tested John's modifications. Can you go back to the original script and see if you get any duplicates?

Tim L

Thanks for the reply. All my photos are from my mobile and I have auto-backup to Google Photos switched on.

I cannot make use of Windows as for security reasons, the link between a PC and Google drive is switched off. It's not possible to determine if someone's computer is encrypted like it is with a mobile.

The additional comment said they modified the script to ignore duplicates, which is what I was trying to do. This has to be reasonably straight forward or I'll spend more time deleting photos than the script spends keeping the stuff in sync.

Robert Ellison

Tim - unfortunately yes. There are a couple of ways of handling this. If your photo sources are mobile and a camera you can just add them differently. Mobile photos are added to Google Photos from your phone, allow the script to copy these over to your backup archive and you shouldn't get duplicates. For the camera upload directly to Google Photos and don't copy to your backup location. The script will move these over to drive and again you shouldn't have duplicates.

The other way - which is what I do - is to detect and remove duplicates locally. I allow all my photos to collect in the Google Photos folder in Google Drive and then periodically copy them to the Windows Photos folder with a tool that sorts them into month + year subfolders and skips any duplicates. I released this tool for Windows recently, it's called Photo Sorter. I don't have anything equivalent for Mac or Linux.

Tim L

Thanks, Robert for this blog posting.

I created the above script with John Gillings' modification but I am still getting duplicates showing up.

Do I need to remove something to stop the duplicates being created?

John Gillings

Not really knowing much about Google script, my adjusted date calculations seemed to work, until we rolled over into the new month. Having read some documentation about how dates work, I've changed the start and end dates to this. Hopefully clearer and will survive month (and year?) rollovers? :

var MILLIS_PER_MINUTE = 60000;

var MILLIS_PER_HOUR = MILLIS_PER_MINUTE * 60;

var MILLIS_PER_DAY = MILLIS_PER_HOUR * 24;

var now = new Date();

var from = new Date(now.getTime() - MILLIS_PER_DAY)

var request = {

"pageSize":"100",

"filters": {

"dateFilter": {

"ranges": [

{

"startDate": {

"year": from.getFullYear(),

"month": from.getMonth() + 1,

"day": from.getDate()

},

"endDate": {

"year": now.getFullYear(),

"month": now.getMonth() + 1,

"day": now.getDate()

}

}

]

}

}

Robert Ellison

Logan - I just updated the script to email you if it encounters files larger than 50MB. Not perfect but at least you'll know that there are files you need to download manually.

Google - UrlFetchApp should really exception in this case.

John Gillings

Robert,

Thanks, this is very useful! For my purposes, I want a reasonably frequent update, skipping duplicates instead of making a copy. I adjusted the endDate to

"day": date.getDate() + 1

to include files from today, and the body of the copy loop to

var response = UrlFetchApp.fetch(downloadList[i].baseUrl, {

headers: {

Authorization: 'Bearer ' + photos.getAccessToken()

}

});

var files = folder.getFilesByName(filename);

if (!files.hasNext()) {

var blob = response.getBlob();

blob.setName(filename);

folder.createFile(blob);

}

so duplicates are ignored. My trigger is hourly, which is often enough for what I need. It's almost back to previous behaviour, just a bit lagged.

(I log photos in entries in a Google Calendar work diary - they need to be in Drive to see them from the "Add Attachment" dialog when editing a calendar entry).

Robert Ellison

David - yes, you need space in Google Drive. For any new photos you need quota in both services if you want a copy in both places.

David Katz

Before Google stopped Google Photos and Google Drive sync, this feature created files in Google Drive regardless of the storage space available in the Google Drive account. How does this script handle storage capacity - must there be available space in the Google Drive account?

Add Comment

All comments are moderated. Your email address is used to display a Gravatar and optionally for notification of new comments and to sign up for the newsletter.