/
tape
executable file
·300 lines (240 loc) · 8.99 KB
/
tape
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#!/usr/bin/env ruby
# frozen_string_literal: true
require 'fileutils'
require 'json'
require 'open3'
require 'pathname'
require 'tmpdir'
require 'yaml'
Script_path = Pathname.new($PROGRAM_NAME).expand_path
Script_name = Script_path.basename
# Check for minimum ruby version
abort "#{Script_name} requires Ruby 2.7 or higher" if RUBY_VERSION.to_f < 2.7
# Exit cleanup
Tmp_dir = Pathname.new(Dir.mktmpdir)
at_exit do Tmp_dir.rmtree end
# Helpers
def app_running?(bundle_id)
Open3.capture3(
'/usr/bin/osascript', '-l', 'JavaScript', '-e',
"function run(argv) { return Application(argv[0]).running() }", bundle_id
).first.strip == 'true'
end
def app_installed?(app_token)
definition = YAML.load_file(Definitions_dir.join("#{app_token}.yaml"))
bundle_ids = definition['id'].instance_of?(String) ? [definition['id']] : definition['id']
return true if bundle_ids&.any? { Open3.capture3('/usr/bin/defaults', 'read', _1).last.success? }
return true if definition['paths']&.any? { Pathname.new(_1).expand_path.exist? }
false
end
def copy_path(source, target)
return unless Pathname.new(source).exist? # If path does not exist, do not try to copy it
target.dirname.mkpath # Repeat directory structure of copied path
FileUtils.rm_rf(target) # Delete target path before trying to copy
FileUtils.cp_r(source, target)
end
def backup_app(app_token)
definition = YAML.load_file(Definitions_dir.join("#{app_token}.yaml"))
backup_dir = Tmp_dir.join(definition['name'])
bundle_ids = definition['id'].instance_of?(String) ? [definition['id']] : definition['id']
puts "Backing up #{definition['name']}…"
# Backup preferences
backup_dir.mkpath
bundle_ids&.each do
system('/usr/bin/defaults', 'export', _1, backup_dir.join("#{_1}.plist").to_path)
end
# Backup paths
definition['paths']&.each do
copy_path(Pathname.new(_1).expand_path, backup_dir.join(_1))
end
end
def restore_app(app_token)
definition = YAML.load_file(Definitions_dir.join("#{app_token}.yaml"))
backup_dir = Tmp_dir.join(definition['name'])
bundle_ids = definition['id'].instance_of?(String) ? [definition['id']] : definition['id']
puts "Restoring backup for #{definition['name']}…"
# Restore preferences
bundle_ids&.each do
system('/usr/bin/defaults', 'import', _1, backup_dir.join("#{_1}.plist").to_path)
end
# Restore paths
definition['paths']&.each do
copy_path(backup_dir.join(_1), Pathname.new(_1).expand_path)
end
# Warn if app is running
return unless bundle_ids&.any? { app_running?(_1) }
puts "#{definition['name']} is running. You may need to restart it for settings to take effect."
end
def update_definitions(force: false)
local_hash_file = Config_dir.join('latest_hash.txt')
# Skip if updated in the last hour, unless forced
return if !force && local_hash_file.exist? && (Time.now - local_hash_file.mtime) < 3600
puts 'Updating definitions…'
# Check if there have been any updates
local_hash = local_hash_file.exist? ? local_hash_file.read.chomp : nil
remote_hash = JSON.parse(Open3.capture2(
'/usr/bin/curl', '--silent', 'https://api.github.com/repos/vitorgalvao/tape/commits/main'
).first)['sha']
local_hash_file.write(remote_hash)
return if remote_hash == local_hash
# Update
Definitions_dir.rmtree if Definitions_dir.exist?
tmp_repo = Pathname.new(Dir.mktmpdir)
system('git', 'clone', '--quiet', 'https://github.com/vitorgalvao/tape.git', tmp_repo.to_path)
tmp_repo.join('Definitions').rename(Definitions_dir)
end
# Usage
def usage
puts <<~USAGE
Backup and restore software settings on macOS
Usage:
#{Script_name} backup Update definitions and backup settings
#{Script_name} restore <tgz> [def] Restore settings from previous backup
Giving a definition name restores only that software
#{Script_name} list Show supported software separated by what will be backed up
#{Script_name} launchd <on|off> Load or unload an agent to perform daily backups
#{Script_name} update Force update of backup definitions
#{Script_name} version Show #{Script_name} version
#{Script_name} help Show this help
USAGE
exit
end
usage if ARGV.empty? || ARGV.include?('help') || ARGV.include?('--help') || ARGV.include?('-h')
# Load config
Config_dir = Pathname.new(ENV['HOME']).join('.config', 'tape')
Config_file = Config_dir.join('config.yaml')
Default_config = {
'backup_to' => Config_dir.join('Backups').to_path, # Directory to save backups (leading "~" is expanded)
'keep' => 5, # Number of backups to keep
'exclude' => ['ssh'], # By default, backup everything except what is on this list
'include' => nil # If set, *only* these will be backed up and the "exclude" list will be ignored
}.freeze
unless Config_file.exist?
warn "No config file found. One with default settings was saved to #{Config_file}:\n#{Default_config.to_yaml}\n"
Config_dir.mkpath
Config_file.write(Default_config.to_yaml)
end
Config = YAML.load_file(Config_file)
abort '"keep" value in configuration needs to be a whole number bigger than 0' unless Config['keep'].positive?
# Collect software to backup or restore
Backups_dir = Pathname.new(Config['backup_to']).expand_path
Definitions_dir = Config_dir.join('Definitions')
# Launchd for automatic backups
Launchd_file = Pathname.new(ENV['HOME']).join('Library', 'LaunchAgents', 'com.vitorgalvao.tape.plist')
Launchd_contents = <<~LAUNCHD_PLIST
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.vitorgalvao.tape</string>
<key>EnvironmentVariables</key>
<dict>
<key>PATH</key>
<string>/opt/homebrew/opt/ruby/bin:/usr/local/opt/ruby/bin:/opt/homebrew/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin</string>
</dict>
<key>ProgramArguments</key>
<array>
<string>#{Script_path}</string>
<string>backup</string>
</array>
<key>StandardOutPath</key>
<string>/tmp/tape_backup.log</string>
<key>StandardErrorPath</key>
<string>/tmp/tape_backup.log</string>
<key>StartCalendarInterval</key>
<dict>
<key>Hour</key>
<integer>20</integer>
<key>Minute</key>
<integer>0</integer>
</dict>
</dict>
</plist>
LAUNCHD_PLIST
# Main
# Update definitions
update_definitions
All_apps = Definitions_dir
.children
.select { _1.extname == '.yaml' }
.sort
.map { _1.basename(_1.extname).to_path }
case ARGV[0]
when 'backup'
# Backup selected and valid apps
(Config['include'].nil? ? All_apps - Config['exclude'].to_a : Config['include'])
.select { app_installed?(_1) }
.each do backup_app(_1) end
# Archive
puts 'Archiving…'
Backups_dir.mkpath
system(
'/usr/bin/tar', '--gzip', '--create',
'--file', Backups_dir.join("#{Time.now.strftime('%Y-%m-%d')}.tgz").to_path,
'--directory', Tmp_dir.to_path, '.'
)
# Remove older backups
Backups_dir
.children
.select { _1.extname == '.tgz' }
.sort
.reverse
.drop(Config['keep'])
.each do _1.delete end
when 'restore'
abort 'Restore needs a path to a valid .tgz' if ARGV[1].nil? || Pathname.new(ARGV[1]).extname != '.tgz'
# Unarchive
system(
'/usr/bin/tar', '--extract',
'--file', ARGV[1],
'--directory', Tmp_dir.to_path
)
# Restore apps available in backup
Apps_to_restore = ARGV[2] ? [ARGV[2]] : All_apps
Apps_to_restore.each do |app_token|
begin
name = YAML.load_file(Definitions_dir.join("#{app_token}.yaml"))['name']
rescue Errno::ENOENT
abort "No defintion for #{app_token}"
end
next unless Tmp_dir.join(name).exist? # Skip if app directory does not exist in backup
restore_app(app_token)
end
when 'list'
included = (Config['include'].nil? ? All_apps - Config['exclude'].to_a : Config['include']).select { app_installed?(_1) }
excluded = All_apps - included
puts excluded.map { YAML.load_file(Definitions_dir.join("#{_1}.yaml"))['name'] }.map { "\e[31m✗\e[0m #{_1}" }.join("\n")
puts included.map { YAML.load_file(Definitions_dir.join("#{_1}.yaml"))['name'] }.map { "\e[32m✓\e[0m #{_1}" }.join("\n")
when 'launchd'
if ARGV[1] == 'on'
Launchd_file.dirname.mkpath
Launchd_file.write(Launchd_contents)
system(
'launchctl', 'bootstrap',
"gui/#{Open3.capture2('/usr/bin/id', '-u', ENV['USER']).first}",
Launchd_file.to_path
)
exit
end
if ARGV[1] == 'off'
system(
'launchctl', 'bootout',
"gui/#{Open3.capture2('/usr/bin/id', '-u', ENV['USER']).first}",
Launchd_file.to_path
)
Launchd_file.delete
exit
end
# If we reach this point, wrong argument was given
abort 'Launchd needs an argument of "on" or "off"'
when 'update'
update_definitions(force: true)
when 'version'
puts '2022.4'
else
abort <<~INVALID
Invalid argument. Try:
#{Script_path} help
INVALID
end