What's the best way to achieve this automated backup?


  1. Posts : 271
    Windows 10
       #1

    What's the best way to achieve this automated backup?


    To preface: I've already been doing this via Macrium however it's kinda uncomfortable or not optimal because I have to mount the folder backup as a virtual drive to access its contents.

    So because of this, I've written the following script:

    Code:
    Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass
    
    $Source = "C:\Users\(USERNAME)\Scripts"
    
    
    Import-Module -Name BurntToast
    
    
    function ShowNotification {
        $NotificationMessage = "All files copied from $Source to $Destination"
        New-BurntToastNotification -Text $NotificationMessage 
    }
    
    
    function CopyFiles {
        Write-Host "Copying files from $Source to $Destination"
    
            $sourceFiles = Get-ChildItem -Path $Source -File -Recurse
    
        foreach ($sourceFile in $sourceFiles) {
            $destinationFile = Join-Path -Path $Destination -ChildPath $sourceFile.FullName.Substring($Source.Length + 1)
    
            
            if (!(Test-Path -Path $destinationFile) -or $sourceFile.LastWriteTime -gt (Get-Item -Path $destinationFile).LastWriteTime) {
                Copy-Item -Path $sourceFile.FullName -Destination $destinationFile -Force
                Write-Host "Copied " -NoNewline
                Write-Host "$($sourceFile.FullName)" -ForegroundColor Red
            }
            else {
                Write-Host "Skipped " -NoNewline
                Write-Host "$($sourceFile.FullName)" -ForegroundColor Green
                Write-Host " (already up to date)"
            }
        }
    
        Write-Host "Copying complete."
        ShowNotification  # Display the notification
    }
    
    
    while ($true) {
        # Check if USB drive is connected
        $usbDrive = Get-WmiObject Win32_Volume | Where-Object { $_.DriveType -eq 2 -and $_.Label -eq "USB-C-Drive" }
    
        if ($usbDrive) {
            $Destination = Join-Path -Path $usbDrive.DriveLetter -ChildPath "downloads test mappa"
            Write-Host "USB drive found. Copying files..."
            CopyFiles
        }
        else {
            Write-Host "USB drive not found. Waiting..."
        }
    
    
        
        Start-Sleep -Seconds 10
    }
    Ideally the event trigger would be the plugging of a USB flash drive into the PC but all the articles I have found regarding that seem to make it an even less desirable way to get this working

    Is there a way to do this in a simpler, more comfortable manner? The script works, I'll even refine it if needs be, but I need to make this trigger event more simpler and convenient.
      My Computer


  2. Posts : 43,014
    Win 10 Pro (22H2) (2nd PC is 22H2)
       #2

    FWIW I use Genie Timeline - which 'normally' runs the backup when I plug in a USB drive.

    Note that sometimes have to start the backup manually as for whatever reason it isn''t always triggered by plugging the disk in.

    That experience may be one you have to grapple with.

    Yes, I do know this is NOT an answer regarding your script, nor is it intended to be
      My Computers


 

  Related Discussions
Our Sites
Site Links
About Us
Windows 10 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 06:15.
Find Us




Windows 10 Forums