-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBackupDate.ps1
154 lines (134 loc) · 5.27 KB
/
BackupDate.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
###############################################
# --SCRIPT BACKUPDATE-- #
#---------------------------------------------#
# Creator : Cedric H. #
# Version : 1.2 #
# Last Update : 30/01/2023 #
#---------------------------------------------#
# Download only the files/folders #
# that have been recently #
# modified or created and keeps a copy of #
# each version of each file #
#---------------------------------------------#
# /!\ SECURITY /!\ #
# 1-Use an IPSEC VPN for remote servers. #
# 2-Create a Special user for shared folders. #
#---------------------------------------------#
# Backup Schema #
#---------------------------------------------#
# #
# BACKUP_2022/January/01-01-2022 #
# /02-01-2022 #
# /03-01-2022 #
# **-**-***** #
# /Febuary/01-02-2022 #
# /02-02-2022 #
# /**-**-**** #
# BACKUP_2023/January/01-01-2022 #
# /02-01-2022 #
# /03-01-2022 #
# /**-**-**** #
# #
###############################################
cls
Write-Host "----------------------------------"
Write-Host "-------- BACKUPDATE-V1.2 ---------"
Write-Host "----------------------------------"
#$PSDefaultParameterValues['Out-File:Encoding'] = 'utf8'
################ Variables ####################
#Path to Backup
$BackPath = "B:\BACKUPS"## /!\ CHANGE ME /!\
###############################################
#Annual Folder
$year = (get-date).ToString("yyyy")
$checkpathyear = Test-Path $BackPath\Backup_$year
if( $checkpathyear -eq "True" )
{
Write-Host "Backup_$year exist"
}
else
{
Write-Host "New Folder : Backup_$year"
#Monthly Folder
New-Item -Force -Path "$BackPath\Backup_$year" -ItemType Directory
}
$month = (get-date).ToString("MMMM")
$checkpathmonth = Test-Path $BackPath\Backup_$year\$month
if( $checkpathmonth -eq "True" )
{
Write-Host "$month exist"
}
else
{
Write-Host "New Folder : $month"
$month = (get-date).ToString("MMMM")
New-Item -Force -Path "$BackPath\Backup_$year\$month" -ItemType Directory
}
#Dayly Folder
$dd = (get-date).ToString("dd-MM-yyyy")## /!\ CHANGE ME /!\ --> 01-01-2023
$checkpathday = Test-Path $BackPath\Backup_$year\$month\$dd
if( $checkpathday -eq "True" )
{
Write-Host "$dd exist"
}
else
{
Write-Host "New Folder : $dd "
New-Item -Force -Path "$BackPath\Backup_$year\$month\$dd" -ItemType Directory
}
##########################################################################
########################### START BACKUP ################################
#List of folders you want to backup
$Path1 = "YourFolder1" ## /!\ CHANGE ME /!\
$Path2 = "YourFolder2" ## /!\ CHANGE ME /!\
$Path3 = "YourFolder3" ## /!\ CHANGE ME /!\
$Path4 = "YourFolder4" ## /!\ CHANGE ME /!\
$Path5 = "YourFolder5" ## /!\ CHANGE ME /!\
$Path6 = "YourFolder6" ## /!\ CHANGE ME /!\
$Folder_to_Back = "$Path1","$Path2","$Path3","$Path4","$Path5","$Path6"
$x=0
while ($x -lt $Folder_to_Back.Count)
{
#Path to server
$Path_To_Server = "\\Your\Shared\Folder\On\Server\" ## /!\ CHANGE ME /!\
$Folder_To_Server = ""
#Path to Backup
$Path_To_Backup = "B:\BACKUPS\Backup_$year\$month\$dd\"
$Folder_To_Backup = $Folder_to_Back[$x]
Write-Host "------------------------------------------------------"
Write-Host "Backupdate to : " $Folder_to_Back[$x]
Write-Host "From : "$Path_To_Server$Folder_To_Backup
Write-Host "To : "$Path_To_Backup$Folder_To_Backup
Write-Host "------------------------------------------------------"
#Selection of folder and sub-folders
Write-Host "STEP 1 : Retrieving file names in $Path_To_Server$Folder_To_Backup"
$getfile4a = Get-ChildItem -Path $Path_To_Server$Folder_To_Backup -Recurse
#Selection of last modification time : example : -22 since 22H
$DatedeDerniereModif = (Get-date).AddHours(-24)## /!\ CHANGE ME /!\
#List of files to ignore compared to "$DatedeDerniereModif"
Write-Host "STEP 2 : Retrieving the date of last modification"
$oldfilelist4 = $getfile4a | Where-Object LastWriteTime -lt $DatedeDerniereModif
#New Backup.
Write-Host "STEP 3 : Creation of the backup folders"
New-Item -Force -Path "$BackPath\Backup_$year\$month\$dd\$Folder_To_Backup" -ItemType Directory
Write-Host "STEP 4 : Copying recently modified files and folders"
$output4 =
foreach ($File in $getfile4a)
{
if ($File.LastWriteTime -gt $DatedeDerniereModif)
{
Write-Output "$File <-----Copying"
}
else
{
#Nothing
}
}
Write-Host $output4
#Copying files/folders while excluding the list of files that haven't been modified in the past 24H.
Copy-Item -Path "$Path_To_Server$Folder_To_Backup\*" -Destination "$Path_To_Backup$Folder_To_Backup" -Exclude $oldfilelist4.Name -Recurse -Force
$x++
}
################################ END #####################################
##########################################################################
exit 0