## 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 ##########################

[PowerShell] Backup Folder and Files Across Network的更多相关文章

  1. BSA Network Shell系列-通过NSH执行Powershell,VBScript或bat files脚本

    参考:Running Powershell, VBScript, or bat files via NSH 如果你直接在NSH命令行执行的话,可以参考我翻译的下面的东东,如果想运行NSH 脚本作业的话 ...

  2. Azure PowerShell (13) 批量设置Azure ARM Network Security Group (NSG)

    <Windows Azure Platform 系列文章目录> 刚刚在帮助一个合作伙伴研究需求,他们的虚拟机全面的网络安全组(Network Security Group, NSG)会经常 ...

  3. What is required for a successful backup of all files during hoi backup?

    There is a typo in the body of this question. It should be "Hot" instead of "hoi" ...

  4. How to create a project with existing folder of files in Visual Studio?

    1. Select Visual Studio tool bar-> New -> Project from existing code-> continue with config ...

  5. C# copy folder and files from source path to target path

    static void Main(string[] args) { string sourceDir = @"E:\SourcePath"; string destDir = @& ...

  6. [转]Configure Network Drive Visible for SQL Server During Backup and Restore Using SSMS

    本文转自:https://mytechmantra.com/LearnSQLServer/Configure-Network-Drive-Visible-for-SQL-Server-During-B ...

  7. TFS Express backup and restore

    When we setup source control server, we should always make a backup and restore plan for it. This ar ...

  8. VC++6.0在Win7以上系统上Open或Add to Project files崩溃问题 解决新办法

    崩溃原因是和office高版本冲突,比如我64位win7装了64位office2013及visio就遇到了这个问题(我很纳闷,记得重装系统前装的是32位office2013及visio就未曾遇到该问题 ...

  9. Three Steps to Migrate Group Policy Between Active Directory Domains or Forests Using PowerShell

    Three Steps Ahead Have you ever wished that you had three legs? Imagine how much faster you could ru ...

随机推荐

  1. hdu 4336 Card Collector(期望 dp 状态压缩)

    Problem Description In your childhood, people in the famous novel Water Margin, you will win an amaz ...

  2. 使用ssh远程执行命令批量导出数据库到本地

    前天正在跟前端的同事调试功能.服务器开好,模拟的玩家登录好,就在倒计时.这时突然运营的同事跑过来说要统计几个服务器玩家的一些情况,也就是需要从几个服的数据库导出部分玩家的数据.好吧,我看了一下时间,1 ...

  3. 01串(dp)

    01串 时间限制:1000 ms  |  内存限制:65535 KB 难度:2   描述 ACM的zyc在研究01串,他知道某一01串的长度,但他想知道不含有“11”子串的这种长度的01串共有多少个, ...

  4. Weblogic的Admin server进程将CPU消耗尽问题解决

    1.serverCPU被耗尽,持续100% 以下附nmon图 2.两个weblogicadmin server进程将CPU耗尽 问题:24298进程,占用百分之四千多的CPU资源 23529进程,占用 ...

  5. 2014 (多校)1011 ZCC Loves Codefires

    自从做了多校,整个人都不好了,老是被高中生就算了,题老是都不懂=-=原谅我是个菜鸟,原谅我智力不行.唯一的水题. Problem Description Though ZCC has many Fan ...

  6. Oracle 10g轻量级客户端安装[转]

    http://www.oracle.com/technetwork/cn/topics/winsoft-095945-zhs.html oracle技术官方网 http://www.oracle.co ...

  7. Android应用程序资源的查找过程分析

    文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/8806798 我们知道,在Android系统中, ...

  8. WebApi官网学习记录---web api中的路由

    如果一条路由匹配,WebAPI选择controller和action通过如下方式: 1.找到controller,将"controller"赋值给{controller}变量 2. ...

  9. .Net Memory -- GC基本知识

    参考资料: http://blogs.msdn.com/b/tess/archive/2008/04/17/how-does-the-gc-work-and-what-are-the-sizes-of ...

  10. 9、Khala实现0.01版QQ

    这次来个有界面的. 登录界面: 主界面: 1.服务端开发: 只需创建一个类ChatType(./examples/HelloKhala/src/ChatType.cpp),在该类型中,核心为创建的两个 ...