Project

General

Profile

script to backup your Redmine database and files folder f... » redmineBackup.ps1

Alexandre Jobin, 2010-05-26 17:36

 
1
####--------------------------------------------------------------------------------
2
#### SVN Repository Backup 
3
#### Author: James Coffman
4
#### Date Created: 6/16/2009
5
#### -------------------------------------------------------------------------------
6
#### Updates: 
7
####  2009/06/23 - modified zip function and added wait at the end to help make sure
8
####               compression has time to complete.
9
#### -------------------------------------------------------------------------------
10
#### Disclaimer: !!!! Use at your own risk. !!!!
11
#### 
12
#### This Script will use svnadmin to hotcopy every repository in your 
13
#### repositories directory. For example I have all of my repositories 
14
#### in the directory ..\SVN\Repositories. This script assumes each directory 
15
#### under the main directory is a repository and will attempt to back it up.
16
#### It will zip the backedup dir and copy it to a directory named after the repository 
17
#### under the backup path. If the directory doesnt exist will be created for you.
18
#### If you set the DaysToKeepBackups variable to something > 0 then this script
19
#### will delete the backup zipfiles older that the # of days you set.
20
#### 1) You need to have svnadmin.exe accessible.
21
#### 2) Set your paths in the global vars below
22
####--------------------------------------------------------------------------------
23

    
24

    
25
# Set up Global variables needed by script
26
$mySqlDatabaseName = "databasename" 
27
$mySqlUser = "user"
28
$mySqlPassword = "password"
29
$redmineFileFolderPath = "D:\Program Files\Redmine\files"
30
$backupPath = "d:\dump"     
31
$mySqlDumpExe = "D:\Program Files\MySQL\MySQL Server 5.0\bin\mysqldump.exe"
32
$DaysToKeepBackups = 60
33
$sz = "$env:ProgramFiles\7-Zip\7z.exe"
34

    
35
#*************************************************************************
36
#                          F U N C T I O N S 
37
#*************************************************************************
38

    
39
#------------------------------------------------------------------------#
40
#  Create a new temp Directory
41
#------------------------------------------------------------------------#
42
function CreateTempDir ([string]$name)
43
{
44
    #create a dir in the system temp dir for svn to copy the repository into
45
	#before zipping it
46
	
47
	$newDir = "Not Found"
48
	
49
	$tempCopyPath = [System.IO.Path]::GetTempPath().ToString() + $name
50

    
51
    #delete it first if it exists
52
	if ( [System.IO.Directory]::Exists($tempCopyPath) )
53
	{ 
54
		Remove-Item -path $tempCopyPath -recurse -force
55
		##[System.IO.Directory]::Delete($repoTempCopyPath, $true)
56
	}
57

    
58
    #then create it
59
	if( ![System.IO.Directory]::Exists($tempCopyPath)) 
60
	{ 
61
		$newDir = [System.IO.Directory]::CreateDirectory($tempCopyPath)
62
	}
63
	
64
	$newDir.FullName
65
}
66

    
67
#------------------------------------------------------------------------#
68
#  Run SVN Backup 
69
#------------------------------------------------------------------------#
70
function RunMySqlDump ([string]$user, [string]$password, [string]$databaseName, [string]$sqlName)
71
{
72
    $currentOutputEncoding = [Console]::OutputEncoding   
73
    [Console]::OutputEncoding = [Text.Encoding]::UTF8
74
    
75
	& $mySqlDumpExe --user=$user --password=$password $databaseName | Out-File "$sqlName" -encoding UTF8
76
    
77
    [Console]::OutputEncoding = $currentOutputEncoding
78
}
79

    
80
#------------------------------------------------------------------------#
81
#  Zip a whole Directory
82
#------------------------------------------------------------------------#
83
function ZipDir ([string]$_dirToZip, [object]$_zipName)
84
{
85
    #add zip extension if not present
86
	if (-not $_zipName.EndsWith(".zip")) {$_zipName += ".zip"} 
87

    
88
    #make sure directory to zip exists
89
	if (test-path $_dirToZip)
90
	{
91
    	#make sure zip file doesnt already exist
92
		if (-not (test-path $_zipName)) 
93
		{ 
94
			& $sz a -tzip "$_zipName" "$_dirToZip"
95
		}
96
	}
97
	else 
98
	{
99
		"ERROR: Path does not exist -- $_dirToZip"
100
	}
101
}
102

    
103
#------------------------------------------------------------------------#
104
#  Clean Backup Dirs
105
#------------------------------------------------------------------------#
106
function CleanBackupDirs ([string]$backupPathtoClean, [int]$daysToKeep)
107
{
108
	" ... cleaning $backupPathtoClean"
109
	foreach ($backupFile in Get-ChildItem -Path $backupPathtoClean) 
110
	{
111
		if ( Test-Path $backupFile.FullName )
112
		{ 
113
			$y = ((Get-Date) - $backupFile.CreationTime).Days
114
        	if ($y -gt $daysToKeep -and $backupFile.PsISContainer -ne $True)
115
            {
116
				" ...... Deleting $backupFile : " + $backupFile.CreationTime			
117
				$backupFile.Delete()
118
			}
119
			else
120
			{
121
				" ...... Keeping $backupFile : " + $backupFile.CreationTime			
122
			}
123
		}
124
	}
125
}
126

    
127
#*************************************************************************
128
#                      MAIN LOGIC BEGINS HERE
129
#*************************************************************************
130

    
131

    
132
if (-not (test-path "$sz"))
133
{
134
   "7z est manquant"
135
   exit(-1)
136
}
137

    
138

    
139
"*******************************************************"
140
"* BEGIN: Backing Up Redmine"
141
"*******************************************************"
142
"* Params: "
143
"* The Database Name is: $mySqlDatabaseName"
144
"* The Redmine File Folder is: $redmineFileFolderPath"
145
"* The Backup Path is: $backupPath"
146
$temp = [System.IO.Path]::GetTempPath().ToString()
147
"* The Temp Directory is: $temp" 
148
"* "
149
"*------------------------------------------------------"
150

    
151
""
152
" Processing backup for: $mySqlDatabase"
153
$tempDir = ""
154
" ... Creating Temp Directory"
155
$tempDir = CreateTempDir $mySqlDatabaseName
156
" ... Running MySqlDump"
157
$tempFileName = $tempDir + "\" + $mySqlDatabaseName + ".sql"
158
RunMySqlDump $mySqlUser $mySqlPassword $mySqlDatabaseName $tempFileName
159

    
160
" ... Copying Redmine files"
161
Copy-Item $redmineFileFolderPath $tempDir -recurse
162

    
163

    
164
$newBackupPath = ""
165
# Create a Folder in the backup path for each repository if needed
166
if($backupPath.EndsWith("\"))
167
{
168
	$newBackupPath = $backupPath + $mySqlDatabaseName	
169
}
170
else
171
{
172
	$newBackupPath = $backupPath + "\" + $mySqlDatabaseName	
173
}
174
if( ![System.IO.Directory]::Exists($newBackupPath)) 
175
{ 
176
	" ... Creating Backup Directory $newBackupPath"
177
	[System.IO.Directory]::CreateDirectory($newBackupPath)
178
}
179
if(!$newBackupPath.EndsWith("\")){$newBackupPath = $newBackupPath + "\"}
180

    
181
# Zip the the backup into a zip file with datetime stamp
182
$timeStamp = Get-Date -uformat "%Y_%m_%d_%H%M%S"
183
$zipNamePath = $newBackupPath + $mySqlDatabaseName + "_" + $timeStamp + ".zip"
184
" ... Zipping Repository Backup to $zipNamePath"
185
ZipDir $tempDir $zipNamePath 
186

    
187
# next step is to clean old zipped backups
188
if ($DaysToKeepBackups -gt 0)
189
{
190
	CleanBackupDirs $newBackupPath $DaysToKeepBackups
191
}
192

    
193

    
194

    
195
"*******************************************************"
196
"* END: Redmine Archived"
197
"*******************************************************"
198

    
199
Exit(0)
    (1-1/1)