This project backs up SharePoint files from Microsoft Graph to a local folder using a PowerShell 7 wizard script.
graph-backup.ps1
- App-only authentication with Microsoft Entra app credentials.
- Role and access validation before backup:
Sites.Read.AllFiles.Read.All
- Interactive site selection in terminal (
allor numbered selection). - One-way mirror sync for selected site(s):
- New/changed files are downloaded.
- Local files missing in Microsoft are deleted (only inside selected site roots).
- File compare mode:
name + size + mtime
- Interactive skip during download:
- Press
sto skip current file.
- Press
- Retry handling:
- Graph listing requests retry on temporary failures.
- File downloads retry on temporary failures.
- Safer download writes:
- Downloads are written to
*.parttemp files first. - On success, temp file is moved to final filename.
- On failure/skip, temp file is removed.
- Resume is not enabled; interrupted files are downloaded from start next run.
- Downloads are written to
- Terminal wizard flow:
- Initial loading and token/access validation
- Site selection
- File counting and download
- Final report with rerun/quit prompt
- Quick mode (non-interactive):
-Sitestarts backup immediately without wizard steps.-Folderis optional; if omitted, backup path is taken fromBACKUP_DIR(or default).-DownloadLimitMbpsis optional and overrides env throttling for this run.
- Site listing mode:
-Listprints available site names and exits.
- Help mode:
-Helpprints command usage and exits.
- Download throttling:
- Configurable with
DOWNLOAD_LIMIT_MBPSin env. - Configurable per quick run with
-DownloadLimitMbps. 0means disabled.
- Configurable with
- Input validation:
- Unknown/extra arguments return a clean error and suggest
-Help. -Listcannot be combined with-Site,-Folder, or-DownloadLimitMbps.
- Unknown/extra arguments return a clean error and suggest
- Main menu (default interactive mode):
Run backupUpdate env settingsExit
- Built-in env editor:
- Updates common keys directly in env file from terminal.
- Includes short input-format guide and examples.
- PowerShell 7 (
pwsh) - Microsoft Entra App Registration with:
Sites.Read.All(Application)Files.Read.All(Application)
- Admin consent granted
Copy and edit:
cp graph-login.env.example graph-login.envSupported variables:
TENANT_ID(required)CLIENT_ID(required)CLIENT_SECRET(required)DOWNLOAD_TIMEOUT(optional, default120)DOWNLOAD_LIMIT_MBPS(optional, default0, set max download speed in Mbps)LOG_FILE(optional, default./graph-backup.log)TOKEN_FILE(optional, default.graph_token)BACKUP_DIR(optional, default./sharepoint-backup)SKIP_PATTERNS(optional, comma-separated globs)INCLUDE_PATTERNS(optional, comma-separated globs)INTERACTIVE_SKIP(optional,1or0)
pwsh ./graph-backup.ps1Quick backup (non-interactive):
pwsh ./graph-backup.ps1 -Site Test -Folder D:\backup
pwsh ./graph-backup.ps1 -Site Test
pwsh ./graph-backup.ps1 -Site Test -DownloadLimitMbps 10
pwsh ./graph-backup.ps1 -Site Test -Folder D:\backup -DownloadLimitMbps 0.5List sites:
pwsh ./graph-backup.ps1 -ListHelp:
pwsh ./graph-backup.ps1 -Help- Backup folder:
./sharepoint-backup
- Token files:
./.graph_token./.graph_token.expires_at
- Log file:
./graph-backup.log(orLOG_FILEvalue)
- Site filtering is interactive in the wizard (no
SITE_FILTERenv usage). - Quick mode uses exact site match on
displayName,name, orid. - Skipped files are not counted as downloaded and are attempted again in future runs.
- During download, progress panel is terminal-only and not written to log.
- If backup is interrupted (
Ctrl+C), partially downloaded*.partfiles are not resumed automatically.
- Do not commit
graph-login.env, token files, logs, or backup data. - Rotate
CLIENT_SECRETimmediately if exposed.