Skip to content

Instantly share code, notes, and snippets.

@lrhazi
Last active August 14, 2022 12:36
Show Gist options
  • Save lrhazi/2229dccb2d0fcc3344589e02385b6e3a to your computer and use it in GitHub Desktop.
Save lrhazi/2229dccb2d0fcc3344589e02385b6e3a to your computer and use it in GitHub Desktop.
Copy Google Workspace Contacts from one account to another
#!/bin/bash
shopt -s expand_aliases
alias gam='/root/bin/gamadv-xtd3/gam'
if [[ $# -ne 2 ]]; then
echo "Usage: $0 user-src@dom.ain user-target@dom.ain" >&2
exit 2
fi
USER_SRC=$1
USER_DST=$2
[[ -d /data ]] || mkdir /data
CONTACTS_FN_SRC="/data/${USER_SRC}-contacts.csv"
CONTACTS_FN_DST="/data/${USER_DST}-contacts.csv"
CONTACTGROUPS_FN_SRC_CSV="/data/${USER_SRC}-contactgroups.csv"
CONTACTGROUPS_FN_SRC_JSON="/data/${USER_SRC}-contactgroups-json.csv"
CONTACTGROUPS_FN_DST="/data/${USER_DST}-contactgroups.csv"
# gam config verify
gam config api_calls_rate_check true save
# Export all contacts, but exclude "Other contacts"
gam redirect csv $CONTACTS_FN_SRC user $USER_SRC print contacts selectmaincontacts allfields formatjson quotechar "'"
# gam config csv_output_row_drop_filter "resourceName:regex:otherContacts" \
# redirect csv $CONTACTS_FN_SRC \
# user $USER_SRC print contacts allfields formatjson quotechar "'"
# Export list of user defined contact groups (or lables)
gam user $USER_SRC print contactgroups > $CONTACTGROUPS_FN_SRC_CSV
gam config csv_output_row_filter "groupType:regex:USER_CONTACT_GROUP" \
redirect csv $CONTACTGROUPS_FN_SRC_JSON \
user $USER_SRC print contactgroups quotechar "'" formatjson
# Create contact groups (or lables) in $USER_DST
gam csv $CONTACTGROUPS_FN_SRC_JSON quotechar "'" gam user $USER_DST create contactgroup json "~JSON"
# Export newly created contact groups
gam user $USER_DST print contactgroups > $CONTACTGROUPS_FN_DST
# Correct the contacts gourps in contacts file
python prep-contacts.py $CONTACTGROUPS_FN_SRC_CSV $CONTACTGROUPS_FN_DST $CONTACTS_FN_SRC $CONTACTS_FN_DST
# Import contacts into $USER_DST
gam csv $CONTACTS_FN_DST quotechar "'" gam user $USER_DST create contact json "~JSON"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment