Jump to content

Recommended Posts

  • Platinum VIP
Posted (edited)

Hey guys :D

 

i'm working as an IT-System Administrator for a company and they needed a "Low-Budget" Backup for their Files.

this is running via a scheduled task everyday at 9pm on a server

 

If you got some question u can feel free to ask me :3

 

i thought i could share the script (reduced to basic) for your computers :)

Full script is made with PSVersion 5.1.14409.1018  

# Full DAILY Backupscript with 7 Days to keep

#region initializing
# You can use this here if you need to place the files on a NAS / Server
net use A: /d /y
net use A: \\path\to\backup\folder /user:username password
#endregion initializing

##############################################################################################################################################################
##                                     ##                                     ##                                     ##                                     ##
##            Set Variables            ##            Set Variables            ##            Set Variables            ##            Set Variables            ##
##                                     ##                                     ##                                     ##                                     ##
##############################################################################################################################################################
#region Variables
#F_DATE
# Here you collect the date & how many days u want to keep files. 
# i'm doing it 7 days back

    $date = Get-Date -Format "yyyy-MM-dd"
    $Daysback = "-7"
    $DatetoDelete = (Get-Date).AddDays($Daysback).ToString('yyyy-MM-dd')

    Write-Host -ForegroundColor Red -BackgroundColor Black "Date Defined"

#F_FPath
# Here we define our Paths where the files gonna be. an empty folder to clear faster, the Logdrive, the Drive u are gonna Backup, the Path to the Backup, and another logfile path
# also we create the actual folder :)
    $nofiles = "C:\Data\NoFiles"
    $strLogPath = "\\path\to\log\files\"
    $strMassDrive = "D:" 
    $strBackupDrive = "\\path\to\backup"
    $strLogDrive = "\\path\to\webserver\for\LOG"
    New-Item -Path $strBackupDrive\$date -ItemType directory -ErrorAction Ignore
    Write-Host -ForegroundColor Red -BackgroundColor Black "Backup Paths defined"

#F_Logfiles
# some stuff for Logs
# u don't need to change that :)
    $strLogFile ="$strBackupDrive\Logs\"+ $date + "_backup.txt"
    $strLogFile1 ="$strBackupDrive\Logs\"+ $date + "_full.txt"
    $strDelFile ="$strBackupDrive\Logs\"+ $date + "_deleted-files.txt"
    $shortCopyFile = "$strBackupDrive\"+ $date + "_copied.log"
    $shortDelFile  = "$strBackupDrive\"+ $date + "_deleted.log"
    $index = "$strLogDrive\index.html"
    Write-Host -ForegroundColor Red -BackgroundColor Black "Logfiles defined"

#F_Folders
# Here you place the folder which u want to backup.
# Syntax => $variable = ("pathtofile");
    $strDataFolder = ("C:\Users\ADMIN\Documents"); 
    $strPictureFolder = ("C:\Users\ADMIN\Pictures"); 

    Write-Host -ForegroundColor Red -BackgroundColor Black "Backup Folder defined"

#F_SetBackup
# Here we defined the "FROM & TO" stuff
# Always be sure the folders are in the same Order. FromFolder 1 should be the same as ToFolder 1
    $argsFromFolders = (   "$strDataFolder",                   "$strPictureFolder"); 
    $argsToFolders = (     "$strBackupDrive\$date\DATA",       "$strBackupDrive\$date\Pictures",); 
    Write-Host -ForegroundColor Red -BackgroundColor Black "Arguments defined"


Start-SleepProgress -Seconds 5
clear

#endregion
##############################################################################################################################################################
##                                     ##                                     ##                                     ##                                     ##
##             Make Backup             ##             Make Backup             ##             Make Backup             ##             Make Backup             ##
##                                     ##                                     ##                                     ##                                     ##
##############################################################################################################################################################
#region Backup
#F_OldBackup
# here we delete the old backup
    if (Test-Path "$strBackupDrive\$DatetoDelete") {
        robocopy $noFiles "$strBackupDrive\$DateToDelete" /MIR
        } else { Write-Host -ForegroundColor Cyan "No Backup found.. Continuing now"
    }

#F_NewBackup
# here we create the new backup, and create the logfile
For ($i=0; $i -lt $argsFromFolders.Length; $i++) { 
    robocopy $argsFromFolders[$i] $argsToFolders[$i] *.* /MIR /MT:8 /w:0 /r:1 /TEE /NP /NDL /DCOPY:T /UNILOG:$strLogfile
    gc $strLogFile | Out-File $strLogFile1 -Append
    gc $strLogFile | select -first 15 | Out-File $shortCopyFile -Append
    gc $strLogFile | select -last 10 | Out-File $shortCopyFile -Append
    Write-Host -ForegroundColor Cyan "Log Edited"
    } 

   

#endregion
##########################################################################################################################################################
##                                    ##                                    ##                                    ##                                    ##
##              LOG FILE              ##              LOG FILE              ##              LOG FILE              ##              LOG FILE              ##
##                                    ##                                    ##                                    ##                                    ##
##########################################################################################################################################################
#region Logfile

#F_CreateLog
# this is just to get an HTML Index file with the link to the Logfiles so u get an overview of the backups.
# if the backup is older than 7 days, the log file is still here but the files arent :P
    cat "$strBackupDrive\$date*.log" | sc $strBackupDrive\$date\overview.txt
    Write-Host -ForegroundColor Cyan "Logfiles merged"

    $cont = "<font face='monospace'>$(Get-Content $strBackupDrive\$date\overview.txt | ForEach-Object {"$($_ -replace '\s',' ') <br/>"})</font>"
    ConvertTo-Html -Title "Robocopy Backup" -Body $cont | Out-File $strLogPath\$date.small.html
    Write-Host -ForegroundColor Cyan "Backup Summary Created"

    $cont = "<font face='monospace'>$(Get-Content $strLogFile1 | ForEach-Object {"$($_ -replace '\s',' ') <br/>"})</font>"
    ConvertTo-Html -Title "Robocopy Backup" -Body $cont | Out-File $strLogPath\$date.big.html
    Write-Host -ForegroundColor Cyan "Full Backup Log Created"

    Add-Content -path $index -value "`r`n<a href=`"day/$date.small.html`">`&Uuml`;bersicht vom Daily Backup ( $date )</a>`&nbsp`;`&nbsp`;`&nbsp`;`&nbsp`;`&nbsp`;`&nbsp`;<a href=`"day/$date.big.html`">Ganzer Log vom Daily Backup ( $date )</a><br/>"
    Write-Host -ForegroundColor Cyan "Index up-2-date ($date)"

#Old_Files
# and here we delete the old folder and the temp logfiles
if (Test-Path "$strBackupDrive") {
    Remove-Item "$strBackupDrive\$DatetoDelete" -ErrorAction Ignore
    Remove-Item "$strBackupDrive\$date*.log" -ErrorAction Ignore
    Write-Host -ForegroundColor Cyan "Files Deleted"
    } else { Write-Host -ForegroundColor Cyan "No Logs found.. Continuing now"
}
 


#endregion

 

Edited by n30cortex
made a mistake XD
  • Like 2

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.