Skip to content
Browse files

Added backup file rotation

  • Loading branch information...
1 parent 99a6ce0 commit d8074d971fe0a74402c4fc3858e698e76de3870d @lojic committed Feb 23, 2012
Showing with 29 additions and 9 deletions.
  1. +1 −0 .gitignore
  2. +28 −9 dedup_bash_history.rb
View
1 .gitignore
@@ -0,0 +1 @@
+*~
View
37 dedup_bash_history.rb
@@ -29,17 +29,36 @@
# ls
# echo "hello
-history_file = File.expand_path('~/.bash_history')
-backup_file = File.expand_path('~/.bash_history_backup')
-temp_file = File.expand_path('~/.bash_history_temp')
+module DeDuper
+ NUM_BACKUP_FILES = 20
+ HISTORY_FILE = File.expand_path('~/.bash_history')
+ BACKUP_FILE = File.expand_path('~/.bash_history_backup')
+ TEMP_FILE = File.expand_path('~/.bash_history_temp')
-`cp #{history_file} #{backup_file}`
+ module_function
-File.open(history_file, "r") do |hfile|
- File.open(temp_file, "w") do |tfile|
- hfile.readlines.reverse.uniq.reverse.each {|line| tfile.puts(line) }
+ def rotate_backup_files n
+ return if n < 1
+
+ if File.exist?(current_file = "#{BACKUP_FILE}#{n}")
+ `cp #{current_file} #{BACKUP_FILE}#{n+1}`
+ end
+
+ rotate_backup_files(n-1)
end
-end
-`mv #{temp_file} #{history_file}`
+ def run
+ rotate_backup_files(NUM_BACKUP_FILES)
+ `cp #{HISTORY_FILE} #{BACKUP_FILE}1`
+
+ File.open(HISTORY_FILE, "r") do |hfile|
+ File.open(TEMP_FILE, "w") do |tfile|
+ hfile.readlines.reverse.uniq.reverse.each {|line| tfile.puts(line) }
+ end
+ end
+
+ `mv #{TEMP_FILE} #{HISTORY_FILE}`
+ end
+end
+DeDuper.run

0 comments on commit d8074d9

Please sign in to comment.
Something went wrong with that request. Please try again.