Get started with the Google Workspace User Archive script in 5 minutes.
IMPORTANT: This script uses GAM ONLY for read-only user discovery (listing users in the target OU). GAM makes no modifications to your Workspace. Always review the GAM commands in the script to verify read-only operations before running.
IMPORTANT: This script requires pre-installed and configured GAM and GYB. If you haven't set these up yet, stop here and configure them first. See the GAM Installation Guide and GYB Installation Guide.
Before using this script, verify that GAM and GYB are properly installed and configured:
# Check GAM version
gam version
# Verify GAM can access your domain
gam info domain
# Test listing users
gam print users maxResults 5If any of these commands fail, you need to configure GAM first. GAM configuration is typically stored in ~/.gam/.
# Check GYB version
gyb --version
# Test GYB with an estimate (doesn't actually download emails)
gyb --email test.user@yourdomain.com --action estimateIf GYB fails, ensure it's configured with a service account that has domain-wide delegation. GYB uses the same configuration directory as GAM (~/.gam/ by default).
# Check default location
ls -la ~/.gam/
# Should see files like:
# - oauth2.txt (OAuth credentials)
# - oauth2service.json (Service account)
# - client_secrets.json (API credentials)Security Check:
# Ensure proper permissions on GAM config
chmod 700 ~/.gam
chmod 600 ~/.gam/*# Clone the repository
git clone https://github.com/ericgjerde/archiving-gmail-users.git
cd archiving-gmail-users
# Make script executable
chmod +x archive-workspace-users.sh
# Verify script can find GAM and GYB
./archive-workspace-users.sh --helpTest the script without making any changes:
./archive-workspace-users.sh --dry-runThis will:
- Verify GAM and GYB are accessible
- Check for users in the FormerEmployees OU
- Show what would be done (without actually doing it)
- Validate directory structure
If you see errors about GAM or GYB not being configured, return to Step 1.
Before processing all users, test with one account:
./archive-workspace-users.sh --user test.user@yourdomain.comThis verifies:
- GAM can query user information
- GYB can backup the user's email
- Compression works correctly
- Archive is created properly
Monitor the progress:
# In another terminal, watch the log
tail -f logs/archive_*.logOnce the single user test succeeds:
./archive-workspace-users.shThe script will:
- Query GAM for all users in the FormerEmployees OU (read-only operation)
- Display the list of users
- Ask you to type the exact OU path to confirm (security feature)
- Process each user sequentially (with 30s delay between users)
- Generate a comprehensive report
Note: You must type /FormerEmployees exactly - this prevents accidental archival of wrong OUs.
Solution:
# Verify GAM is in PATH
which gam
# Check GAM configuration
gam version
gam info domain
# If using custom config location
export GAMCFGDIR="/path/to/gam-config"
gam versionSolution:
# Verify GYB is in PATH
which gyb
# Test GYB
gyb --version
# Check service account configuration
ls -l ~/.gam/oauth2service.json
# Test with a user
gyb --email test@domain.com --action estimateSolution:
# Verify OU path
gam print orgs
# Check users in the OU
gam print users query "orgUnitPath='/FormerEmployees'"
# Verify GAM authentication
gam oauth infoSolution:
# Check detailed GYB log
tail -50 logs/gyb_*.log
# Verify service account has domain-wide delegation
# Check in Google Cloud Console:
# IAM & Admin > Service Accounts > View delegated scopes
# Test GYB manually
gyb --email test@domain.com --action backup --local-folder /tmp/test-backupIf your GAM config is not in ~/.gam/:
export GAMCFGDIR="/opt/gam-config"
./archive-workspace-users.sh./archive-workspace-users.sh --ou "/SuspendedUsers"./archive-workspace-users.sh --dry-run --ou "/FormerEmployees"- Small mailbox (< 1 GB): 5-15 minutes
- Medium mailbox (1-10 GB): 15-45 minutes
- Large mailbox (> 10 GB): 45-120+ minutes
Ensure at least 2x the size of the largest mailbox is available.
The script automatically waits 30 seconds between users to comply with Google API rate limits. This is normal and expected.
cat reports/archive_report_*.txttail -f logs/archive_*.logls -lh archives/tar -tzf archives/user@domain.com_*.tar.gz > /dev/null && echo "Archive OK"# Verify prerequisites
gam version && gyb --version
# Dry run
./archive-workspace-users.sh --dry-run
# Single user test
./archive-workspace-users.sh --user email@domain.com
# Archive all in FormerEmployees OU
./archive-workspace-users.sh
# Archive specific OU
./archive-workspace-users.sh --ou "/SuspendedUsers"
# Show help
./archive-workspace-users.sh --help
# Monitor progress
tail -f logs/archive_*.log
# Check disk space
df -h .
# Verify GAM config
ls -la ~/.gam/
gam info domainIf something isn't working:
- GAM is installed and in PATH
- GYB is installed and in PATH
-
gam versionworks -
gam info domainshows your domain -
gyb --versionworks -
~/.gam/directory exists with proper permissions (700) -
~/.gam/oauth2service.jsonexists (for GYB) - Service account has domain-wide delegation enabled
- Adequate disk space available
- Stable internet connection
- Review the full README.md for advanced features
- Set up automated backups (cron/systemd)
- Establish a regular review schedule
- Document your backup procedures
- Test archive restoration process
The script includes multiple layers of security and safety:
Input Validation:
- ✅ OU path validation (prevents command injection)
- ✅ Email address format validation
- ✅ Path validation before file deletions
Confirmation & Control:
- ✅ Must type exact OU path to confirm (not just "yes")
- ✅ Single-user mode requires confirmation
- ✅ Dry-run mode for safe testing
- ✅ Can be safely interrupted with Ctrl+C
Operational Safety:
- ✅ GAM used ONLY for read-only operations (querying users in target OU)
- ✅ Uses existing GAM/GYB configuration (doesn't manage credentials)
- ✅ Skips already archived users (resume capability)
- ✅ Continues on individual user failures
- ✅ Detailed audit logging (user, host, timestamp)
- ✅ Automatic cleanup of temporary files
- ✅ Owner-only permissions (700/600)
- Check the error message in the console
- Review the detailed log file in
logs/ - Verify GAM and GYB work independently:
gam print users maxResults 1gyb --email test@domain.com --action estimate
- Consult the README.md troubleshooting section
- Verify all prerequisites are properly configured
- This script does NOT configure GAM or GYB
- GAM and GYB must be working BEFORE running this script
- OAuth credentials are managed by GAM/GYB in
~/.gam/ - Never commit
~/.gam/directory to version control - Keep GAM and GYB updated regularly
- Test restore procedures periodically