Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backup script for Windows #512

Merged
merged 4 commits into from Dec 14, 2017
Merged

Backup script for Windows #512

merged 4 commits into from Dec 14, 2017

Conversation

@bdleedy
Copy link
Contributor

@bdleedy bdleedy commented Aug 7, 2017

This script will backup the userdata (except unnecessary files) and conf
folders into a .zip file.

For #499

Signed-off-by: Brian Leedy awsnap@gmail.com

bdleedy added 3 commits Aug 7, 2017
This script will backup the userdata (except unnecessary files) and conf
folders into a .zip file.

Signed-off-by: Brian Leedy <awsnap@gmail.com>
Signed-off-by: Brian Leedy <awsnap@gmail.com>
Signed-off-by: Brian Leedy <awsnap@gmail.com>
@BClark09
Copy link
Member

@BClark09 BClark09 commented Sep 20, 2017

Hi @bdleedy, I'm a PowerScript novice. But wanted to test out the script today to see if it produces the same zip as the Linux version, how do I use the file? Running it by right-click doesn't seem to do anything?

@DarkLite1
Copy link

@DarkLite1 DarkLite1 commented Sep 23, 2017

@BClark09 it tells you in the well documented .EXAMPLE section how to use it:

+    .EXAMPLE
 +    Backup an openHAB instance to a zip file
 +    Backup-openHAB
 +    .EXAMPLE
 +    Backup the openHAB distribution in the C:\openHAB2 directory to c:\openhab-backup\backup.zip
 +    Backup-openHAB -OHDirectory C:\openHAB2 -OHBackups c:\openHAB-backup -ZipFileOut backup.zip

If you are on Windows box it should be possible to load the function first and then you can use it as in the example. Something like this would do:

# Loads the function
& 'C:\Backup.ps1'

# Executes the function
Backup-openHAB

@BClark09
Copy link
Member

@BClark09 BClark09 commented Sep 23, 2017

Thank you @DarkLite1, unfortunately I could not get it to work.

image

The backup folder is set as .\backups, however it looks like the error references the backup folder to be in system32!? I'm assuming these directories should probably be expanded to a full path first.

@bdleedy
Copy link
Contributor Author

@bdleedy bdleedy commented Sep 23, 2017

@DarkLite1
Copy link

@DarkLite1 DarkLite1 commented Sep 24, 2017

You probably already know this, but I feel I need to say it anyhow. To have this work on all Windows machines you need to use PowerShell.exe -ExecutionPolicy Bypass -Command {MyCode}.

@kaikreuzer
Copy link
Member

@kaikreuzer kaikreuzer commented Dec 10, 2017

@bdleedy Are you still around? As we are heading towards the 2.2 by the end of this week, is there any chance to finish this off?

@bdleedy
Copy link
Contributor Author

@bdleedy bdleedy commented Dec 10, 2017

@tmrobert8
Copy link
Contributor

@tmrobert8 tmrobert8 commented Dec 12, 2017

@bdleedy
Let me know if you want any help on testing it (or the update.ps1) - I can certainly take the time to test.

@bdleedy
Copy link
Contributor Author

@bdleedy bdleedy commented Dec 13, 2017

Seems like the CreateFromDirectory doesn't like .\ so I handled that and used pwd to grab the current directory.

@tmrobert8 et al, this needs a quick test also.

@DarkLite1
Copy link

@DarkLite1 DarkLite1 commented Dec 13, 2017

When we create production code in PowerShell we usually create a Pester file full of tests. It could maybe be an idea to add this too. Makes updating easier in the future.

Copy link
Member

@BClark09 BClark09 left a comment

I can't comment on efficiency/safety of the code. (@DarkLite1 are you able to comment?) but I can say that it fails to include backup.properties to the zipfile. Implementing my comments seems to resolve the issue and a good zip file is created.

$CurrentVersionIndex = $VersionLine.IndexOf(":")
$CurrentVersion = $VersionLine.Substring($currentVersionIndex + 2)
$timestamp = Get-Date -Format yyyyMMddHHmm
Write-Output "version=$CurrentVersion" | Set-Content "$TempDir\backup.properties"
Copy link
Member

@BClark09 BClark09 Dec 13, 2017

$TempDir hasn't been set yet, so this writes backup.properties to the C:\\ root.

