├── .gitattributes ├── LICENSE ├── README.md └── ScoopBackup ├── Lib.ps1 ├── Scoop-Export.ps1 └── Scoop-Import.ps1 /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Cologler 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ScoopBackup 2 | 3 | Scoop backup scripts. 4 | 5 | **Note: Powershell Core is required.** 6 | 7 | ## Install 8 | 9 | ``` pwsh 10 | scoop bucket add cologler https://github.com/Cologler/cologler-scoop-bucket 11 | scoop install scoop-backup 12 | ``` 13 | 14 | ## How To Use 15 | 16 | ``` pwsh 17 | # for export 18 | .\ScoopBackup\Scoop-Export.ps1 > scoop.json 19 | 20 | # for import 21 | .\ScoopBackup\Scoop-Import.ps1 scoop.json 22 | ``` 23 | 24 | ### Export 25 | 26 | By default, only user scope packages will be export. 27 | If you want to export with both user scope and global scope packages, you can use `-global` switch: 28 | 29 | ``` pwsh 30 | .\ScoopBackup\Scoop-Export.ps1 -global 31 | ``` 32 | 33 | ### Import 34 | 35 | By default, only user scope packages will be import. 36 | If you want to import global scope packages, you can use `-global` switch: 37 | 38 | ``` pwsh 39 | .\ScoopBackup\Scoop-Import.ps1 scoop.json -global 40 | ``` 41 | 42 | When you use `-global` option, **ONLY** global packages will be import. 43 | -------------------------------------------------------------------------------- /ScoopBackup/Lib.ps1: -------------------------------------------------------------------------------- 1 | function Get-Buckets { 2 | $buckets = @{} 3 | 4 | Get-ChildItem "$(Get-UserScoopDir)\buckets\" | ForEach-Object { 5 | $job = Start-Job -WorkingDirectory $_.FullName -ScriptBlock { 6 | git remote get-url origin 7 | } 8 | $remote = Receive-Job $job -Wait -AutoRemoveJob 9 | $buckets[$_.BaseName] = @{ 10 | BucketName = $_.BaseName 11 | RemoteUrl = [string]::new($remote) # remove ps properties. 12 | } 13 | } 14 | return $buckets 15 | } 16 | 17 | function Get-Apps([string] $appsDir) { 18 | $apps = @{} 19 | Get-ChildItem $appsDir | ForEach-Object { 20 | $current = "$_\current" 21 | $installPath = "$current\install.json" 22 | if (Test-Path $installPath) { 23 | $install = Get-Content -Raw $installPath | ConvertFrom-Json -AsHashTable 24 | $apps[$_.name] = @{ 25 | Name = $_.name 26 | Bucket = $install.bucket 27 | Arch = $install.architecture 28 | Url = $install.url 29 | } 30 | } 31 | } 32 | return $apps 33 | } 34 | 35 | function Get-UserScoopDir { 36 | $baseDir = '~\scoop' 37 | 38 | if ($env:SCOOP -and (Test-Path $env:SCOOP)) { 39 | $baseDir = "$env:SCOOP" 40 | } 41 | 42 | return $baseDir 43 | } 44 | function Get-UserScopeApps { 45 | return Get-Apps "$(Get-UserScoopDir)\apps\" 46 | } 47 | 48 | function Get-GlobalScopeApps { 49 | if ($env:SCOOP_GLOBAL -and (Test-Path $env:SCOOP_GLOBAL)) { 50 | $appsDir = "$env:SCOOP_GLOBAL\apps" 51 | } else { 52 | $appsDir = "$env:ProgramData\scoop\apps\" 53 | } 54 | 55 | $d = Get-Apps $appsDir 56 | $d.Values | ForEach-Object { 57 | $_['Global'] = $true 58 | } 59 | return $d 60 | } 61 | -------------------------------------------------------------------------------- /ScoopBackup/Scoop-Export.ps1: -------------------------------------------------------------------------------- 1 | param( 2 | [switch] $global 3 | ) 4 | 5 | . $PSScriptRoot\Lib.ps1 6 | 7 | function ScoopExport { 8 | $buckets = Get-Buckets 9 | $noBucketApps = @{} 10 | 11 | if ($Script:global) { 12 | $apps = $(Get-GlobalScopeApps).Values + $(Get-UserScopeApps).Values 13 | } else { 14 | $apps = $(Get-UserScopeApps).Values 15 | } 16 | 17 | $apps | ForEach-Object { 18 | $app = $_ 19 | if ($app.Bucket) { 20 | if ($buckets.ContainsKey($app.Bucket)) { 21 | # if user remove bucket, unable to backup 22 | $bucket = $buckets[$app.Bucket] 23 | if (!$bucket.ContainsKey("Apps")) { 24 | $bucket["Apps"] = @{} 25 | } 26 | $entry = @{ 27 | Arch = $app.Arch 28 | } 29 | if ($app.Global) { 30 | $entry.Global = $true 31 | } 32 | # local package should override global package: 33 | $bucket.Apps[$app.Name] = $entry 34 | } 35 | } elseif ($app.Url) { 36 | $entry = @{ 37 | Url = $app.Url 38 | Arch = $app.Arch 39 | } 40 | if ($app.Global) { 41 | $entry.Global = $true 42 | } 43 | # local package should override global package: 44 | $noBucketApps[$app.Name] = $entry 45 | } 46 | } 47 | 48 | $rv = @{ 49 | Version = 1 50 | Buckets = @{} 51 | } 52 | $buckets.Values | ForEach-Object { 53 | $bucket = $_ 54 | $url = $bucket.RemoteUrl 55 | if (!$rv.Buckets.ContainsKey($url)) { 56 | $rv.Buckets[$url] = $bucket 57 | } 58 | } 59 | if ($noBucketApps.Count -gt 0) { 60 | $rv.NoBucketApps = $noBucketApps 61 | } 62 | 63 | return $rv | ConvertTo-Json -Depth 10 64 | } 65 | 66 | $ea = $ErrorActionPreference 67 | $ErrorActionPreference = 'Stop' 68 | try { 69 | ScoopExport 70 | } 71 | finally { 72 | $ErrorActionPreference = $ea 73 | } 74 | -------------------------------------------------------------------------------- /ScoopBackup/Scoop-Import.ps1: -------------------------------------------------------------------------------- 1 | using namespace System 2 | using namespace System.Collections.Generic 3 | 4 | param( 5 | [string] $file, 6 | [parameter(ValueFromPipeline=$true)][string] $plainText, 7 | [switch] $global 8 | ) 9 | 10 | . $PSScriptRoot\Lib.ps1 11 | 12 | $Script:HighPriorityApps = [HashSet[string]]::new([string[]] @( 13 | 'git' 14 | '7zip' 15 | 'sudo' 16 | 'innounp' 17 | 'dark' 18 | 'lessmsi' 19 | ), [System.StringComparer]::OrdinalIgnoreCase) 20 | 21 | function ImportBuckets { 22 | param ( 23 | [hashtable] $bucketsToImport # map url to name 24 | ) 25 | 26 | $existsBuckets = Get-Buckets 27 | $bucketsMapUrl2Name = @{} 28 | $existsBuckets.Values | ForEach-Object { 29 | $bucketsMapUrl2Name[$_.RemoteUrl] = $_.BucketName 30 | } 31 | 32 | $bucketsToImport.Keys | ForEach-Object { 33 | $url = $_ 34 | $name = $bucketsToImport[$_] 35 | 36 | if (!$bucketsMapUrl2Name.ContainsKey($url)) { 37 | if ($existsBuckets.ContainsKey($name)) { 38 | throw [System.NotImplementedException]::new("bucket name conflict: $name") 39 | } 40 | scoop bucket add $name $url 41 | $bucketsMapUrl2Name[$url] = $name 42 | } 43 | } 44 | 45 | return $bucketsMapUrl2Name 46 | } 47 | 48 | function ScoopInstall { 49 | Write-Verbose "Installing $args" 50 | scoop install @args 51 | Write-Host "" 52 | } 53 | 54 | function ScoopImportV1([hashtable] $data) { 55 | $bucketsToImport = @{} 56 | $data.Buckets.Values | ForEach-Object { 57 | $bucketsToImport[$_.RemoteUrl] = $_.BucketName 58 | } 59 | 60 | $bucketsMapUrl2Name = ImportBuckets $bucketsToImport 61 | $existsApps = Get-UserScopeApps 62 | 63 | function FilterPackageByScope([string]$name, $app) { 64 | if ($Script:global -eq ([bool] $app.Global)) { 65 | return $true 66 | } else { 67 | Write-Debug "Ignore package by scope not match: $name" 68 | return $false 69 | } 70 | } 71 | 72 | # install high priority apps: 73 | $mainUrl = 'https://github.com/ScoopInstaller/Main' 74 | if ($data.Buckets.ContainsKey($mainUrl)) { 75 | $mainBucket = $data.Buckets[$mainUrl] 76 | $bucketName = $bucketsMapUrl2Name[$mainUrl] 77 | if ($mainBucket.Apps) { 78 | $apps = $mainBucket.Apps 79 | $apps.Keys | 80 | Where-Object { $Script:HighPriorityApps.Contains($_) } | 81 | Where-Object { FilterPackageByScope $_ $apps[$_] } | 82 | Where-Object { !$existsApps.ContainsKey($_) } | 83 | ForEach-Object { 84 | $appName = $_ 85 | if ($apps[$_].Global) { 86 | ScoopInstall "$bucketName/$appName" --arch $apps[$_].Arch --global 87 | } else { 88 | ScoopInstall "$bucketName/$appName" --arch $apps[$_].Arch 89 | } 90 | $existsApps[$appName] = @{} 91 | } 92 | } 93 | } 94 | 95 | $data.Buckets.Values | ForEach-Object { 96 | $bucketName = $bucketsMapUrl2Name[$_.RemoteUrl] 97 | 98 | if ($_.Apps) { 99 | $apps = $_.Apps 100 | $apps.Keys | 101 | Where-Object { FilterPackageByScope $_ $apps[$_] } | 102 | Where-Object { !$existsApps.ContainsKey($_) } | 103 | ForEach-Object { 104 | $appName = $_ 105 | if ($apps[$_].Global) { 106 | ScoopInstall "$bucketName/$appName" --arch $apps[$_].Arch --global 107 | } else { 108 | ScoopInstall "$bucketName/$appName" --arch $apps[$_].Arch 109 | } 110 | $existsApps[$appName] = @{} 111 | } 112 | } 113 | } 114 | 115 | if ($data.NoBucketApps) { 116 | $data.NoBucketApps.Keys | 117 | Where-Object { FilterPackageByScope $_ $data.NoBucketApps[$_] } | 118 | Where-Object { !$existsApps.ContainsKey($_) } | 119 | ForEach-Object { 120 | $appName = $_ 121 | $app = $data.NoBucketApps[$_] 122 | if ($app.Url) { 123 | if ($apps[$_].Global) { 124 | ScoopInstall $app.Url --arch $apps[$_].Arch --global 125 | } else { 126 | ScoopInstall $app.Url --arch $apps[$_].Arch 127 | } 128 | } 129 | } 130 | } 131 | } 132 | 133 | function ScoopImport { 134 | if ($Script:file) { 135 | $plainText = Get-Content -Path $Script:file -Raw 136 | } elseif ($Script:plainText) { 137 | $plainText = $Script:plainText 138 | } else { 139 | throw "Nothing to import." 140 | } 141 | 142 | $data = $plainText | ConvertFrom-Json -AsHashtable 143 | if ($data.Version -eq 1) { 144 | ScoopImportV1 $data 145 | } else { 146 | throw "Unknown version." 147 | } 148 | } 149 | 150 | $ea = $ErrorActionPreference 151 | $ErrorActionPreference = 'Stop' 152 | try { 153 | ScoopImport $plainText 154 | } 155 | finally { 156 | $ErrorActionPreference = $ea 157 | } 158 | --------------------------------------------------------------------------------