Perpetual Backup Batch File Script

IT Support Forum Forums Backup and Restore General Discussion Perpetual Backup Batch File Script

This topic contains 1 reply, has 1 voice, and was last updated by  Webmaster 1 month, 3 weeks ago.

  • Author
    Posts
  • #1969

    Webmaster
    Keymaster

    Here’s my batch file script to perpetually backup some files to a drive, deleting the files older than a day.

    :Start
    forfiles /p “G:” /s /m *.* /c “cmd /c Del @path” /d -1
    set folder=Date_%date:~10,4%_%date:~7,2%_%date:~4,2%_Time_%time:~0,2%_%time:~3,2%_%time:~6,2%_%time:~9,2%
    mkdir G:\%folder%
    robocopy C:\folder G:\%folder% /XO
    Timeout 1
    GoTo Start

    The script does each backup to a new folder, so you can recover files from any time of the day. If you want to perpetually backup the files to only have one copy, you can just use the following batch file script:

    robocopy C:\folder G:\%folder% /XO

  • #1972

    Webmaster
    Keymaster

    Perpetual Backup PowerShell Script

    Ok, so the batch file doesn’t work as well as a PowerShell script because the command forfiles doesn’t work well. A better script is the following PowerShell script, which you can save as a ps1 file and run it the same as a batch file (perpetually).

    Here’s the PowerShell version of my perpetual backup batch file script:

    while ($true){
    Get-ChildItem –Path “G:\” –Recurse | Where-Object CreationTime –lt (Get-Date).AddDays(-1) | Remove-Item
    $folder=((Get-Date).Year).ToString()+”_”+((Get-Date).Month).ToString()+”_”+((Get-Date).Day).ToString()+”_”+((Get-Date).Hour).ToString()+”_”+((Get-Date).Minute).ToString()+”_”+((Get-Date).Millisecond).ToString()
    mkdir G:\$folder
    Copy-Item C:\ G:\$folder
    Timeout 60
    }

You must be logged in to reply to this topic.