惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Vercel News
Vercel News
博客园 - 司徒正美
大猫的无限游戏
大猫的无限游戏
Last Week in AI
Last Week in AI
V
Visual Studio Blog
阮一峰的网络日志
阮一峰的网络日志
小众软件
小众软件
宝玉的分享
宝玉的分享
Apple Machine Learning Research
Apple Machine Learning Research
美团技术团队
WordPress大学
WordPress大学
博客园 - 聂微东
人人都是产品经理
人人都是产品经理
罗磊的独立博客
The Cloudflare Blog
V
V2EX
月光博客
月光博客
有赞技术团队
有赞技术团队
Y
Y Combinator Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
GbyAI
GbyAI
博客园 - 【当耐特】
T
Tailwind CSS Blog

博客园 - stefanie

SQL - 获取多机构最近相同节点 PowerShell-将CSV导入SQL Server [Powershell] 检查IIS设置 VBS 操作注册表 十六进制 无法在People Picker中选择用户 Microsoft Certification List WINDOWS 2012忘记密码之后。。。 Auto Install Workflow Manager 1.0 批量删除Kindle Personal Documents 导入Excel表格到SharePoint站点 用户中心 - 博客园 [Powershell] FTP Download File SSRS 请求并显示SharePoint人员和组字段 VS 2010 安装 .net framework2.0/3.0/3.5 How to Setup AssociatedOwnerGroup / AssociatedMemberGroup / AssociatedVisitorGroup RPC服务不可用 [转]SharePoint Versions 安装 64 位版本的 Office 2010 后,无法查看数据表视图中的列表 [MOSS]关闭弹出窗口
[PowerShell] Backup Folder and Files Across Network
stefanie · 2013-07-10 · via 博客园 - stefanie
## 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:\VMBackup\Log_$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\test\Drupal.bak" "\\192.168.1.6\BackupFolder"

#CopyFolder "D:\VM\test\" "\\192.168.1.6\BackupFolder" "2.1*"
#DeleteItems "\\192.168.1.6\BackupFolder" 1
#DeleteDBItems "\\192.168.1.6\BackupFolder" 1

########################## For Debug ##########################