## 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. XML基础<第一篇>

    一.XML简介 XML是一种标记语言,用于描述数据,它提供一种标准化的方式来来表示文本数据.XML文档以.xml为后缀.需要彻底注意的是XML是区分大小写的. 先从一个简单的XML例子来了解下xml基 ...

  2. 11g Rac 切换

    <pre name="code" class="sql">[grid@devrac1 ~]$ crsctl status res -t ------ ...

  3. Hessian和Burlap入门教程

    一.简介 Hessian和Burlap是由Caucho Technology提供的基于HTTP协议的轻量级远程服务解决方案.他们都致力于借助尽可能简单那的API和通信协议来简化Web服务.    He ...

  4. Android 开发笔记-Eclipse中文乱码

    使用eclipse时经常中文乱码网上搜罗了下解决办法:   使用Eclipse编辑文件经常出现中文乱码或者文件中有中文不能保存的问题,Eclipse提供了灵活的设置文件编码格式的选项,我们可以通过设置 ...

  5. 《Java程序员面试笔试宝典》之组合与继承有什么区别

    组合和继承是面向对象中两种代码复用的方式.组合是指在新类里面创建原有类的对象,重复利用已有类的功能.继承是面向对象的主要特性之一,它允许设计人员根据其它类的实现来定义一个类的实现.组合和继承都允许在新 ...

  6. 【转】PF_NETLINK应用实例NETLINK_KOBJECT_UEVENT具体实现--udev实现原理

    相对于linux来说,udev还是一个新事物.然而,尽管它03年才出现,尽管它很低调(J),但它无疑已经成为linux下不可或缺的组件了.udev是什么?它是如何实现的?最近研究Linux设备管理时, ...

  7. vc10的C2664和C2065错误

    在vs2010中编译一个普通的C++程序(Win32 Console Application),都会出现这两个错误! 究其原因是:我们已经习惯了VC6的种种简陋和不规范! 例如,下列程序在VC6中编译 ...

  8. hdu 2825 Wireless Password(ac自己主动机&amp;dp)

    Wireless Password Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  9. 微信网页授权获取code链接

    本公众号授权 "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + appid + "&r ...

  10. [Spring入门学习笔记][maven]

    什么是maven? 我的理解: 一个项目有一大堆依赖包的时候,没必要下下来,可以利用maven中的pom.xml 指定需要那些依赖包,让maven去本地中央库(如果没找到)->网上仓库库帮你调用 ...