---
title: "Script Backup for Windows File System to S3 Bucket"
canonical: "https://servicedesk.maxgeo.com/space/MAXGEO/48529409/Script%20Backup%20for%20Windows%20File%20System%20to%20S3%20Bucket"
format: markdown
---
> ⚠️ The following scripts are a work in progress and need to be change to backup multi .bak files and are scheduled using a administrator or backup account.


Calling script

```powershell
#==============================================================================       
#Title           :Backup_call.ps1
#Description     :This script will run compress and copy to s3.
#Author		     :TOSH
#Date            :20200722
#Version         :0.1
#Usage		     :Backup_call.ps1
#Notes           :N/A
#Script_Version  :Powershell v3
#==============================================================================
#cd C:\SysAdmin\Scripts\Backup
$PSScriptRoot
& "$PSScriptRoot\compress.ps1"
& "$PSScriptRoot\S3Copy.ps1"
```

Find and Compress


```powershell
#==============================================================================       
#Title			:compress.ps1
#Description	:This script will compress and encypt *.bak files for off-site backups.
#Author			:TOSH
#Date			:20200624
#Updated		:20200721
#Version		:0.1
#				:0.2    
#Usage			:compress.ps1
#Notes			:N/A
#				:Made changes to source folder and allow to backup all files
#Script_Version	:Powershell v3
#==============================================================================

#----- define Functions -----#

Function Write-Log {
    [CmdletBinding()]
    Param(
    [Parameter(Mandatory=$False)]
    [ValidateSet("INFO","WARN","ERROR","FATAL","DEBUG")]
    [String]
    $Level = "INFO",

    [Parameter(Mandatory=$True)]
    [string]
    $Message,

    [Parameter(Mandatory=$False)]
    [string]
    $logfile
    )

    $Stamp = (Get-Date).toString("yyyy/MM/dd HH:mm:ss")
    $Line = "$Stamp $Level $Message"
    If($logfile) {
        Add-Content $logfile -Value $Line
    }
    Else {
        Write-Output $Line
    }
}

#----- define parameters -----#
# To remove, temp fix toh 20200624
#$sqlMaxG = "D:\\SQL_BACKUPS\Barrick_Bulyanhulu_Database"
#$sqlMaxG = "D:\\SQL_BACKUPS"
# To remove, temp fix toh 20200624

$Zipper = "C:\Program Files\7-Zip\7z.exe"
#----- Remember your password:     MUSIC YELP rope walmart 8'7 FRUIT_FRUIT -----#
$Secret = "MYrw8'7F_F"
#----- get current date ----#
$Now = Get-Date
#----- Create Timestamp -----#
$TimeStamp = (Get-Date).toString("yyyyMMddHHmm")
$DirDate = (Get-Date).toString("yyyyMMdd")
#----- Log File Output -----#
$Logfile = "D:\DATA\logFiles\Admin_logs\Backup_$(gc env:computername)_$TimeStamp.log"
#----- At Rest Storage -----#
$AtRestStorage = "D:\DATA\Scratch\$(gc env:computername)\$DirDate\"
#----- define amount of days ----#
#Orginally $Hours = "0.5"
#$Days = "0.5"
$Days = "1"



#----- define folder where files are located ----#
#$TargetFolder = "D:\DATA\Backups\MSSQL"
$TargetFolder = "D:\SQL_BACKUPS\Barrick_Bulyanhulu_Database"
#----- define extension ----#
# $Extension = "*.log"
$Extension = "*.bak"
#----- define LastWriteTime parameter based on $hous ---#
$LastWrite = $(Get-Date).AddDays(-$Days)
#----- Set State -----#
$SState = "1"
#----- get files based on lastwrite filter and specified folder ---#
#$Files = Get-Childitem $TargetFolder -Include $Extension -Recurse | Where {$_.LastWriteTime -gt "$LastWrite"}
$Files = Get-Childitem $TargetFolder -Recurse | Where {$_.LastWriteTime -gt $LastWrite}
#$Files = Get-Childitem $sqlMaxG -Recurse | Where {$_.LastWriteTime -gt $LastWrite}
#----- PSDebug set 1 -----#
$PSDebug = "0"

if ($PSDebug-eq 1) {
 
#----- define parameters -----#
Write-Host "Zip Prog: " $Zipper 
#----- Perent Folder -----#
Write-Host "Start Here: " $sqlMaxG
#----- Remember your password:     MUSIC YELP rope walmart 8'7 FRUIT_FRUIT -----#
Write-Host "Password: " $Secret 
#----- get current date ----#
Write-Host "Get the current date: " $Now 
#----- Create Timestamp -----#
Write-Host "Current TimeStamp: " $TimeStamp 
Write-Host "Current Date: " $DirDate 
#----- Log File Output -----#
Write-Host "Location of Logfile: " $Logfile 
#----- At Rest Storage -----#
Write-Host "Where at rest: " $AtRestStorage 
#----- define amount of days ----#
Write-Host "Defile numbre of Days: " $Days 
#----- define folder where files are located ----#
# $TargetFolder = "C:\Applications\Logs"
Write-Host "Where files are located: " $TargetFolder 
#----- define extension ----#
# $Extension = "*.log"
Write-Host "Extenstion: "$Extension 
#----- define LastWriteTime parameter based on $Days ---#
Write-Host "LastWrite based on Days: "$LastWrite 
#----- Set State -----#
Write-Host "What is the set state: " $SState
#----- get files based on lastwrite filter and specified folder ---#
Write-Host "List Files: " + $Files 

exit
}

# This is a fix until we sort out location of backuPs and process 24062020 tosh.
#New-Item -type Directory -Force $AtRestStorage
#foreach ($i in Get-ChildItem -Recurse $sqlMaxG)
#    {
#        if ($i.CreationTime -ge ($(Get-Date).AddDays(-1)))
#        {
#            #Write-Host $i
#            Copy-Item $i.FullName $TargetFolder -Force
#        }
#    } 
#


if ($Files)
   {
   Write-Host "Files = " + $Files 
        foreach ($File in $Files) 
            {

            if ($File -ne $NULL)
                {
                if ($SState -eq 0)
                    {
                    Write-Log "INFO" "#===== Start Compression and Encryption of SQLServer Database *.bak =====#" "$Logfile"
                    $SState=1
                    #write-host "Files to be Compressed and Encypted $File" -ForegroundColor "Red"
                    }
                
                # Get the filename from the string
                $Filename = [io.path]::GetFileName($File)
                #write-host "File Name = $Filename" -ForegroundColor "Green"

                # Compress and Encrypt Databases
                Write-Log "INFO" "Compressed and Encypted file - $File" "$Logfile"
                #write-host "Files to be Compressed and Encypted $Filename" -ForegroundColor "Green"                             
                #&$Zipper a $AtRestStorage$Filename.zip $File -p"$Secret" | where {$_ -ne ""} | out-file "$Logfile" -Append -Encoding utf8
				cd  $TargetFolder
				&$Zipper a $AtRestStorage$Filename.zip $File | where {$_ -ne ""} | out-file "$Logfile" -Append -Encoding utf8 
               
                }
            else
                {
                Write-Host "(1) No more files to be Compress and Encypt" -foregroundcolor "Green"
                }
            }
   }
ELSE
   {
   Write-Host "(2) No $Extension files greater than $Days Days" -foregroundcolor "Green"
   }
Write-Log "INFO" "#===== End Compression and Encryption of SQLServer Database *.bak =====#" "$Logfile"
```


