Share via


PowerShell script for incremental data (file/folder) backup

Question

Wednesday, September 27, 2017 6:25 AM | 1 vote

Dear Concerned,

I want to incremental backups copies only the files that are modified since the last backup by PowerShell scripts. And it will be run by task scheduler. Please help immediately.

Example: FileBackup c:\source-folder to d:\dest-folder  for 30 days.

Regards

Md. Mohashin Khan

 

All replies (11)

Thursday, September 28, 2017 1:55 AM | 1 vote

Hi Md. Mohashin Khan,

Since this query is more related to PowerShell, I will move this thread into PowerShell forum so that you would get more efficient support.

Best Regards,

Candy

Please remember to mark the replies as answers if they help.
If you have feedback for TechNet Subscriber Support, contact [email protected].


Thursday, September 28, 2017 3:08 AM | 1 vote

Hi Mohashin,

    What's the issue? Did you have a look at Get-ChildiItem cmdlet help? 

To point you in the right direction.. you can use this oneliner:

Get-ChildItem | Where-Object {$_.LastWriteTime -ge $(get-date).AddDays(-30)}

And add -Recurse to Get-ChildItem if needed. Then you can pipe Copy-Item at the end of the oneliner to copy to the target folder. 

I strongly suggest to you to get familiar with the Get-Help cmdlet.

Regards,

Paolo


Thursday, September 28, 2017 3:53 AM

This is not a Windows backup forum.  You need to pursue backups in a Windows user forum.

\(ツ)_/


Thursday, September 28, 2017 5:33 AM | 1 vote

Hi Md. Mohashin Khan,

Since this query is more related to PowerShell, I will move this thread into PowerShell forum so that you would get more efficient support.

Best Regards,

Candy

Please remember to mark the replies as answers if they help.
If you have feedback for TechNet Subscriber Support, contact [email protected].

Hi Candy, this is not the right place for script requests, they belong here:

Script Center - What script do we need


Monday, October 2, 2017 9:06 AM | 1 vote

Thanks for your realize.


Monday, October 2, 2017 9:12 AM | 1 vote

Dear Paolo,

I have got a powershell scripts. Scripts also run by powershell command "PS C:\ .\FileBackup.ps1 C:\mysrcfolder C:\mydestfolder 30". But I want to this command convert to powershell scripts or batch file for execute automatic by task scheduler. Below the powershell scripts:

Param(
**  [Parameter(Mandatory=$true, position=0)][string]$source,**
**  [Parameter(Mandatory=$true, position=1)][string]$destination,**
**  [Parameter(Mandatory=$true, position=2)][Int]$days**
)

$src = $source.toLower()
$dest = $destination.toLower()

Write-Host "File backup started."
Write-Host "Source: " $src
Write-Host "Destination: " $dest
Write-Host "Policy: Files older than" $days "days"

Write-Host "Scanning files ..."

$FilesToCopy = Get-ChildItem $src -force -recurse| where-object {($_.LastWriteTime -gt (Get-Date).AddDays(-$days))  -and  ($_.PSIsContainer -ne $True)}

Write-Host "Scanning completed."

Write-Host "Copying files..."
$count = 0
Foreach($File in $FilesToCopy)

{
**    $src_file = $File.Fullname.tolower()**
**    $dst_file = $File.Fullname.tolower().replace($src, $dest)**
**    $dst_dir = Split-path -path $dst_file**
 
**     if (!(Test-Path -path $dst_dir) ) {**
**       Write-Host "`t Create directory " $dst_dir**
**       New-Item -path $dst_dir -type Directory | out-null**
**    }**
    
    
**    Copy-Item $src_file -Destination $dst_file**
**    $count++**
}

Write-Host "File copy completed."
Write-Host $count "file(s) copied."

Write-Host "File backup completed."
Sample Output

PS C:\ .\FileBackup.ps1 C:\mysrcfolder C:\mydestfolder 30


Tuesday, October 3, 2017 8:55 AM | 1 vote

Dear Paolo,

I have got a powershell scripts. Scripts also run by powershell command "PS C:\ .\FileBackup.ps1 C:\mysrcfolder C:\mydestfolder 30". But I want to this command convert to powershell scripts or batch file for execute automatic by task scheduler.

Hi Mohashin,

   It's very straightforward  to create a scheduled task to execute a PowerShell script. I suggest to follow this simple guide: https://blogs.technet.microsoft.com/heyscriptingguy/2012/08/11/weekend-scripter-use-the-windows-task-scheduler-to-run-a-windows-powershell-script/.

Just pay attention to add your arguments C:\mysrcfolder C:\mydestfolder 30  in add arguments field (optional). 

If you want to improve your script I suggest you:

  • to insert try catch for exception management to your script (i.e. UnauthorizedAccessException)
  • using an exit value (task scheduler logs the exit value of each execution)
  • fire some events like sending email or write a log for errors 

Regards


Monday, October 9, 2017 3:24 AM | 1 vote

Hi Paolo,

I have tried but command did not run from task schedule. Below the argument field command:

-file "C:\inetpub> .\incrementaldatabackup.ps1" E:\srcsync E:\dstsync 30

Please help me more to success for running scripts.


Monday, October 9, 2017 11:30 AM | 1 vote

Hi Mohashin,

   I hope to clarify it better with a screenshot.

   

FYI : This is another interesting article on how to create a new scheduled task using powershell:

https://blogs.technet.microsoft.com/heyscriptingguy/2015/01/13/use-powershell-to-create-scheduled-tasks/

Regards


Wednesday, October 11, 2017 3:49 AM

Hi Paolo

Thanks for your significant reply. I have attached screenshot.

Regards

Md. Mohashin Khan


Wednesday, October 11, 2017 10:57 AM

Hi Mohashin,

     Please pay attention to my screenshot. 

  • Program / Script : powershell 
  • Add arguments : "incrementaldatabackup.ps1 E:\srcsync E:\dstsync 30"
  • Start in : C:\inetpub 

   Go back to the article and make sure you understand every step of the process.

Regards