· 7 years ago · Sep 23, 2018, 11:54 AM
1# This script creates backups of SLMS and Wiki.
2# It containces 3 functions:
3# create_backup() - creates a backup of the passed as param DB using "mysqldump.exe"
4# and upload it to the S3 bucket.
5# Also it checks an age of the backup and deletes the old one.
6# rm_s3_object() - checks an age of the backups on S3 and deletes them. You can pass to it retention time.
7# upload_courses() - just upload courses from the passed folder to S3
8
9Param (
10[string]$BucketName = "bucket_name",
11
12[int32]$RetentionTime = 604800*4 # 604800 = 1 week
13)
14
15$CurrentDate = Get-Date # get local system time
16$AccessKey = "your_access_key"
17$SecretKey = "your_secret_key"
18
19
20#### Functions #####
21
22## Delete backups from s3 ###
23function rm_s3_object([string]$key,[int32]$RetentionTime) { # Gets $key bucketname/key - folder inside of a bucket
24
25 # Get objects from the pointed bucket
26 $getObjects = Get-S3Object -BucketName $BucketName -AccessKey $AccessKey -SecretKey $SecretKey -Key $key
27 # Remove objects from Bucket.
28 foreach ($_ in $getObjects){
29
30 # The "ToFileTime()" method converts string output got previously to nanoseconds;
31 # 1 and seven "0" convert nanoseconds to seconds; 'split' cuts unnecessory digits agter point "."
32 $ObjectAge = ($CurrentDate.ToFileTime()/10000000 -split "\.")[0] - ($_.LastModified.ToFileTime()/10000000 -split "\.")[0]
33
34 # If the object is older then RetTime then delete it
35 if($ObjectAge -gt $RetentionTime){
36
37 Remove-S3Object -BucketName $BucketName -AccessKey $AccessKey -SecretKey $SecretKey $_.Key -Force | out-null # Delete object from a bucket
38 write-host "The object "$_.Key" is older then $($RetentionTime/86400) days and has been deleted"
39
40 }
41 else {
42 write-host "Nothing to do. The" $_.Key "object isn't older then $($RetentionTime/86400) days."
43 }
44 }
45}
46########################
47
48### Create backups ###
49function create_backup([string]$dbname,[string]$path,[string]$key){ # Gets three params - a name of the backuping DB, a path to the DB and a key - folder inside of bucket
50 # Get current date
51 $d = (Get-Date).ToString('ddMMyy')
52
53 # Run backup
54 C:\xampp\mysql\bin\mysqldump.exe -u root -pPassword $dbname > "$($path)$($dbname)$("_$d.sql")"
55
56 # Upload files
57 Write-S3Object -BucketName $BucketName -Key "$($key)$($dbname)$("_$d.sql")" -AccessKey $AccessKey -SecretKey $SecretKey -File "$($path)$($dbname)$("_$d.sql")"
58 Write-Host "file " "$($dbname)$("_$d.sql")" "has been uploaded"
59
60
61 # Remove the backups oldest then 1 month
62
63 $files = Get-ChildItem -Path $path | select Name, CreationTime | sort CreationTime -Descending
64
65 foreach ($_ in $files){
66
67 $fileage = (($CurrentDate.ToFileTime()/10000000 -split "\.")[0] - ($_.CreationTime.ToFileTime()/10000000 -split "\.")[0])
68
69 if($fileage -ge $RetentionTime){
70
71 #cd $path
72 write-host "The file" $_.Name "will be deleted!" -ForegroundColor DarkYellow
73 rm "$($path)$($_.Name)" -Force
74
75 }
76 }
77
78}
79#########################
80
81### Upload Courses ###
82function upload_courses([string]$path,[string]$key){
83
84 $files = Get-ChildItem -Path $path | select Name
85
86 foreach ($_ in $files) {
87 Write-S3Object -BucketName $BucketName -Key "$($key)$($_.Name)" -AccessKey $AccessKey -SecretKey $SecretKey -File "$($path)$($_.Name)"
88 Write-Host "file " $($_.Name) "has been uploaded"
89 }
90
91}
92
93
94
95################## Call of Functions ###################
96
97# Create backups
98create_backup 'wikidb' 'C:\Backups\wiki\backups_DBs(auto)\' 'wiki_backup/'
99create_backup 'moodle' 'C:\Backups\moodle\backups_DBs(auto)\' 'slms_backup/'
100
101# Remove objects from Buckes
102rm_s3_object "wiki_backup/wiki" "$RetentionTime" ## 604800*4 = 1 month in seconds
103rm_s3_object "slms_backup/moodle" "$RetentionTime"
104rm_s3_object "courses_backup/backup" "$(432000)" ## 432000 = 5 days in seconds
105
106####### Upload Courses #######
107backup_cources 'c:\Backups\moodle\backups_of_courses_only(auto)\' 'courses_backup/'