Vous êtes sur la page 1sur 3

my subreddits front - all - random | askreddit - funny - news - todayilearned - pics - videos - worldnews - gifs - gaming - movies - aww

s - aww - showerthoughts - mildlyinteresting - jokes


more- nott
»

R/POWERSHELL comments

Submission Guidelines | Link Flair - How To


Want to join? Log in or sign up in seconds. | English

This is an archived post. You won't be able to vote or comment.

Script Sharing Script to Compress and RoboCopy backup files to another this post was submitted on 12 Jan 2016
10
server, as well as clean up old files. (My first script - EVER) (self.PowerShell)
submitted 6 months ago * by admin_n00b
10
This is my first PS script that I have put together using pieces of scripts from all over the (92% upvoted)
place.
shortlink:
I have been searching for a script that would compress the Database backups every day,
and then copy them off the server for archiving. I also have this script files .zip's that are
older than seven days.
If you have any tips or criticism, I'm open to them all. I'll try to update the script as I get
suggestions, and will start the script as v1.0 and change this as I make changes here. remember me reset password login
The script has been written as modular as possible, and can easily be edited to not zip
the files, zip different files, not robocopy the files, or not delete old files.

Prerequisites

This script obviously requires PowerShell, but I am not aware if this works in all
versions. Confirmed to work in PowerShell 4. discuss this ad on reddit
This script also uses Robocopy, but I am not aware if this works in all versions.
Robocopy is bundled into Windows. My version will not give me a version number, Submit a new link
but it is the version bundled with Server 2012 R2.
This script requires the command line version of 7zip. Link to 7zip.org's download Submit a new text post
page.
The script assumes the following directory layout, which can be changed using the
PowerShell
"Variables" section at the start:
subscribe
Local Server Directories
17,662 readers
Backups folder: C:\Backups\
~40 users here now
Logs folder: C:\Backups\Logs\
ABOUT POWERSHELL
7zip executable: C:\7za\7za.exe
Windows PowerShell (POSH) is a command-line shell
and associated scripting language created by
Microsoft. Offering full access to COM, WMI and .NET,
Script POSH is a full-featured task automation framework
for distributed Microsoft platforms and solutions.

#################################################################### SUBREDDIT FILTERS


#########
## This script zips any .bak files in the C:\Backups folder and then Desired State Configuration
##
## copies them to the CentralDBRepo server (A physically seperate ma Unanswered Questions
chine) ##
## which will then copy and archive further. Solved Questions
##
####################################################################
News
#########
## Put together by: ## Created:
## Information
## Aaron Doyle ## 07-Jan-2016
## Script Sharing
####################################################################
######### Daily Post
## Last edit: 12-Jan-2016 ## Version: 1.01
## Misc
####################################################################
######### RESOURCES

############### SCRIPT REPOSITORIES


## Variables ##
############### VIRTUAL LABS & BOOKS
BLOGS & PODCASTS
## Used for Ziping Backups
$sourcedir = "C:\Backups\" REGIONAL USER GROUPS
set-alias 7z "C:\7za\7za.exe"
$files = Get-ChildItem $sourcedir -Recurse -File | Where-Object { $_ a community for 7 years
.Extension -eq ".bak" }

## Used for Robocopy


$date = get-date -f yyyy-MMM-dd
$logfileloc = "c:\Backups\Logs\"
$logfile = $date
$logfile += "-RobocopyLog.log"
$logparam = "/LOG+:"
$logparam += $logfileloc
$logparam += $logfile

#################
## Zip backups ##
#################
discuss this ad on reddit
foreach ($file in $files)
{
$name = $file.name MODERATORS message the moderators
$directory = $file.DirectoryName
malice8691
7z a -tzip "$directory\$file.zip" "$directory\$name"
Davotronic5000
}
derekhans
############## about moderation team »
## Robocopy ##
############## < > discussions in /r/PowerShell X

· 1 comment
NET USE \\REMOTESERVER\Backups /user:REMOTESERVER\Robocopy pa55w0rd REQUEST: Need help taking Excel Workbook and
robocopy C:\Backups\ \\REMOTESERVER\Backups\ /E *.log *.bak *.zip /Z converting data to CSV
B /MIR $logparam
NET USE \\REMOTESERVER\Backups /D

#############
## Cleanup ##
#############

Get-ChildItem -path $sourcedir -Recurse -Include *.zip |? { $_.last


writetime -le (get-date).adddays(-7)} |% {remove-item $_ -force -rec
urse }
Get-ChildItem -path $sourcedir -Recurse -Include *.bak |? { $_.last
writetime -le (get-date).adddays(-1)} |% {remove-item $_ -force -rec
urse }

####################################################################
#########
## Note:
##
## Instead of using the 7zip program, you can use the native Windows
##
## compression, using:
##
##
##
## Add-Type -assembly "system.io.compression.filesystem"
##
## [io.compression.zipfile]::CreateFromDirectory($Source, $destina
tion) ##
##
##
## However, I prefer the 7zip programs versatility.
##
## More information at: http://www.dotnetperls.com/7-zip-examples
##
####################################################################
#########

4 comments share

all 4 comments

sorted by: best

[–] litemage 3 points 6 months ago


Straightforward script.
My only question would be why not launch the script from an account that has local admin privileges on
the REMOTESERVER? Heck, might even be able to do it with just user privileges. That would prevent
needing to use NET USE and storing the Robocopy password in plain text.
permalink embed

[–] admin_n00b [S] 1 point 6 months ago

Thats a good point. The issue here, is that the two computers I created this for, are not part of a Domain.
Therefore, to access the share folder, Robocopy needed an account on the remote server (whichever way
around I put it).
However, I see your point. I'll play around with that and see if I can get that working within a Domain
environment.
permalink embed parent

[–] Drox1 2 points 6 months ago

Instead of using 7zip to do this, you could try to just call the native Windows compression:

Add-Type -assembly "system.io.compression.filesystem"

[io.compression.zipfile]::CreateFromDirectory($Source, $destination)
permalink embed

[–] admin_n00b [S] 2 points 6 months ago

I didn't realize that was an option. In all my searching, no one ever used that. Thanks.
However, I think I am going to keep 7zip in there, and add a footnote for this.
The 7zip program is far more versatile, allowing more compression types (.7z .tar .tar.gz etc.), as well as
having other parameters to control the compression, or adding files to a current archive of any type.
Side note: I am only using .zip because some machines on the network don't have ability to open .7z yet,
and .zip is handled natively by windows. But this will change in the future to a better compression
standard.
permalink embed parent

about help apps & tools <3


blog site rules Reddit for iPhone reddit gold
about FAQ Reddit for Android redditgifts
source code wiki mobile website
advertise reddiquette buttons
jobs transparency
contact us

Use of this site constitutes acceptance of our User Agreement and Privacy Policy (updated). © 2016 reddit inc. All rights reserved.
REDDIT and the ALIEN Logo are registered trademarks of reddit inc.

Vous aimerez peut-être aussi