-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnapshot.sc
executable file
·50 lines (47 loc) · 1.71 KB
/
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
39
40
41
42
43
44
45
46
47
48
49
50
import java.io.File
import sys.process._
import java.util.Date
/**
Usage: java -jar .\ammX.X.X.jar .\snapshot.sc
*/
@main
def main(distro: String = "Debian", pathToSave: String = "C:\\Users\\UserName\\Documents\\WSLBackups") =
{
val realPathToSave = if(pathToSave.contains("UserName")) pathToSave.replace("UserName", sys.env("USERNAME")) else pathToSave
println(s"Generating snapshot for distro: $distro, at: $realPathToSave")
// Reference: https://www.howtogeek.com/426562/how-to-export-and-import-your-linux-systems-on-windows-10/
val targetFilename = s"$distro-${(new Date).toInstant.toString.replace(":","-").split('.')(0)}.tar"
val command = s"wsl.exe --export $distro \"$realPathToSave\\$targetFilename\""
println(s"Executing: $command")
command.!
// Make sure the snapshot tar file exists
if(new File(s"$realPathToSave\\$targetFilename").exists)
{
println(s"Successfully generated: \"$realPathToSave\\$targetFilename\"")
// Now gzip the archive
val sevenZExec = new File("C:\\Program Files\\7-Zip\\7z.exe")
if(sevenZExec.exists())
{
println(s"Creating gzip archive from ${targetFilename}...")
// Use 7z format since that happens in a parallel manner.
val output = s"${realPathToSave}\\${targetFilename}.7z"
val archiveCmd = s"$sevenZExec a -t7z \"$output\" \"$realPathToSave\\$targetFilename\""
println(s"Executing: $archiveCmd")
try
{
archiveCmd.!
println(s"Snapshot complete! Output file: [$output]")
}
catch
{
case e: Exception => println(s"Could not create archive. Error: ${e.getMessage}")
}
}
else println(s"Error: Couldn't locate 7z at [$sevenZExec]!")
}
else
{
println(s"Error: snapshot file $targetFilename has not been generated!")
System.exit(-1)
}
}