Skip to content

Commit 8ad93e9

Browse files
committed
Added Invoke-BackupJob for performing full backup
1 parent c57d277 commit 8ad93e9

File tree

7 files changed

+159
-0
lines changed

7 files changed

+159
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
function Invoke-ChannelBackup {
2+
[CmdletBinding()]
3+
param (
4+
[Parameter(Mandatory = $true, Position = 0 )]
5+
[string]$Token,
6+
[Parameter(Mandatory = $true, Position = 1 )]
7+
[string]$ChannelID,
8+
[Parameter(Mandatory = $true, Position = 2 )]
9+
[string]$Location
10+
)
11+
process {
12+
Write-Verbose "Cmdlet Invoke-ChannelBackup - Process"
13+
14+
$messages = @()
15+
$start = Convert-DateToEpoch (Get-Date "2000-01-01")
16+
17+
if (Test-Path $Location) {
18+
$messages = Get-Content -Path $Location -encoding UTF8 | ConvertFrom-Json
19+
$last = $messages | Select-Object -Last 1
20+
$start = $last.ts
21+
}
22+
Write-Verbose "Get-ChannelNewMessages [$ChannelId][$(Convert-EpochToDate $Start)]"
23+
[array]$newMessages = Get-FullHistory -Token $key -ChannelId $ChannelId -Start $start
24+
25+
$ids = $messages.client_msg_id
26+
$threadDeep = Convert-DateToEpoch (Get-Date).AddDays(-90)
27+
$threaded = $messages | ? { $_.reply_count -ne $null } | ? { [double]$_.ts -le $start -and [double]$_.ts -gt $threadDeep }
28+
$threaded | % {
29+
$om = $_
30+
$response = Get-Thread -Token $key -ChannelId $ChannelId -ThreadTs $om.thread_ts
31+
if ($response.ok -eq $true) {
32+
$replies = $response.messages
33+
$nm = $replies | Select-Object -First 1
34+
if ($nm -and ([double]$nm.latest_reply) -gt ([double]$om.latest_reply)) {
35+
Write-Verbose "Update parent message: $($om.client_msg_id)"
36+
$index = $messages.IndexOf($om)
37+
$messages.Item($index) = $nm
38+
39+
$replies | Select-Object -Skip 1 | % {
40+
$m = $_
41+
if ($ids.Contains($m.client_msg_id) -eq $false) {
42+
Write-Verbose "Adding new thread meassage: $($m.client_msg_id)"
43+
$newMessages += $m
44+
}
45+
}
46+
}
47+
}
48+
}
49+
50+
if ($newMessages.Count -gt 0) {
51+
$messages += $newMessages
52+
$messages | Sort-Object -Property @{Expression = { [double]$_.ts } } | ConvertTo-Json -Depth 100 | Set-Content -Path $Location
53+
}
54+
}
55+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
function Invoke-ChannelsBackup {
2+
[CmdletBinding()]
3+
param (
4+
[Parameter(Mandatory = $true, Position = 0 )]
5+
[string]$Token,
6+
[Parameter(Mandatory = $true, Position = 1 )]
7+
[string]$Location
8+
)
9+
process {
10+
Write-Verbose "Cmdlet Invoke-ChannelsBackup - Process"
11+
12+
$backupLoc = "$Location\channels"
13+
Assert-Path $backupLoc
14+
15+
$channels = Get-Channel $Token | Select-Object -ExpandProperty channels
16+
$channels | ConvertTo-Json -Depth 100 | Set-Content -Path "$backupLoc\channels.json"
17+
18+
$channels | % {
19+
$channelBackupPath = "$backupRoot\channels\$($_.id).json"
20+
Invoke-ChannelBackup -Token $Token -ChannelId $_.id -Location $channelBackupPath
21+
}
22+
}
23+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
function Invoke-FilesBackup {
2+
[CmdletBinding()]
3+
param (
4+
[Parameter(Mandatory = $true, Position = 0 )]
5+
[string]$Token,
6+
[Parameter(Mandatory = $true, Position = 1 )]
7+
[string]$Location
8+
)
9+
process {
10+
Write-Verbose "Cmdlet Invoke-FilesBackup - Process"
11+
12+
$backupLoc = "$Location/files"
13+
Assert-Path $backupLoc
14+
15+
$start = Get-ChildItem -Path $backupLoc -Filter *.json |`
16+
% { $_ | Get-Content -Encoding UTF8 | ConvertFrom-Json } |`
17+
Select-Object -ExpandProperty created |`
18+
Measure-Object -Maximum |`
19+
Select-Object -ExpandProperty Maximum |`
20+
% { $_ + 1 }
21+
22+
if (!$start) {
23+
$start = Convert-DateToEpoch (Get-Date "2000-01-01")
24+
}
25+
26+
$files = Get-FilesList -Token $key -TsFrom $start
27+
if ($files.ok) {
28+
foreach ($f in $files | Select-Object -ExpandProperty files) {
29+
$ext = $f.filetype
30+
$path = "$backupLoc/$($f.id)"
31+
Get-SlackFile -Token $key -Uri $f.url_private | Set-Content "$path.$ext" -AsByteStream
32+
$f | ConvertTo-Json -Depth 10 | Set-Content -Path "$path.json"
33+
}
34+
}
35+
}
36+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function Assert-Path {
2+
[CmdletBinding()]
3+
param (
4+
[Parameter(Mandatory = $true, Position = 0 )]
5+
[string]$Path
6+
)
7+
process {
8+
Write-Verbose "Cmdlet Assert-Path - Process"
9+
10+
if (!(Test-Path $Path -PathType Container)) {
11+
New-Item -ItemType Directory -Force -Path $Path | Out-Null
12+
}
13+
}
14+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
function Invoke-BackupJob {
2+
[CmdletBinding()]
3+
param (
4+
[Parameter(Mandatory = $true, Position = 0 )]
5+
[string]$Token,
6+
[Parameter(Mandatory = $false, Position = 1 )]
7+
[string]$Location = ".\backup",
8+
[Parameter(Mandatory = $false, Position = 2 )]
9+
[switch]$Channels,
10+
[Parameter(Mandatory = $false, Position = 3 )]
11+
[switch]$Files
12+
)
13+
14+
begin {
15+
Write-Verbose "Cmdlet Invoke-BackupJob - Begin"
16+
}
17+
18+
process {
19+
Write-Verbose "Cmdlet Invoke-BackupJob - Process"
20+
if ($Files) {
21+
Invoke-FilesBackup -Token $Token -Location $Location
22+
}
23+
if ($Channels) {
24+
Invoke-ChannelsBackup -Token $Token -Location $Location
25+
}
26+
}
27+
28+
end {
29+
Write-Verbose "Cmdlet Invoke-BackupJob - End"
30+
}
31+
}

0 commit comments

Comments
 (0)