-
Notifications
You must be signed in to change notification settings - Fork 0
/
restore-snapshot.sc
executable file
·38 lines (36 loc) · 1.65 KB
/
restore-snapshot.sc
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
import java.io.File
import sys.process._
@main
def main(distro: String = "Debian", pathToRestore: String = "C:\\Users\\UserName\\Documents\\WSLRoot", defaultUser: String, snapshotPath: String) =
{
val realPathToRestore = if(pathToRestore.contains("UserName")) pathToRestore.replace("UserName", sys.env("USERNAME")) else pathToRestore
println(s"Restoring snapshot: [$distro] from [$snapshotPath] to [$realPathToRestore]")
// Reference: https://www.howtogeek.com/426562/how-to-export-and-import-your-linux-systems-on-windows-10/
// First check if the file is gzipped
var tarFileName = snapshotPath
if(snapshotPath.endsWith(".7z"))
{
// We need to uncompress the file
val sevenZExec = new File("C:\\Program Files\\7-Zip\\7z.exe")
if(sevenZExec.exists())
{
val archiveCmd = s"$sevenZExec e \".\\$snapshotPath\""
println(s"Uncompressing: $snapshotPath")
archiveCmd.!
// Set the tar filename
tarFileName = snapshotPath.stripSuffix(".7z")
}
else println(s"Couldn't locate 7z at [$sevenZExec]!")
}
// First, unregister the distro
println(s"Executing: wsl.exe --unregister $distro")
s"Executing: wsl.exe --unregister $distro".!
// Next, restore the distro
println(s"Executing: wsl.exe --import $distro $realPathToRestore $tarFileName")
s"wsl.exe --import $distro $realPathToRestore $snapshotPath".!
// Next set the default user, otherwise default user is root
println(s"Setting default user for distro: $defaultUser")
println(s"Executing: ${distro.toLowercase}.exe config --default-user $defaultUser")
s"${distro.toLowercase}.exe config --default-user $defaultUser".!
println("Snapshot restoration complete.")
}