-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbackup_just.rb
executable file
·131 lines (94 loc) · 2.7 KB
/
backup_just.rb
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
#!/usr/bin/env ruby
# encoding: utf-8
# frozen_string_literal: true
# warn_indent: true
##########################################################
###
## File: backup_just.rb
## Desc: Find all *.just, *.justfile and */justfile files and copy them
## to ~/.just_backup directory
## By: Dewayne VanHoozer ([email protected])
#
# NOTE: Uses 'mdfind' which is a MacOS-only tool ???
#
require 'date'
require 'pathname'
require 'tempfile'
HOME = Pathname.new(ENV['HOME'])
BACKUP_DIR = HOME + '.just_backup'
LAST_TIME_PATH = BACKUP_DIR + 'last_backup_timestamp.txt'
home_string = HOME.to_s
BACKUP_DIR_STRING = BACKUP_DIR.to_s
unless BACKUP_DIR.exist?
BACKUP_DIR.mkdir
end
unless BACKUP_DIR.directory?
puts "\nERROR: Not a Directory ~/#{BACKUP_DIR.basename}"
exit(1)
end
# Handle the case where LAST_TIME_PATH doesn't exist
if LAST_TIME_PATH.exist?
LAST_BACKUP_TIME = LAST_TIME_PATH.mtime
else
# If the file doesn't exist, use a default time (e.g., 24 hours ago)
LAST_BACKUP_TIME = Time.now - 86400*365 # set the time to last year
puts "No previous backup timestamp found. Using default: #{LAST_BACKUP_TIME}"
end
require 'amazing_print'
require 'debug_me'
include DebugMe
######################################################
# Local methods
######################################################
# Main
at_exit do
puts
puts "Done."
puts
end
puts
puts "Last backup was: #{LAST_BACKUP_TIME}"
puts
puts "Finding all the files modified since then ... "
# Define a method to filter files with specific extensions
def filter_files(tempfile_path)
File.readlines(tempfile_path)
.reject{|line| line.include?(BACKUP_DIR_STRING) }
.map{|line| line.strip}
.select{|line|
line.end_with?('.just') ||
line.end_with?('justfile')
}
.map{|line| Pathname.new(line)}
end
def with_tempfile
just_files = [] # set the context
Tempfile.create('tempfile') do |tempfile|
system("rg --files -uu ~/ 2> /dev/null > #{tempfile.path}")
just_files = filter_files(tempfile.path)
end
just_files
end
source_paths = with_tempfile
total_file_count = source_paths.size
source_paths.reject!{|f|
f.mtime < LAST_BACKUP_TIME
}
modified_file_count = source_paths.size
puts "done"
print "Creating target paths ... "
target_paths = []
source_paths.each do |source|
target_paths << Pathname.new( source.to_s.gsub(home_string, BACKUP_DIR_STRING) )
end
puts "done"
puts "Copy files to backup ... "
(0..source_paths.size-1).each do |inx|
directory = target_paths[inx].parent
directory.mkpath
command = "cp -f '#{source_paths[inx]}' '#{target_paths[inx]}'"
puts command
`#{command}`
end
puts "done"
LAST_TIME_PATH.write(DateTime.now.to_s)