## 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. 在C51中如何实现软复位?

    可以定义一个指向复位向量(0x0000)的函数指针,然后在C程序中需要软复位的地方调用该函数: ((void (code *) (void)) 0x0000) (); 例如,以下程序不断地复位: vo ...

  2. unix c 06

    文件操作 fcntl-> 复制文件描述符/取文件状态/文件锁 文件一系列函数-> access/chmod/truncate/... 目录操作 相关函数:mkdir/rmdir/telld ...

  3. Spring集成Hessian

    一.概述 Spring 通过org.springframework.remoting.caucho.HessianServiceExporter将POJO中的所有public方法发布为Hessian服 ...

  4. hdu 1011 Starship Troopers_树状dp

    题目链接 题意:给你一棵树(必须从根节点出发),每个节点上都有bug和value,你有m个骑士,每个骑士能消灭20个bug,你必须消灭该节点的全部bug才能拿到该节点的value,问最多能拿到valu ...

  5. Ajax_post发送

    $('#img_file_del_3').click(function() { var data={name:$('#img_file_del_3').attr('name')}; var url=' ...

  6. dispatch_group_async

    - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. dispat ...

  7. 玩程序 之 一 . 字符串处理工具(可通过C#脚本扩展)

    平常喜欢写点小东西玩玩,既可以娱乐自己满足自己的虚荣心,又可以方便工作和学习,今天且拿出一个来,与大家一起分享!  1. 软件介绍 言归正传,先看看需求,有这样一串字符串 abc,def,ghi,jk ...

  8. java与.net比较学习系列(4) 运算符和表达式

    上一篇总结了java的数据类型,得到了冰麟轻武等兄弟的支持,他们提出并补充了非常好的建议,在这里向他们表示感谢.在后面的文章中,我会尽力写得更准确和更完善的,加油! 另外,因为C#是在java之后,也 ...

  9. java实现各种数据统计图(柱形图,饼图,折线图)

    近期在做数据挖掘的课程设计,须要将数据分析的结果非常直观的展现给用户,这就要用到数据统计图,要实现这个功能就须要几个第三方包了: 1.       jfreechart-1.0.13.jar 2.   ...

  10. ACM-简单题之Ignatius and the Princess II——hdu1027

    转载请注明出处:http://blog.csdn.net/lttree Ignatius and the Princess II Time Limit: 2000/1000 MS (Java/Othe ...