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

推荐订阅源

WordPress大学
WordPress大学
T
Threatpost
阮一峰的网络日志
阮一峰的网络日志
美团技术团队
F
Fortinet All Blogs
The GitHub Blog
The GitHub Blog
月光博客
月光博客
V
Visual Studio Blog
T
Tailwind CSS Blog
Stack Overflow Blog
Stack Overflow Blog
博客园 - 聂微东
Jina AI
Jina AI
J
Java Code Geeks
Martin Fowler
Martin Fowler
大猫的无限游戏
大猫的无限游戏
Recorded Future
Recorded Future
C
Check Point Blog
腾讯CDC
N
Netflix TechBlog - Medium
aimingoo的专栏
aimingoo的专栏
罗磊的独立博客
Hacker News: Ask HN
Hacker News: Ask HN
SecWiki News
SecWiki News
博客园 - Franky
Hacker News - Newest:
Hacker News - Newest: "LLM"
N
News | PayPal Newsroom
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
S
Security @ Cisco Blogs
W
WeLiveSecurity
The Last Watchdog
The Last Watchdog
Cloudbric
Cloudbric
F
Full Disclosure
The Cloudflare Blog
Y
Y Combinator Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Google DeepMind News
Google DeepMind News
MongoDB | Blog
MongoDB | Blog
S
Schneier on Security
Schneier on Security
Schneier on Security
Spread Privacy
Spread Privacy
L
LINUX DO - 热门话题
AI
AI
N
News and Events Feed by Topic
T
Tor Project blog
P
Palo Alto Networks Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
H
Hackread – Cybersecurity News, Data Breaches, AI and More
G
Google Developers 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 ##########################