3

I have access to a folder shared with me (let's say SharedWithMe folder) and I want to backup this folder as the sharing will be end in the future. So, is it possible to sync / copy the SharedWithMe folder content with / to another folder (let's say MyBackup) in y Google Drive?

2 Answers 2

1

You can not copy shared folders directly, as of yet. For this purpose you'll need to use Google Colaboratory:

  1. Create a new Notebook on Colab
  2. First You'll need to mount your drive as so:
from google.colab import drive
drive.mount('/gdrive')

This asks for a authorisation code.
3. Now, on a separate tab, open your Google Drive and go to Shared with Me. Right click and click 'Add Shortcut to Drive'. This shortcut is temporary and can be deleted later.
4. Type %cd /gdrive/MyDrive/<path-to-the-shortcut> to go to that location and then type pwd to get the path.
5. Execute !cp -r 'above-path/.' '/gdrive/My Drive/<destinantion>'

OR you can avoid all that and simply execute this:

#@title Deeply copy shared folders in Google Drive
from google.colab import drive
import os

print('Mounting Google Drive...')
drive.mount('/gdrive')

src_path = '/gdrive/MyDrive/DE A1' #@param {type: 'string'}
assert os.path.exists(src_path), f"Source '{src_path}' doesn't exist!"

target_path = '/gdrive/MyDrive/Language/German' #@param {type: 'string'}
os.makedirs(target_path, exist_ok=True)
assert os.path.exists(target_path), f"Target '{target_path}' doesn't exist!"

target_path = os.path.join(target_path, os.path.basename(src_path))
print(f'Copying from "{src_path}" to "{target_path}"...')
os.makedirs(target_path, exist_ok=True)
!cp -rf "$src_path"/* "$target_path"  # also work when source is a shortcut
13
  • Thanks a lot for this wonderful explanations, voted up. On the other hand, I have some questions related to this steps >
    – cdor
    Commented Nov 6, 2021 at 17:41
  • 1. Does this script runs periodically or manually?
    – cdor
    Commented Nov 6, 2021 at 17:41
  • 2. As far as I see, this script copies shared folder to one of my Google Drive folder. So, what if I need to synch my folder and copy again the same shared folder to the same destination? In this case will it overwrite the files that has already been copied before?
    – cdor
    Commented Nov 6, 2021 at 17:43
  • By the way I cannot vote up as I have not enough reputation. So, could you pls vote up the question?
    – cdor
    Commented Nov 6, 2021 at 17:45
  • 1
    First of all you need to create shortcut, as told earlier. Lets say, you made the shortcut in the main folder — MyDrive. So now the src_path will be /gdrive/MyDrive/SharedWithMe and target path will be /gdrive/MyDrive/MyBackup Commented Nov 6, 2021 at 18:59
1

The said method from @Saaransh_Garg seems not working now on some sub-folders that is also shared.

However it is still possible to copy a shared file if the path is provided, so instead of relying on the native system call to recursively copy files, following script, which is also supposed to run in colab notebook, list all the files and folders and copy using shutil

from google.colab import drive
import os
import shutil

print('Mounting Google Drive...')
drive.mount('/gdrive')

# List all dirs and sub-dirs
# OS method won't work on links 
def walk_and_copy(root_path, parent_path, target_path, max_depth=3):
    dirs = 0
    copied = 0
    failed = 0
    if max_depth == 0: return (0,0,0)

    print(f"Listing: {root_path}")
    for entry in os.scandir(parent_path):
        subpath = entry.path[len(root_path)+1:]
        target_p = os.path.join(target_path, subpath)

        # DFS
        if entry.is_dir():
           dirs += 1
           os.makedirs(target_p, exist_ok=True)
           d,c,f = walk_and_copy(root_path, entry.path, target_path, max_depth-1)
           dirs += d
           copied += c
           failed += f
        else:
           print(f"copying to {target_p}")
           try:
               shutil.copyfile(entry.path, target_p)
               copied += 1
           except Exception as e:
               print(f"Cannot copy {target_p}, {type(e)}")
               failed += 1
               return (dirs, copied, failed)

src_path = "/gdrive/MyDrive/src_path" #@param {type: 'string'}
assert os.path.exists(src_path), f"Source '{src_path}' doesn't exist!"

target_path = '/gdrive/MyDrive/target_path' #@param {type: 'string'}
os.makedirs(target_path, exist_ok=True) 
assert os.path.exists(target_path), f"Target '{target_path}' doesn't exist!"

target_path = os.path.join(target_path, os.path.basename(src_path))
d, c, f = walk_and_copy(src_path, src_path, target_path, -1)
print(f"dirs listed: {d}, copied files: {c}, failed: {f}")

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .