## 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. 【给你几个使用Xamarin的理由】

    写在开篇前 这种代理操作,绑定影射的机制,有些极端的开发者确实难以接受.追求完美,总感觉原生的各种优点. 如果你非得较这个真,那您还是感觉补习下 Java Eclipse  ,买一台Mac 恶补Obj ...

  2. 深入解析spring中用到的九种设计模式

    转载请注明出处,文章首发于:http://itxxz.com/a/javashili/tuozhan/2014/0601/7.html 设计模式作为工作学习中的枕边书,却时常处于勤说不用的尴尬境地,也 ...

  3. 工具:BT Sync 同步文件

    随着互联网的发展,文件共享变得越来越便捷,但是文件的共享过程是不是安全,这一直是人们关心的问题,今天向大家介绍一个共享工具,可以实现便捷的安全共享.      这个工具分为两个部分,一个是服务器部分, ...

  4. poj 2481 Cows(数状数组 或 线段树)

    题意:对于两个区间,[si,ei] 和 [sj,ej],若 si <= sj and ei >= ej and ei - si > ej - sj 则说明区间 [si,ei] 比 [ ...

  5. bst 二叉搜索树简单实现

    //数组实现二叉树: // 1.下标为零的元素为根节点,没有父节点 // 2.节点i的左儿子是2*i+1:右儿子2*i+2:父节点(i-1)/2: // 3.下标i为奇数则该节点有有兄弟,否则又左兄弟 ...

  6. Java宝典(一)

    -switch语句能作用在byte上,能否作用在long上,能否作用在String上? -在switch(expr1)中,expr1只能是一个整数表达式或者枚举常量,整数表达式可以是int基本类型或I ...

  7. mysql的日志

    是否启用了日志mysql>show variables like ‘log_bin’; 怎样知道当前的日志mysql> show master status; 看二进制日志文件用mysql ...

  8. poj 3616 Milking Time DP

    题意:在给予的N个时间里,奶牛Bessie在M个时间段里进行产奶,但是每次产奶后都要休息R个时间 M个时间段里,分别有开始时间start和结束时间end,和该时间段里产奶的效率efficiency 求 ...

  9. TCP/IP远程访问操作:rwho,rlogin,rcp和rsh

    TCP/IP网络通信 软件 包使用远程访问 的 命令 ,这些命令首先是由UC Berkely为Arpanet开发的.它允许您远程注册到另一个 系统 中,并从一个系统复制文件到另一个系统.您能取得关于一 ...

  10. mybatis于Date和DateTime现场插入

    最近,该公司使用MyBatis3做数据持久层,有在该领域Date和DateTime种类,只有在插入数据时属性设置为一个实体Timestamp将相应mysql的DateTime类型.Date会相应mys ...