🌳
pt0/deployF/servicesF/minecraftF/actionsF.mts
14const mbpMcSavesDir = pathDownJoin(envHome!, `/Library/Application Support/minecraft/saves`)
15const mbpMcSavesDirDisplay = `~/Library/Application Support/minecraft/saves`
17const worldManifestCmd = ['sh', '-c', 'cd /data/world && find . -type f -printf "%P %s %T@\\n" | sort | md5sum']
19export const mcbackup = async (lastRunData?: {worldHash?: string}) => {
20 const {name} = getAppCfg()
22 const {cluster_name} = getReqKlusterCtx()
24 const podInfo = await getSinglePod({labels: {name}, cluster_name})
25 assertDefined(podInfo)
26 const {podName, containerName} = podInfo
28 const worldHash = await doKubeExecCapture({cmdA: worldManifestCmd, podName, containerName, isQuiet: true})
29 if (lastRunData?.worldHash && worldHash === lastRunData.worldHash) {
30 console.log(chalkGray(`world unchanged since last backup, skipping download`))
31 return lastRunData
32 }
34 const localWorldName = luxNow().toFormat('yyLLdd')
36 await kubeCpDown({podName, containerName, fromRemotePath: '/data/world/',
37 toLocalPath: pathDownJoin(mbpMcSavesDir, localWorldName)
38 })
39 console.log(chalkGreen(`world backed up (hash changed)`))
40 return {worldHash}
42mcbackup.cliDescript = `backup world to ${mbpMcSavesDirDisplay}`
44export const mcrestore = async () => {
45 const {name} = getAppCfg()
47 const {cluster_name} = getReqKlusterCtx()
49 const {readdirSync} = await import('fs')
50 const backupDirs = readdirSync(mbpMcSavesDir, {withFileTypes: true})
51 .filter(dirent => dirent.isDirectory())
52 .map(dirent => dirent.name)
53 .filter(name => /^\d{6}$/.test(name))
54 .sort()
55 .reverse()
57 assertNonEmpty(backupDirs)
59 const mostRecentBackup = backupDirs[0]
60 console.log(`Restoring from backup: ${mostRecentBackup}`)
62 const podInfo = await getSinglePod({labels: {name}, cluster_name})
63 assertDefined(podInfo)
64 const {podName, containerName} = podInfo
66 await kubeCpUp({
67 podName,
68 containerName,
69 fromLocalPath: `${mbpMcSavesDir}/${mostRecentBackup}/.`,
70 toRemotePath: '/data/world'
71 })
73 await eptKubeCli([cluster_name, 'delete', 'pod', podName])
75 console.log('Restore complete!')
77mcrestore.cliDescript = `restore world from ${mbpMcSavesDirDisplay}`