[PowerShell] Backup Folder and Files Across Network
## 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的更多相关文章
- BSA Network Shell系列-通过NSH执行Powershell,VBScript或bat files脚本
参考:Running Powershell, VBScript, or bat files via NSH 如果你直接在NSH命令行执行的话,可以参考我翻译的下面的东东,如果想运行NSH 脚本作业的话 ...
- Azure PowerShell (13) 批量设置Azure ARM Network Security Group (NSG)
<Windows Azure Platform 系列文章目录> 刚刚在帮助一个合作伙伴研究需求,他们的虚拟机全面的网络安全组(Network Security Group, NSG)会经常 ...
- 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" ...
- 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 ...
- C# copy folder and files from source path to target path
static void Main(string[] args) { string sourceDir = @"E:\SourcePath"; string destDir = @& ...
- [转]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 ...
- TFS Express backup and restore
When we setup source control server, we should always make a backup and restore plan for it. This ar ...
- VC++6.0在Win7以上系统上Open或Add to Project files崩溃问题 解决新办法
崩溃原因是和office高版本冲突,比如我64位win7装了64位office2013及visio就遇到了这个问题(我很纳闷,记得重装系统前装的是32位office2013及visio就未曾遇到该问题 ...
- 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 ...
随机推荐
- PowerShell_零基础自学课程_8_高级主题:WMI对象和COM组件
本系列文章从最初的初识开始,基本上可以完成一些简单的系统管理了,为了更方便的管理系统,同时为了更好的发掘系统的性能,就需要用到系统提供 的一些高级特性,在Windows Server系列的OS中,如果 ...
- cf486A Calculating Function
A. Calculating Function time limit per test 1 second memory limit per test 256 megabytes input stand ...
- bzoj1671 [Usaco2005 Dec]Knights of Ni 骑士
Description Bessie is in Camelot and has encountered a sticky situation: she needs to pass through t ...
- error Infos
- extjs两个tbar问题
版本:extjs3.4 接触过extjs的同志们都知道每个panel都有一个tbar(top bar 上面工具栏) ,bbar(bottom bar 底部工具栏) 大家做查询页面,一 ...
- Linux用户管理(笔记)
用户:UID, /etc/passwd组:GID, /etc/group 影子口令:用户:/etc/shadow组:/etc/gshadow 用户类别:管理员:0普通用户: 1-65535 系统 ...
- 神坑 关于&&的取值
a = 0&&"ssss": 结果a=0 a=true&&"w": 结果a=w: 类似于 前面是真的 会执行后面并返回后面 前面 ...
- hdu 3711 Binary Number(暴力 模拟)
Problem Description For non-negative integers x and y, f(x, y) , )=,f(, )=, f(, )=. Now given sets o ...
- Codeforces Round #389(div 2)
A =w= B QvQ C 题意:在一个格子图里给出一个路径 里面有UDLR四种移动方向 问 我在格子路径里面最少选几个点 可以让我沿着格子路径走 其实是在相邻的点与点之间走最短路 分析:贪心+模拟 ...
- hadoop执行hbase插入表操作,出错:Stack trace: ExitCodeException exitCode=1:(xjl456852原创)
在执行hbase和mapreduce融合时,将hdfs上的文本文件插入到hbase中,我没有使用"胖包"(胖包就是将项目依赖的jar包放入项目打包后的lib目录中),而是直接将hb ...