Google Drive Script help, user permissions I think

wallis_isaac
New Contributor II

Hey all,

I'm trying to set up a policy to a) move users home directory folders to Drive, then symlink so ~/Documents/ redirects to ~/Google Drive/Documents if ~/Google Drive/ exists, or b) Open /Applications/Google Drive.app if the folder doesn't exist

#!/bin/bash
# Get logged in user
loggedInUser=`/bin/ls -l /dev/console | /usr/bin/awk '{ print $3 }'`
echo $loggedInUser
driveHome="$HOME/Google Drive/"
sudo chflags -R nouchg $HOME
echo $driveHome
#check of drive folder exists
#if yes, do all teh copying and linking
if [[ -d $driveHome ]] ; then
    echo "Drive Exists"
else
    #if no, then open the app
    echo "Drive doesn't exist"
    open /Applications/Google Drive.app
fi

I've removed all the folder work at the moment, because it keeps outputting "Drive doesnt exist" and opening the drive app

Are there any ideas/solutions for this?

Cheers

9 REPLIES 9

wallis_isaac
New Contributor II

I've checked other discussions, which is where the $currentloggedinuser and $HOME bits came from, but I couldn't see any mention of this problem there

stevewood
Honored Contributor II
Honored Contributor II

@wallis.isaac I would probably try grabbing the home folder location in a different method. Rather, I would use dscl to discern the user's home folder location:

myUserHome=$(dscl . read /Users/$loggedInUser NFSHomeDirectory | awk '{print $NF}')

The driveHome becomes:

driveHome="$myUserHome/Google Drive"

Also, since you are quoting the path you do not need the backslash after Google. Notice the bit above does not have it. Using this simple test script I found the folder in my home folder:

#!/bin/bash

loggedInUser=`/bin/ls -l /dev/console | /usr/bin/awk '{ print $3 }'`
myUserHome=$(dscl . read /Users/$loggedInUser NFSHomeDirectory | awk '{print $NF}')

driveHome="$myUserHome/Google Drive"

if [[ -d $driveHome ]]; then
    echo "It's there"
fi

HTH

wallis_isaac
New Contributor II

That did work, but having some problems with slashes at the moment.

I'm more familiar with Python, so I'd like to try to implement that, but the script isn't running (Self Service just has "Gathering Information" and never loads the scripts

#!/usr/bin/python
import subprocess
## Get logged in user
user = subprocess.check_output("/bin/ls -l /dev/console | /usr/bin/awk '{ print $3 }'", shell=True)
print(user)

Thoughts?

stevewood
Honored Contributor II
Honored Contributor II

@wallis.isaac well, if you want to use Python instead of Bash...

#!/usr/bin/python

from SystemConfiguration import SCDynamicStoreCopyConsoleUser
import sys

username = (SCDynamicStoreCopyConsoleUser(None, None, None) or [None])[0]
username = [username,""][username in [u"loginwindow", None, u""]]
sys.stdout.write(username + "
")

That will get you the user name. And I'm assuming you can handle the rest... If not, let me know and I'll try to write it up for you.

wallis_isaac
New Contributor II

Cheers for that, I think I've almost got it

Only problem at the moment is the Desktop folder doesn't play nice

This is the code

#!/usr/bin/python
#import everything 
from SystemConfiguration import SCDynamicStoreCopyConsoleUser
import os
import subprocess
import shutil
import sys
#Get currently logged in user
username = (SCDynamicStoreCopyConsoleUser(None, None, None) or [None])[0]
username = [username,""][username in [u"loginwindow", None, u""]]
#Output it to the log
sys.stdout.write(username + "
")

###VARIABLES
#Directories to sync to Drive
directories = ['Documents', 'Music', 'Desktop', 'Downloads', 'Pictures']
#Users home directory
home = "/Users/" + username + "/"
#Name of the folder in Drive to move things to to
backupFolder = username + "_BSEBackup/"
#backupFolder = ""
#Get list of subdirectories in /Users/username/
list_dir = os.listdir(home)
#Where drive should be located
drive = home + "Google Drive/"
#log the path for drive
sys.stdout.write(drive)
#If drive eists
if os.path.isdir(drive):
    #log that it exists
    sys.stdout.write("Drive Exists"+ "</br >")
    #loop through each subdirectory
    for sub_dir in list_dir:
        #if its in the array of ones to sync across
        if sub_dir in directories:
            #log the subdirectory
            sys.stdout.write("Sub dir is: " + sub_dir + "<br />")
            #Get total path of the folder to move (~/sub_dir)
            dir_to_move = os.path.join(home, sub_dir)
            sys.stdout.write("Dir to move: " + dir_to_move + "</br>")
            #Move that to ~/Google Drive/username_BSEBackup/
            shutil.move(dir_to_move, (drive + backupFolder + sub_dir))
            sys.stdout.write("Shutil command: " + dir_to_move + "   " + (drive + backupFolder) + "</br>")
            #get total path of the drive folder to symlink
            driveFolder = os.path.join(drive + backupFolder, sub_dir)
            sys.stdout.write("Drive Folder: " + driveFolder + "<br />")
            #Create symlink of ~/Google Drive/username_BSEBackup/sub_dir in ~/
            os.symlink(driveFolder, dir_to_move)
            sys.stdout.write("Symlink Drive: " + driveFolder + "  User Location:  " + dir_to_move + "<br />")

else:
    sys.stdout.write("Drive Does Not Exist" + "</br>")

I've got backupFolder set so that if there's already a Documents folder in Drive it won't muck it up

When it's set to anything other than "" (empty string), Desktop won't symlink/copy properly:

The line shutil.move() line seems to affect this (cause it does the moving I guess)

As it is here, shutil.move(dir_to_move, (drive backupFolder sub_dir)), the Desktop folder IS copied, but it's locked (every other folder copies and links fine)

When it's changed to shutil.move(dir_to_move, (drive backupFolder)), and backupFolder is set to username "_BSEBackup", then Desktop is moved somewhere, an empty symlink is created (Points to /Users/username/Desktop), BUT any folders made on the Desktop will show up in the username_BSEBackup/ folder in Drive, and there is no Desktop folder in the backup folder

When backupFolder is empty, and shutil.move() is only drive + backupFolder (effectively just drive), all folders will copy across, symlinks will be made, and it will work perfectly

Are there any special characteristics/behaviours of the desktop folder I'm forgetting?

wallis_isaac
New Contributor II

Those underlined bits got auto formatted, theres meant to be plus signs between variables

stevewood
Honored Contributor II
Honored Contributor II

@wallis.isaac I'll be honest, I exhausted my Python skills with the bit I sent before. :-) Sorry, wish I could help you out on this one.

wallis_isaac
New Contributor II

@stevewood That's alright, you've been a massive help, thank you so much

dmillertds
Contributor

Resurrecting this thread and redirecting a bit, but how have people been fairing with upgrading to "Google Backup and Sync"? I've had a couple users who upgraded without issue, but others who ended up with a broken sync (endless stop/start of the application, sucking up most of the resources). Even after deleting the B&S application and installing Drive from scratch via Google, it would tell me the application was out of date and I needed to upgrade. I had to wipe the Drive profile and reinstall, then resync, to get it to behave.