locked
Windows 2008 R2 (or 2012) : Daily backup script of folders to FTP ? RRS feed

  • Question

  • Hi,
    I have a Windows Server 2008 R2 system.
    And I want to make a daily backup (rotation 15 days) script of some folder to an FTP.

    I found this super script : https://gallery.technet.microsoft.com/scriptcenter/The-ultimate-backup-script-819e52ee#content
    But no option to FTP.

    Do you know how to add an option to save on FTP ?

    Thanks for your help :)

    • Moved by AnnaWY Wednesday, February 4, 2015 11:22 AM powershell related
    Tuesday, February 3, 2015 2:08 PM

Answers

  • Hi Nicopulse,

    You can try to sync the backup folder to FTP server, to upload the modified files based on lastwritetime to ftp server, please refer to this script:

    $BackUpdateTime  = (Get-Date).Year.ToString()
    $BackUpdateTime += (Get-Date).Month.ToString()
    $BackUpdateTime += (Get-Date).Day.ToString()
    $BackUpdateTime += (Get-Date).Hour.ToString()
    $BackUpdateTime += (Get-Date).Minute.ToString()
    $BackUpdateTime += (Get-Date).Second.ToString()
    $today = (Get-Date -Format yyyy-MM-dd)
    
     $ftp = "ftp://ftp.mysite.ca/" 
     $user = "ftpuser" 
     $pass = "ftppassword"  
      
     $webclient = New-Object System.Net.WebClient 
     $webclient.Credentials = New-Object System.Net.NetworkCredential($user,$pass) 
    
     #we specify the directory where all files that we want to upload  
     $Dir="Y:\LocalDirectory\" 
     $LogFile="I:\PowerShell\MyBackup_"+$today+".txt"
     Clear-Host
     #Write-Host $LogFile
     "From:"+$Dir+" (on server01) To:"+$ftp | Out-File $LogFile -Append
     "Start: "+(Get-Date) | Out-File $LogFile -Append
    
     $files = @(Get-ChildItem -Path  $Dir -Recurse | ?{ !$_.PSIsContainer } |Where-Object { $_.lastwritetime -gt (get-date).AddDays(-1)} | Select-Object -ExpandProperty FullName )
     foreach($item in $files)
     {
         if($item -ne $null)
      {
       $uri = New-Object System.Uri($ftp+$item.Substring(3))
       $webclient.UploadFile($uri, $item)
       #Write-Host (Get-Date)$item
       "$(Get-Date): "+$item | Out-File $LogFile -Append
      }
     }
     $webclient.Dispose()
    
     "End:"+(Get-Date) | Out-File $LogFile -Append

    Refer to:

    PowerShell Backup from Local folder to FTP location

    In addition, to manage folders in FTP server, please also check this module to help us to download and upload:

    PowerShell FTP Client Module

    If there is anything else regarding this issue, please feel free to post back.

    Best Regards,

    Anna Wang


    Please remember to mark the replies as answers if they help and unmark them if they provide no help. If you have feedback for TechNet Support, contact tnmff@microsoft.com

    • Proposed as answer by AnnaWY Wednesday, February 11, 2015 5:28 AM
    • Marked as answer by AnnaWY Thursday, March 5, 2015 5:28 AM
    Wednesday, February 4, 2015 12:05 PM

All replies

  • If you have a script that looks promising, you can modify it yourself.  Forums are not designed as sources of 'free consulting to create scripts'.  Your best bet is to try making the modifications, and then, if you run into issues, post specific questions in one of the scripting forums, such as the Official Scripting Guys Forum - https://social.technet.microsoft.com/Forums/en-US/home?forum=ITCG

    They won't write the scripts for you, but they are very helpful in pointing you in the right direction.


    . : | : . : | : . tim

    Tuesday, February 3, 2015 9:57 PM
  • Hi Nicopulse,

    You can try to sync the backup folder to FTP server, to upload the modified files based on lastwritetime to ftp server, please refer to this script:

    $BackUpdateTime  = (Get-Date).Year.ToString()
    $BackUpdateTime += (Get-Date).Month.ToString()
    $BackUpdateTime += (Get-Date).Day.ToString()
    $BackUpdateTime += (Get-Date).Hour.ToString()
    $BackUpdateTime += (Get-Date).Minute.ToString()
    $BackUpdateTime += (Get-Date).Second.ToString()
    $today = (Get-Date -Format yyyy-MM-dd)
    
     $ftp = "ftp://ftp.mysite.ca/" 
     $user = "ftpuser" 
     $pass = "ftppassword"  
      
     $webclient = New-Object System.Net.WebClient 
     $webclient.Credentials = New-Object System.Net.NetworkCredential($user,$pass) 
    
     #we specify the directory where all files that we want to upload  
     $Dir="Y:\LocalDirectory\" 
     $LogFile="I:\PowerShell\MyBackup_"+$today+".txt"
     Clear-Host
     #Write-Host $LogFile
     "From:"+$Dir+" (on server01) To:"+$ftp | Out-File $LogFile -Append
     "Start: "+(Get-Date) | Out-File $LogFile -Append
    
     $files = @(Get-ChildItem -Path  $Dir -Recurse | ?{ !$_.PSIsContainer } |Where-Object { $_.lastwritetime -gt (get-date).AddDays(-1)} | Select-Object -ExpandProperty FullName )
     foreach($item in $files)
     {
         if($item -ne $null)
      {
       $uri = New-Object System.Uri($ftp+$item.Substring(3))
       $webclient.UploadFile($uri, $item)
       #Write-Host (Get-Date)$item
       "$(Get-Date): "+$item | Out-File $LogFile -Append
      }
     }
     $webclient.Dispose()
    
     "End:"+(Get-Date) | Out-File $LogFile -Append

    Refer to:

    PowerShell Backup from Local folder to FTP location

    In addition, to manage folders in FTP server, please also check this module to help us to download and upload:

    PowerShell FTP Client Module

    If there is anything else regarding this issue, please feel free to post back.

    Best Regards,

    Anna Wang


    Please remember to mark the replies as answers if they help and unmark them if they provide no help. If you have feedback for TechNet Support, contact tnmff@microsoft.com

    • Proposed as answer by AnnaWY Wednesday, February 11, 2015 5:28 AM
    • Marked as answer by AnnaWY Thursday, March 5, 2015 5:28 AM
    Wednesday, February 4, 2015 12:05 PM