~aleteoryx/backupper

ref: 1db63d22aa8b009cfd76e7acbca74dd9a87f23e3 backupper/backupper.tcl -rwxr-xr-x 4.8 KiB
1db63d22Aleteoryx importing 9 months 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#!/bin/env tclsh

package require sha256
package require sqlite3

proc readbak {path} {
  set fd [open $path rb]
  set magic [read $fd 6]
  if {$magic != "AMEBAK"} {
    close $fd
    return -code error "invalid magic string: \"$magic\""
  }

  set ver [read $fd 1]
  binary scan $ver c ver
  if {$ver != 0} {
    close $fd
    return -code error "archive file too new: $ver (self: 0)"
  }

  set mode [read $fd 1]
  read $fd 8; # skip filesize
  switch -- $mode {
    r {}
    F {
      zlib push inflate $fd -level 9
    }
    default {
      close $fd
      return -code error "unknown compression mode: $mode"
    }
  }

  set ret [read $fd]
  close $fd
  return $ret
}
proc readstore {store hash {hex 0}} {
  if {!$hex} { set hexhash [binary encode hex $hash] }
  set path $store
  append path "/[string range $hexhash 0 1]"
  append path "/[string range $hexhash 2 3]"
  append path "/[string range $hexhash 4 5]/"
  append path "/[string range $hexhash 6 end]"
  if {[file exists $path]} {
    return [readbak $path]
  } else {
    return -code error "missing file"
  }
}

proc writebak {path data} {
  binary scan $data c _
  set bench [string range $data 0 10000000]
  binary scan $bench c _
  set Fbench [zlib deflate $bench 9]

  set fd [open $path wb]
  puts -nonewline $fd "AMEBAK\0"

  set compress 0
  if {[string length $Fbench] < ([string length $bench] * 95 / 100)} {
    puts -nonewline $fd "F"
    set compress 1
  } else { puts -nonewline $fd "r" }

  puts -nonewline $fd [binary format W [string length $data]]

  if {$compress} { zlib push deflate $fd -level 9 }

  puts -nonewline $fd $data
  close $fd
}
proc writestore {store data {hex 0}} {
  set hash [::sha2::sha256 -bin -- $data]
  set hexhash [binary encode hex $hash]
  set path $store
  append path "/[string range $hexhash 0 1]"
  append path "/[string range $hexhash 2 3]"
  append path "/[string range $hexhash 4 5]/"
  file mkdir $path
  set path "$path/[string range $hexhash 6 end]"

  if {![file exists $path]} {
    writebak $path $data
  }

  if {$hex} {
    return $hexhash
  } else {
    return $hash
  }
}

proc statbak {path} {
  set fd [open $path rb]
  set magic [read $fd 6]
  if {$magic != "AMEBAK"} {
    close $fd
    return -code error "invalid magic string: \"$magic\""
  }

  set ver [read $fd 1]
  binary scan $ver c ver
  if {$ver != 0} {
    close $fd
    return -code error "archive file too new: $ver (self: 0)"
  }

  set mode [read $fd 1]
  switch -- $mode {
    r { set mode raw }
    F { set mode deflate }
    default {
      close $fd
      return -code error "unknown compression mode: $mode"
    }
  }

  set disksize [file size $path]
  binary scan [read $fd 8] W datasize

  close $fd
  return [list ver $ver mode $mode disksize $disksize datasize $datasize]
}
proc statstore {store hash {hex 0}} {
  if {$$hex} { set hexhash [binary encode hex $hash] }
  set path $store
  append path "/[string range $hexhash 0 1]"
  append path "/[string range $hexhash 2 3]"
  append path "/[string range $hexhash 4 5]/"
  append path "/[string range $hexhash 6 end]"
  if {![file exists $path]} return

  statbak $path
}

proc usage {} {
  global argv0
  puts stderr "usage: $argv0 command store ?args ...?"
  puts stderr "commands:"
  puts stderr "  init - initialize a store in store"
  puts stderr "  importlist ?listfile? - if listfile is ommitted, reads stdin"
  exit -1
}
proc die {reason} {
  global argv0
  puts stderr "$argv0: fatal: $reason"
  exit -2
}


if {[llength $argv] < 2} usage

set argv [lassign $argv command store]
set dbpath "$store/index.db"
set arpath "$store/archive"

if {$command == "init"} {
  if {[file exists $dbpath]} { die "won't overwrite \"$dbpath\"." }
  file mkdir $store
  file mkdir $arpath
  sqlite3 db $dbpath

  db eval {
    CREATE TABLE tarballs (name TEXT NOT NULL, type TEXT NOT NULL, ref TEXT NOT NULL, desc TEXT NOT NULL, date TEXT NOT NULL);
  }
  exit
}

if {[catch {sqlite3 db $dbpath -create false}]} { die "can't find a store at \"$store\"." }

switch -- $command {
  importlist {
    if {[llength $argv] > 1} usage
    if {[llength $argv] == 1} {
      set fd [open [lindex $argv 0]]
      set fname [lindex $argv 0]
    } else {
      set fd stdin
      set fname "pipeline"
    }

    set files [split [string trim [read $fd]] "\n"]
    set filelist {}
    close $fd

    foreach filename $files {
      if {![file exists $filename]} {
        puts stderr "warning: \"$filename\" doesn't exist"
        continue
      }

      set fd [open $filename rb]
      set data [read $fd]
      close $fd

      set hash [writestore $arpath $data 1]
      lappend filelist [string trimleft $filename "/"] $hash
    }

    set ref [writestore $arpath $filelist 1]
    set date [clock seconds]
    set rowid [db eval {
      INSERT INTO tarballs VALUES ($fname, "list", $ref, "", $date) RETURNING rowid;
    }]

    puts stderr "inserted as archive $rowid"
  }
}