zoukankan      html  css  js  c++  java
  • [PowerShell] Backup Folder and Files Across Network

    ## This Script is used to backup folder/files and delete the old backup files.
    ## Author: Stefanie
    ## Last Update Date: 07/10/2013
    ## Copyright (c) Stefanie, All Rights Received. 
    
    
    # Declare datetime for now
    $now = get-date
    # Declare today string: "20130101"
    $today = (get-date -UFormat %Y%m%d)
    
    # Log file path
    $logFilePath = "d:VMBackupLog_$today.txt"
    
    # Network Map Drive, Declare drive name, UserName and Password  
    $destDrive = "f:"
    $User = "BackupUser"
    $Password = "password01!"
    
    
    ########################## Common Function ########################################
    
    # Log function
    function AddToLog{
    param ($ErrorMsg)
        $ErrorMsg = "$now:`r`n $ErrorMsg"
        Write-Output $ErrorMsg
        Add-Content $logFilePath $ErrorMsg
    }
    
    # Copy file to other place
    function CopyFile{
    param($sourceFilePath,$destinationFolder)
    
        $net = new-object -ComObject WScript.Network
        $net.MapNetworkDrive($destDrive, $destinationFolder, $false, $User, $Password)
        
        Try 
        { 
            $destFolder =  $destDrive +“$today"
    
            if (!(Test-Path -path $destFolder)) {
                New-Item $destFolder -Type Directory
            }
            copy-item -Force $sourceFilePath $destFolder
        } 
        Catch 
        { 
             AddToLog "ERROR Copying $sourceFilePath to $destinationFolder`r`n ExceptionMsg: $_”
        }
    
        $net.RemoveNetworkDrive($destDrive,"true","true")
    
    }
    
    # Copy folder and subfolder to other palce
    function CopyFolder{
    param($sourceFolder,$destinationFolder,$exclude)
    
        $net = new-object -ComObject WScript.Network
        $net.MapNetworkDrive($destDrive, $destinationFolder, $true, $User, $Password)
    
        Try 
        { 
            $destFolder =  $destDrive+“$today"
            if (!(Test-Path -path $destinationFolder)) {
                New-Item $destFolder -Type Directory
            }
    
            Get-ChildItem $sourceFolder -Recurse -Exclude $exclude | % { 
                    $filePath = $_.FullName
                    $subFolder = $filePath.Replace($sourceFolder, "")
                    Copy-Item $filePath -Destination "$destFolder$subFolder" -Force 
            }
        } 
        Catch 
        { 
             AddToLog "ERROR Copying Folder $sourceFolder to $destinationFolder`r`n ExceptionMsg: $_”
        } 
        
        $net.RemoveNetworkDrive($destDrive,"true","true")
    }
    
    # Delete Old backup file
    function DeleteItems{
    param($folderPath,[int]$remainDays)
        
        $net = new-object -ComObject WScript.Network
        $net.MapNetworkDrive($destDrive, $folderPath, $false, $User, $Password)
    
        Try 
        { 
            $dayStr = Get-Date ($now.AddDays(-$remainDays)) -UFormat "%Y%m%d"
            $deleteItems = Get-ChildItem "$destDrive" | Where { [int]$_.Name -le [int]$dayStr}
            foreach($item in $deleteItems){
                Remove-Item $item.FullName -Recurse
            }
        } 
        Catch 
        { 
             AddToLog "ERROR, Removing the item $folderPath`r`n ExceptionMsg: $_”
        } 
    
        $net.RemoveNetworkDrive($destDrive,"true","true")
    }
    
    
    # Delete Old Database file
    # ADDB_backup_2013_07_04_010006_2927760.bak
    function DeleteDBItems{
    param($folderPath,[int]$remainDays)
        
        $net = new-object -ComObject WScript.Network
        $net.MapNetworkDrive($destDrive, $folderPath, $false, $User, $Password)
    
        Try 
        { 
            $day = Get-Date ($now.AddDays(-$remainDays)) -UFormat "%Y%m%d"
            $deleteItems = Get-ChildItem "$destDrive" #| Where-Object { $_.LastWriteTime -le $day}
            foreach($item in $deleteItems){
                $itemName = $item.Name
                $itemLen = $itemName.Length
    
                #Get file create day by file name. 
                if([int]$itemLen -gt 29){
                    $fileDay = $itemName.Substring( $itemLen - 29,10).Replace("_","")
    
                    #Compare with remain day to check if the file need to delete
                    if($fileDay -le $day){
                        Remove-Item $item.FullName -Recurse
                        #$item.FullName
                    }
                }
            }
        } 
        Catch 
        { 
             AddToLog "ERROR, Deleting the DB item $folderPath`r`n ExceptionMsg: $_”
        } 
        $net.RemoveNetworkDrive($destDrive,"true","true")
    }
    
    ########################## Common Function ##########################
    
    
    
    
    ########################## For Debug ##########################
    #CopyFile "D:VM	estDrupal.bak" "\192.168.1.6BackupFolder"
    
    #CopyFolder "D:VM	est" "\192.168.1.6BackupFolder" "2.1*"
    #DeleteItems "\192.168.1.6BackupFolder" 1
    #DeleteDBItems "\192.168.1.6BackupFolder" 1
    
    ########################## For Debug ##########################
  • 相关阅读:
    Rhythmk 一步一步学 JAVA(4):Spring3 MVC 之 Hello Word
    使用webclient同时post普通表单字段和文件表单字段数据到指定的URL【转】
    Rhythmk 一步一步学 JAVA(2) : 操作 MYSQL 数据库
    判断FLASH版本问题
    Rhythmk 一步一步学 JAVA(3): java JSON 数据序列化 以及反序列化
    Rhythmk 一步一步学 JAVA(5) Hibernate环境配置
    JS 数据存储
    文件下载 获取远程图片
    SQL SERVER 学习笔记
    AngularJS源码学习一 :directive
  • 原文地址:https://www.cnblogs.com/wpsl5168/p/3182137.html
Copyright © 2011-2022 走看看