2023-11-18 22:54:25

This commit is contained in:
z17CX 2023-11-18 22:54:25 +00:00
parent 19ad69399f
commit e8d4c7b849
Signed by: z17cx
GPG Key ID: 3F5F87C84EE943E4
11 changed files with 417 additions and 2 deletions

0
CHANGELOG.md Normal file
View File

14
CONTRIBUTING.md Normal file
View File

@ -0,0 +1,14 @@
# Contributing
- Feedback, wishes and suggestions can be sent by email.
- Constructive criticism, bug descriptions and other reports are welcome.
- Email: [mail@ihub.to](mailto:mail@ihub.to).
## Sources
- [**GitHub**](https://github.com/pkgstore)
- [GitLab](https://gitlab.com/pkgstore) (MIRROR)
- [Codeberg](https://codeberg.org/pkgstore) (MIRROR)
- [Disroot](https://git.disroot.org/pkgstore) (MIRROR)
- [MosHub](https://hub.mos.ru/pkgstore) (MIRROR)
- [Git.Org.Ru](https://git.org.ru/pkgstore) (MIRROR)

View File

@ -1,6 +1,6 @@
MIT License
Copyright (c) 2023 Package Store
Copyright (c) 2023 iHub.TO <https://ihub.to>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

132
PkgStore.Vault.psd1 Normal file
View File

@ -0,0 +1,132 @@
#
# Module manifest for module 'PkgStore.Vault'
#
# Generated by: Kitsune Solar
#
# Generated on: 19.11.2023
#
@{
# Script module or binary module file associated with this manifest.
RootModule = 'PkgStore.Vault.psm1'
# Version number of this module.
ModuleVersion = '0.1.0'
# Supported PSEditions
# CompatiblePSEditions = @()
# ID used to uniquely identify this module
GUID = '8fd0ce2c-0288-4d9c-805f-703a0c659ade'
# Author of this module
Author = 'Kitsune Solar'
# Company or vendor of this module
CompanyName = 'iHub.TO'
# Copyright statement for this module
Copyright = '(c) 2023 Kitsune Solar. All rights reserved.'
# Description of the functionality provided by this module
Description = 'The script moves files to Vault based on various criteria.'
# Minimum version of the PowerShell engine required by this module
PowerShellVersion = '7.2'
# Name of the PowerShell host required by this module
# PowerShellHostName = ''
# Minimum version of the PowerShell host required by this module
# PowerShellHostVersion = ''
# Minimum version of Microsoft .NET Framework required by this module. This prerequisite is valid for the PowerShell Desktop edition only.
# DotNetFrameworkVersion = ''
# Minimum version of the common language runtime (CLR) required by this module. This prerequisite is valid for the PowerShell Desktop edition only.
# ClrVersion = ''
# Processor architecture (None, X86, Amd64) required by this module
# ProcessorArchitecture = ''
# Modules that must be imported into the global environment prior to importing this module
RequiredModules = @('PkgStore.Kernel', 'PkgStore.7z')
# Assemblies that must be loaded prior to importing this module
# RequiredAssemblies = @()
# Script files (.ps1) that are run in the caller's environment prior to importing this module.
# ScriptsToProcess = @()
# Type files (.ps1xml) to be loaded when importing this module
# TypesToProcess = @()
# Format files (.ps1xml) to be loaded when importing this module
# FormatsToProcess = @()
# Modules to import as nested modules of the module specified in RootModule/ModuleToProcess
# NestedModules = @()
# Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export.
FunctionsToExport = 'Start-Vault'
# Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export.
CmdletsToExport = @()
# Variables to export from this module
# VariablesToExport = @()
# Aliases to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no aliases to export.
AliasesToExport = @()
# DSC resources to export from this module
# DscResourcesToExport = @()
# List of all modules packaged with this module
# ModuleList = @()
# List of all files packaged with this module
# FileList = @()
# Private data to pass to the module specified in RootModule/ModuleToProcess. This may also contain a PSData hashtable with additional module metadata used by PowerShell.
PrivateData = @{
PSData = @{
# Tags applied to this module. These help with module discovery in online galleries.
Tags = 'pwsh', 'vault'
# A URL to the license for this module.
LicenseUri = 'https://github.com/pkgstore/pwsh-vault/blob/main/LICENSE'
# A URL to the main website for this project.
ProjectUri = 'https://github.com/pkgstore/pwsh-vault'
# A URL to an icon representing this module.
# IconUri = ''
# ReleaseNotes of this module
# ReleaseNotes = ''
# Prerelease string of this module
# Prerelease = ''
# Flag to indicate whether the module requires explicit user acceptance for install/update/save
# RequireLicenseAcceptance = $false
# External dependent modules of this module
# ExternalModuleDependencies = @()
} # End of PSData hashtable
} # End of PrivateData hashtable
# HelpInfo URI of this module
# HelpInfoURI = ''
# Default prefix for commands exported from this module. Override the default prefix using Import-Module -Prefix.
# DefaultCommandPrefix = ''
}

51
PkgStore.Vault.psm1 Normal file
View File

@ -0,0 +1,51 @@
$ModuleManifest = (Get-ChildItem -Path $PSScriptRoot | Where-Object {$_.Extension -eq '.psd1'})
$CurrentManifest = (Test-ModuleManifest $ModuleManifest)
$Aliases = @()
$PrivateFunctions = (Get-ChildItem -Path (Join-Path $PSScriptRoot 'Private') | Where-Object {$_.Extension -eq '.ps1'})
$PublicFunctions = (Get-ChildItem -Path (Join-Path $PSScriptRoot 'Public') | Where-Object {$_.Extension -eq '.ps1'})
(@($PrivateFunctions) + @($PublicFunctions)) | ForEach-Object {
try {
Write-Verbose "Loading: '$($_.FullName)'."
. $_.FullName
} catch {
$_.Exception.Message | Write-Warning
}
}
@($PublicFunctions) | ForEach-Object {
$Alias = (Get-Alias -Definition $_.BaseName -ErrorAction 'SilentlyContinue')
if ($Alias) {
$Aliases += $Alias
Export-ModuleMember -Function $_.BaseName -Alias $Alias
} else {
Export-ModuleMember -Function $_.BaseName
}
}
$FunctionsAdded = ($PublicFunctions | Where-Object {$_.BaseName -notin $CurrentManifest.ExportedFunctions.Keys})
$FunctionsRemoved = ($CurrentManifest.ExportedFunctions.Keys | Where-Object {$_ -notin $PublicFunctions.BaseName})
$AliasesAdded = ($Aliases | Where-Object {$_ -notin $CurrentManifest.ExportedAliases.Keys})
$AliasesRemoved = ($CurrentManifest.ExportedAliases.Keys | Where-Object {$_ -notin $Aliases})
if ($FunctionsAdded -or $FunctionsRemoved -or $AliasesAdded -or $AliasesRemoved) {
try {
$UpdateModuleManifestParams = @{}
$UpdateModuleManifestParams.Add('Path', $ModuleManifest)
$UpdateModuleManifestParams.Add('ErrorAction', 'Stop')
if ($Aliases.Count -gt 0) {
$UpdateModuleManifestParams.Add('AliasesToExport', $Aliases)
}
if ($PublicFunctions.Count -gt 0) {
$UpdateModuleManifestParams.Add('FunctionsToExport', $PublicFunctions.BaseName)
}
Update-ModuleManifest @UpdateModuleManifestParams
} catch {
$_ | Write-Error
}
}

View File

@ -0,0 +1,11 @@
function Compress-VaultData() {
param (
[Alias('P')][string]$Path,
[Alias('N')][string]$Name,
[Alias('O')][switch]$Overwrite
)
if (-not $Overwrite -and (Test-Data -T 'F' -P "${Path}")) {
Compress-7z -T '7z' -L 9 -I "${Path}" -O "${Name}"
}
}

View File

@ -0,0 +1,58 @@
function Start-VaultMoveFiles() {
param (
[string]$Mode,
[string]$Source,
[string]$Vault,
[long]$CreationTime,
[long]$LastWriteTime,
[string]$FileSize,
[string]$Exclude,
[switch]$Overwrite
)
$UTS = "$([DateTimeOffset]::Now.ToUnixTimeSeconds())"
$NL = [Environment]::NewLine
Write-Msg -T 'HL' -M 'Moving Files to Vault'
$Files = ((Get-ChildItem -LiteralPath "${Source}" -Recurse -File -Exclude (Get-Content "${Exclude}"))
| Where-Object { (($_.CreationTime) -lt ((Get-Date).AddSeconds(-$CreationTime))) `
-and (($_.LastWriteTime) -lt ((Get-Date).AddSeconds(-$LastWriteTime))) }
| Where-Object { ($_.Length) -ge "${FileSize}" })
if (-not $Files) { Write-Msg -M "Required files were not found in the '${Source}'!" }
$Files | ForEach-Object {
$File = $_
if (($File.FullName.Length) -ge 245) {
Write-Msg -T 'W' -M "Over 250 characters in path! Skip:${NL}'${File}'"
continue
}
$PathSRC = @("$($File.FullName)")
$PathDST = @("$($File.FullName)", "$($File.DirectoryName)").ForEach({ $_.Remove(0, $Source.Length) })
$PathDST[0] = (${Vault} | Join-Path -ChildPath "$($PathDST[0])")
switch ($Mode) {
'CP' {
New-Data -T 'D' -P "${Vault}" -N "$($PathDST[1])" | Out-Null
Compress-VaultData -P "$($PathDST[0])" -N "$($PathDST[0]).${UTS}.7z" -O=$Overwrite
Write-Msg -M "[CP] '$($PathSRC[0])' -> '$($PathDST[0])'"
Copy-Data -S "$($PathSRC[0])" -D "$($PathDST[0])"
}
'MV' {
New-Data -T 'D' -P "${Vault}" -N "$($PathDST[1])" | Out-Null
Compress-VaultData -P "$($PathDST[0])" -N "$($PathDST[0]).${UTS}.7z" -O=$Overwrite
Write-Msg -M "[MV] '$($PathSRC[0])' -> '$($PathDST[0])'"
Move-Data -S "$($PathSRC[0])" -D "$($PathDST[0])"
}
'RM' {
Write-Msg -M "[RM] '$($PathSRC[0])'"
Remove-Data -p "$($PathSRC[0])"
}
}
}
}

View File

@ -0,0 +1,26 @@
function Start-VaultRemoveDirs() {
param (
[string]$Source,
[long]$CreationTime,
[long]$LastWriteTime,
[string]$Exclude
)
Write-Msg -T 'HL' -M 'Removing Empty Directories'
do {
$Dirs = ((Get-ChildItem -LiteralPath "${Source}" -Recurse -Directory)
| Where-Object { (($_.CreationTime) -lt ((Get-Date).AddSeconds(-$CreationTime))) `
-and (($_.LastWriteTime) -lt ((Get-Date).AddSeconds(-$LastWriteTime))) }
| Where-Object { ((Get-ChildItem $_.FullName -Force).Count) -eq 0 }
| Select-Object -ExpandProperty 'FullName')
if (-not $Dirs) { Write-Msg -M "No empty directories were found in the '${Source}'!" }
$Dirs | ForEach-Object {
$Dir = $_
Write-Msg -M "[RM] '${Dir}'"
Remove-Data -P "${Dir}"
}
} while ( $Dirs.Count -gt 0 )
}

54
Public/Start-Vault.ps1 Normal file
View File

@ -0,0 +1,54 @@
function Start-Vault {
Param(
[Parameter(HelpMessage="Script operation mode. Default: 'MV'.")]
[ValidateSet('CP', 'MV', 'RM')]
[Alias('M')][string]$Mode = 'MV',
[Parameter(Mandatory, HelpMessage="Path to the source. E.g.: 'C:\Data\Source'.")]
[Alias('SRC')][string]$Source,
[Parameter(Mandatory, HelpMessage="Path to the Vault. E.g.: 'C:\Data\Vault'.")]
[Alias('DST')][string]$Vault,
[Parameter(HelpMessage="Time since file creation (in seconds). E.g.: '5270400'. Default: 61 day (5270400 sec.).")]
[Alias('CT')][long]$CreationTime = 5270400,
[Parameter(HelpMessage="Time since file modification (in seconds). E.g.: '5270400'. Default: 61 day ('5270400' sec.).")]
[Alias('WT')][long]$LastWriteTime = $CreationTime,
[Parameter(HelpMessage="File size check. E.g.: '5kb' / '12mb'. Default: '0kb'.")]
[Alias('FS')][string]$FileSize = '0kb',
[Parameter(HelpMessage="Path to the file with exceptions. E.g.: 'C:\Data\exclude.txt'.")]
[Alias('E')][string]$Exclude = "$(Join-Path (Split-Path $PSScriptRoot -Parent) 'Vault.Exclude.txt')",
[Parameter(HelpMessage="Path to the directory with logs. E.g.: 'C:\Data\Logs'.")]
[Alias('L')][string]$Logs = "$(Join-Path (Split-Path $PSScriptRoot -Parent) 'Logs')",
[Parameter(HelpMessage="Removing empty directories.")]
[Alias('RD')][switch]$RemoveDirs = $false,
[Parameter(HelpMessage="Overwrite existing files in Vault.")]
[Alias('O')][switch]$Overwrite = $false
)
$TS = "$(Get-Date -Format 'yyyy-MM-dd.HH-mm-ss')"
Start-Transcript -LiteralPath "${Logs}\$((Get-Date).Year)\$((Get-Date).Month)\${TS}.log"
$MoveFiles = @{
Mode = $Mode
Source = $Source
Vault = $Vault
CreationTime = $CreationTime
LastWriteTime = $LastWriteTime
FileSize = $FileSize
Exclude = $Exclude
Overwrite = $Overwrite
}
Start-VaultMoveFiles @MoveFiles
if ($RemoveDirs) { Start-VaultRemoveDirs }
Stop-Transcript
}

View File

@ -1 +1,68 @@
# pwsh-vault
# PowerShell Vault Module
Скрипт для переноса и сохранения файлов в Vault (директорию хранилища), с сохранением структуры исходной директории. Может на входе принимать различные параметры фильтрации исходных файлов.
## Параметры
- `M` | `Mode`
Режим работы скрипта. В этом параметры можно указать режим работы скрипта. По умолчанию: `MV`.
- `CP` - копировать файлы в Vault.
- `MV` - перемещать файлы в Vault.
- `RM` - удалять файлы в источнике без перемещения в Vault.
- `SRC` | `Source`
Путь к исходной директории (источнику). По умолчанию: `$($PSScriptRoot)\Source`.
- `DST` | `Destination` | `Vault`
Путь к директории назначения (Vault). По умолчанию: `$($PSScriptRoot)\Vault`.
- `CT` | `CreationTime` | `Create`
Время создания файла (в секундах). По умолчанию: `5270400` (61 день и более).
- `WT` | `LastWriteTime` | `Modify`
Время модификации файла (в секундах). По умолчанию: `5270400` (61 день и более).
- `FS` | `FileSize` | `Size`
Исходный размер файла. Файлы соответствующего размера и более, будут добавлены в фильтр для подготовки к переносу. По умолчанию: `0kb` и более.
- `*kb` - размер в килобайтах.
- `*mb` - размер в мегабайтах.
- `*gb` - размер в гигабайтах.
- `*tb` - размер в терабайтах.
- `*pb` - размер в петабайтах.
- `FE` | `Exclude`
Путь и название файла с исключениями. По умолчанию: `$($PSScriptRoot)\Vault.Exclude.txt`.
- `LOG` | `Logs`
Путь к директории с журналами выполнения скрипта. По умолчанию: `$($PSScriptRoot)\Logs`.
- `OW` | `Overwrite`
Перезаписывать файлы в Vault. Файлы с одинаковыми именами в Vault при копировании или перемещении из источника будут перезаписаны. Если параметр не указан, файлы перед перезаписью будут архивированы с меткой времени выполнения скрипта. По умолчанию: `false`.
## Синтаксис
```powershell
.\Vault.ps1 -SRC 'C:\Data' -DST 'C:\Vault'
```
- Сканировать директорию `C:\Data` на файлы.
- Время создания и изменения 61 день (и более).
- Переместить отобранные файлы в директорию `C:\Vault` с сохранением исходной структуры.
```powershell
.\Vault.ps1 -SRC 'C:\Data' -DST 'C:\Vault' -CT '864000' -WT '864000'
```
- Сканировать директорию `C:\Data` на файлы.
- Время создания и изменения 10 дней (и более).
- Переместить отобранные файлы в директорию `C:\Vault` с сохранением исходной структуры.
```powershell
.\Vault.ps1 -SRC 'C:\Data' -DST 'C:\Vault' -CT '864000' -WT '864000' -FS '32mb'
```
- Сканировать директорию `C:\Data` на файлы.
- Время создания и изменения 10 дней (и более).
- Размер 32 мегабайта (и более).
- Переместить отобранные файлы в директорию `C:\Vault` с сохранением исходной структуры.
```powershell
.\Vault.ps1 -SRC 'C:\Data' -DST 'C:\Vault' -CT '864000' -WT '864000' -OW
```
- Сканировать директорию `C:\Data` на файлы.
- Время создания и изменения 10 дней (и более).
- Переместить отобранные файлы в директорию `C:\Vault` с сохранением исходной структуры.
- Не архивировать подобные файлы в Vault, перезаписать файлы с одинаковыми именами.

2
Vault.Exclude.txt Normal file
View File

@ -0,0 +1,2 @@
desktop.ini
Thumbs.db