Last active
January 12, 2023 20:35
-
-
Save zk-1/5d796c054bea50c65feddcd6ce12d5cb to your computer and use it in GitHub Desktop.
This script will archive a G Suite account's email (as .mbox) and/or Google Drive files to a specified IT Google Drive folder.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# Name: gam-archive.sh | |
# Usage: gam-archive.sh email_prefix [mail|drive|both] | |
# Example: gam-archive.sh jsmith mail | |
# Description: This script will archive a G Suite account's email (as .mbox) and/or Google Drive files to a specified IT Google Drive folder. | |
# Author: Zoë Kelly ([email protected]) | |
# License: MIT | |
# Created: 2020 | |
# Updated: 2020 | |
## Note: Depending on the export size and your upload speed, it could take 10 minutes or longer for each archival. | |
# Configuration: | |
# Your G Suite admin username (email prefix) | |
gam_admin='admin' | |
# Your G Suite domain | |
gam_domain='example.com' | |
# A Vault matter ID. You can create a matter using: gam create vaultmatter "Offboarding - Archives" | |
matter_id='000000-0000-0000-0000-000000000000' | |
# The ID of the Google Drive folder or shared drive where you want to store the archives (extract from URL) | |
drive_archive_folder_id='0000000000000000000000000000000' | |
# How many seconds between export readiness checks | |
export_wait_interval='31' | |
# Delete G Suite account after archiving? | |
delete_user='no' | |
# Where to put error logs, if there are any | |
log_path="$HOME/Desktop" | |
# --- --- --- --- --- --- --- --- --- --- | |
the_user=$1 | |
today_stamp=$(date "+%d-%m-%Y") | |
time_stamp=$(date "+%d-%m-%Y %H:%M") | |
## Specify your GAM binary location here if different from default | |
gam() { | |
"$HOME/bin/gam/gam" "$@" ; | |
} | |
# Returns 0 if number is between two specified numbers | |
# Usage: length_between 55 50 60 | |
length_between() { | |
if [[ ${#1} -ge $2 ]] && [[ ${#1} -le $3 ]]; then return 0; else return 1; fi | |
} | |
# Usage info | |
usage="`printf 'Usage: ' && basename $0 | tr -d '\n' && printf ' email_prefix [mail|drive|both]'`" | |
# If no paramater given, print usage and exit | |
if [ $# -lt 1 ]; then | |
echo "$usage" | |
exit | |
fi | |
# --- --- --- --- --- --- --- --- --- --- | |
# Determine export type from parameter if given, | |
# otherwise ask for type | |
if [[ $# == 2 ]]; then | |
if [[ $2 == 'mail' ]] || [[ $2 == 'drive' ]] || [[ $2 == 'both' ]]; then | |
to_archive=$2 | |
else | |
echo "Invalid choice" | |
return | |
fi | |
else | |
PS3='Choose archive options: ' | |
options3=("Email" "Google Drive" "Both") | |
select opt3 in "${options3[@]}" | |
do | |
case $opt3 in | |
"Email") | |
to_archive='mail' | |
break | |
;; | |
"Google Drive") | |
to_archive='drive' | |
break | |
;; | |
"Both") | |
to_archive='both' | |
break | |
;; | |
esac | |
done | |
fi | |
# Main export function | |
# Takes export type as single parameter | |
do_export () { | |
export_type="$1" | |
# Start the export | |
if [[ $export_type == "mail" ]]; then | |
export_id=$(gam create export matter uid:$matter_id name "GAM - Offboarding Email export for $the_user" corpus mail accounts $the_user@$gam_domain | grep "id: " | cut -d " " -f3) | |
elif [[ $export_type == "drive" ]]; then | |
# Drive export also requires owner terms be specified, otherwise it will include every document the user has access to! | |
export_id=$(gam create export matter uid:$matter_id name "GAM - Offboarding Google Drive export for $the_user" corpus drive accounts $the_user@$gam_domain terms owner:$the_user@$gam_domain | grep "id: " | cut -d " " -f3) | |
fi | |
# Ensure the returned UID is valid | |
if $(length_between $export_id 40 50); then | |
printf '%s' "-- Server-side $export_type export for $the_user in progress.." | |
else | |
echo "-- Invalid export_id, logging error to $log_path/gam-archive-error.log" | |
echo "$time_stamp - Abort - Invalid export_id for $the_user" >> $log_path/gam-archive-error.log | |
exit 1 | |
fi | |
# Check repeatedly until the export is ready | |
export_ready="0" | |
while [[ $export_ready != "COMPLETED" ]]; do | |
printf '%s' ".." | |
sleep $export_wait_interval | |
export_ready=$(gam info export uid:$matter_id uid:$export_id | grep "status: " | cut -d " " -f3) | |
done | |
echo | |
echo "-- Export finished, downloading files.." | |
# Set up the temp folder | |
archive_name="$the_user-$export_type-archive-$today_stamp" | |
mkdir /tmp/$archive_name | |
# Download the export and compress it | |
gam download export uid:$matter_id uid:$export_id targetfolder /tmp/$archive_name &>/dev/null | |
echo "-- Download complete, compressing.." | |
tar -cz -f /tmp/$archive_name.tar.gz -C /tmp/ ./$archive_name | |
rm -r /tmp/$archive_name | |
file_size=$(du -m /tmp/$archive_name.tar.gz | cut -f1) | |
echo "-- Archive is $file_size MB in size" | |
# Uploading to Google Drive | |
echo "-- Uploading the archive to the Google Drive location specified.." | |
echo " (this could take a while)" | |
upload_result=$(gam user $gam_admin add drivefile localfile /tmp/$archive_name.tar.gz parentid $drive_archive_folder_id | cut -d " " -f1) | |
if [[ $upload_result == "Successfully" ]]; then | |
echo "-- File uploaded to Google Drive, deleting temporary file" | |
rm -f /tmp/$archive_name.tar.gz | |
else | |
echo "-- Upload failed, leaving archive in /tmp/, logging error to $log_path/gam-archive-error.log" | |
echo "$time_stamp - Abort - Google Drive upload failed for $the_user" >> $log_path/gam-archive-error.log | |
exit 1 | |
fi | |
} | |
# Proceed with chosen options | |
if [[ $to_archive == 'mail' ]]; then | |
echo "-- Now archiving Email for $the_user" | |
do_export mail | |
elif [[ $to_archive == 'drive' ]]; then | |
echo "-- Now archiving Google Drive files for $the_user" | |
do_export drive | |
elif [[ $to_archive == 'both' ]]; then | |
echo "-- Now archiving Email and Google Drive files for $the_user" | |
do_export mail | |
do_export drive | |
fi | |
# Delete user in G Suite if specified above | |
if [[ $delete_user == "yes" ]]; then | |
echo "-- Deleting G Suite account.." | |
gam delete user "$the_user" &>/dev/null | |
fi | |
echo "-- Archive process complete for $the_user" | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment