~aleteoryx/muditaos

ref: 310befd13727aaf64ab4933c35d07b0c0bcfda21 muditaos/scripts/lua/backup.lua -rw-r--r-- 1.8 KiB
310befd1 — Lukasz Mastalerz [BH-1769] Incorrect message after setting the alarm with deep press 2 years ago
                                                                                
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
local helpers = require('helpers')
local recovery = require('recovery')
local paths = require('paths')
local tar = require('ltar')
local lfs = require('lfs')
local backup = {}

-- Match only files with '.db' extensions and omit such files inside subdirectories
local match_db_files = '^[^%/]*%.db$'

backup.script_name = "backup.lua"
backup.img_in_progress = "assets/gui_image_backup_in_progress.bin"
backup.img_success = "assets/gui_image_backup_success.bin"
backup.img_failure = "assets/gui_image_backup_failed.bin"

local function check_available_space()
    local db_size = helpers.dir_size_filtered(paths.db_dir, match_db_files)
    local version_size = lfs.attributes(paths.version_file, 'size')
    local available_space = recovery.sys.free_space(recovery.sys.user())
    -- Multiply the result by two due to the internal padding inside tar
    local required_space = (db_size + version_size) * 2

    print(string.format("Checking disk space:\nNeeded space: %d bytes, available space: %d bytes", required_space,
        available_space))

    assert(available_space > required_space, "Not enough free space on user disk")
end

local function create_temp_dir()
    if not helpers.exists(paths.temp_dir) then
        print(string.format("'%s' does not exist, creating", paths.temp_dir))
        lfs.mkdir(paths.temp_dir)
    end
end

local function create_backup_package()
    print(string.format("Creating backup package from the contents of '%s' directory and saving it into '%s'",
        paths.db_dir, paths.backup_file))
    tar.create_from_path_regex(paths.db_dir, paths.backup_file, match_db_files)
    tar.append_file(paths.version_file, paths.backup_file)
end

function backup.execute()
    check_available_space()
    create_temp_dir()
    create_backup_package()
end

return backup