$timestamp = Get-Date -Format yyyyMMddHHmm
Write-Output "version=$CurrentVersion" | Set-Content "$TempDir\backup.properties"
Write-Output "timestamp=$timestamp" | Add-Content "$TempDir\backup.properties"
#Write-Output "user=$OHUser" | Add-Content "$TempDir\backup.properties"
Copy link
Member

@BClark09 BClark09 Dec 13, 2017

Setting this to something like "windows" would be better for cross platform compatibility.

Copy link
Contributor Author

@bdleedy bdleedy Dec 13, 2017

What is the 'this' you are referring to? The directory, property, file, ...?

Copy link
Member

@BClark09 BClark09 Dec 13, 2017

Apologies, I assume you commented out this line because OHUser and OHGroup is blank? If you set to this to the default: user=openhab, group=openhab then it would be clearer to the restore scripts on other operating systems.

Copy link
Contributor Author

@bdleedy bdleedy Dec 13, 2017

Yeah, in Windows there isn't a real reason to grab that info as there is in your case so I just left if commented out in case someone thought we needed something there. I'll just hardcode 'openhab' for both then. Check that. openHAB.

Copy link
Member

@BClark09 BClark09 Dec 13, 2017

Case is important in unix/macos, so please let it be 'openhab'. The idea here is to be able to use a backup from Windows, on a restore to Unix, and visa versa.

Write-Output "version=$CurrentVersion" | Set-Content "$TempDir\backup.properties"
Write-Output "timestamp=$timestamp" | Add-Content "$TempDir\backup.properties"
#Write-Output "user=$OHUser" | Add-Content "$TempDir\backup.properties"
#Write-Output "group=$OHGroup" | Add-Content "$TempDir\backup.properties"
Copy link
Member

@BClark09 BClark09 Dec 13, 2017

Same as above.

#Write-Output "group=$OHGroup" | Add-Content "$TempDir\backup.properties"

Write-Host -ForegroundColor Cyan "Copying userdata and conf folder contents to temp directory"
$TempDir=([Environment]::GetEnvironmentVariable("TEMP", "Machine"))+"\openhab"
Copy link
Member

@BClark09 BClark09 Dec 13, 2017

These two lines (82 and 83) have to be moved before L76 and L77 for the script to work properly.

Copy link
Contributor Author

@bdleedy bdleedy Dec 13, 2017

Fixed and ready to go, just waiting on your clarification above.

@DarkLite1
Copy link

@DarkLite1 DarkLite1 commented Dec 13, 2017

Sorry, no time for the moment. Packing to leave on a vacation trip tomorrow..

Fixed issue with CreateFromDirectory using .\ relative path
Fixed synopsis
Moved $TempDir creation up, added default user and group of openHAB to
backup.properties
Changed openHAB to openhab in backup.properties

Signed-off-by: Brian Leedy <awsnap@gmail.com>
@tmrobert8
Copy link
Contributor

@tmrobert8 tmrobert8 commented Dec 14, 2017

@bdleedy
This worked fine for me - great job! Are you going to wrap both of these in an easily executed .bat file (I have .bat wrappers around both right now - pretty simple to create).

Copy link
Member

@BClark09 BClark09 left a comment

Excellent, thats working well! Thanks @bdleedy!

@martinvw
Copy link
Member

@martinvw martinvw commented Dec 14, 2017

Hooray thanks guys!

@kaikreuzer
Copy link
Member

@kaikreuzer kaikreuzer commented Dec 14, 2017

Awesome!

@kaikreuzer kaikreuzer merged commit 70392f4 into openhab:master Dec 14, 2017
1 check passed
@bdleedy
Copy link
Contributor Author

@bdleedy bdleedy commented Dec 14, 2017

@tmrobert8 I kind of despise .bat wrappers a little but I think it makes sense in this case and easier for 'Joe Automator' to use. I can do a PR or feel free to do it yourself. I need to work on the update script still and I'm making progress on the restore script but behind on that. Also, the documentation needs some work as well.

@kaikreuzer kaikreuzer changed the title Add backup.ps1 script for Windows Added backup script for Windows Dec 15, 2017
@kaikreuzer kaikreuzer added this to the 2.2 milestone Dec 15, 2017
@kaikreuzer kaikreuzer changed the title Added backup script for Windows Backup script for Windows Dec 15, 2017
@kaikreuzer kaikreuzer removed this from the 2.2 milestone Dec 15, 2017
@kaikreuzer kaikreuzer added this to the 2.2 milestone Dec 15, 2017
@bdleedy bdleedy deleted the backup-ps1 branch Dec 16, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

6 participants