Move to S3 Bucket


```
#==============================================================================       
#Title           :S3Copy.ps1
#Description     :This script will copy compressed files to maxgeo S3 Bucket.
#Author		     :TOSH
#Date            :20161003
#Version         :0.2    
#Usage		     :S3Copy.ps1
#Notes           :N/A
#Script_Version  :Powershell v3
#Comments        : Add remove files from log directory v0.2
#==============================================================================

#----- Define Function -----#

Function Write-Log {
    [CmdletBinding()]
    Param(
    [Parameter(Mandatory=$False)]
    [ValidateSet("INFO","WARN","ERROR","FATAL","DEBUG")]
    [String]
    $Level = "INFO",

    [Parameter(Mandatory=$True)]
    [string]
    $Message,

    [Parameter(Mandatory=$False)]
    [string]
    $logfile
    )

    $Stamp = (Get-Date).toString("yyyy/MM/dd HH:mm:ss")
    $Line = "$Stamp $Level $Message"
    If($logfile) {
        Add-Content $logfile -Value $Line
    }
    Else {
        Write-Output $Line
    }
}


#----- Define Parameters -----#


#----- S3 Bucket -----#
$S3Bucket = "s3-af-s-1-servers-bak"
#----- S3 AccessKey -----#
$S3AccessKey = "AKIARPWGOFWOJVNQ5JEX"
#----- S3 SecreKey -----#
$S3Secret = "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"
#----- S3 Region -----#
$S3Region = "af-south-1"
#----- define folder where files are located ----#
$TargetFolder = "D:\DATA\Scratch\$(gc env:computername)"
#----- define extension ----#
$Extension = "*.zip"
#----- Get filename to transfer to S3 Bucket -----#
$results = Get-ChildItem $TargetFolder -Include $Extension -Recurse
#----- Set State -----#
$SState = "1"
#----- Create Timestamp -----#
$TimeStamp = (Get-Date).toString("yyyyMMddHHmm")
$DirDate = (Get-Date).toString("yyyyMMdd")
#----- Server Name -----#
$HName = $(gc env:computername)
#----- Log File Output -----
$LogDir = "D:\DATA\logFiles\Admin_logs\"
#----- Log File Output -----
$Logfile = $LogDir+"S3_Copy_$HName.$TimeStamp.log"
#----- Set Info for Log Removal -----#
$DeleteDaysGreater = "-7"
$CurrentDate = Get-Date
#----- PSDebug set 1 -----#
$PSDebug = "0"

if ($PSDebug-eq 1) {

Write-Host $S3Bucket 
#----- S3 AccessKey -----#
Write-Host $S3AccessKey
#----- S3 SecreKey -----#
Write-Host $S3Secret 
#----- define folder where files are located ----#
Write-Host $TargetFolder 
#----- define extension ----#
Write-Host $Extension 
#----- Get filename to transfer to S3 Bucket -----#
Write-Host $results 
#----- Set State -----#
Write-Host $SState
#----- Create Timestamp -----#
Write-Host $TimeStamp 
Write-Host $DirDate 
#----- Server Name -----#
Write-Host $HName 
#----- Log File Output -----
Write-Host $LogDir
#----- Log File Output -----
Write-Host $Logfile 
exit
}


foreach ($path in $results){

    if ($SState -eq 0){
    Write-LOG "INFO" "#===== Start - Copy zipped files to S3 Bucket {$S3Bucket} =====#" "$Logfile"
    $SState = "1"
    }
    $filename = [System.IO.Path]::GetFileName($path)
    Write-log "INFO" "Coping $filename to s3-af-s-1-servers-bak bucket subfolder $Hostname/$DirDate" "$Logfile" 
    # Write-S3Object -BucketName $S3Bucket  -File $path -Key $DirDate/$filename -CannedACLName Private -AccessKey AKIAJ5E3ANI4KA5MAHXQ -SecretKey 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 | out-file "$Logfile" -Append -Encoding utf8 
    Write-S3Object -BucketName $S3Bucket  -File $path -Key $DirDate/$filename -CannedACLName Private -AccessKey $S3AccessKey -SecretKey $S3Secret -Region $S3Region | out-file "$Logfile" -Append -Encoding utf8 
}
Write-LOG "INFO" "#===== End - Copy zipped files to S3 Bucket {$S3Bucket} =====#" "$Logfile"

#----- Remove compressed backup files -----# 
Write-LOG "INFO" "#===== Start Remove directory $TargetFolder\$DirDate =====#" "$Logfile"
rm $TargetFolder\$DirDate -Recurse -Force | out-file "$Logfile" -Append -Encoding utf8 
Write-LOG "INFO" "#===== End Remove directory $TargetFolder\$DirDate =====#" "$Logfile"
```