Last active
August 29, 2015 14:17
-
-
Save 5t111111/ffa9561054b6f3320a04 to your computer and use it in GitHub Desktop.
Evernote Backup Script (Mac)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env ruby | |
| require "pathname" | |
| require "fileutils" | |
| require "date" | |
| BACKUP_PATH = "~/Documents/evernote_backup" | |
| def osascript(script) | |
| system "osascript", *script.split(/\n/).map { |line| ["-e", line] }.flatten | |
| end | |
| def copy_backup(src, dst, rotate: 5) | |
| src_path = File.expand_path(src) | |
| dst_path = File.expand_path(dst) | |
| name = File.basename(src) | |
| name = File.join(dst_path, name) # full path | |
| return false unless Pathname.new(name).absolute? | |
| backup_filename = name + "-" + Time.new.strftime("%Y%m%d%H%M%S") + ".tar.gz" | |
| cmd = "tar cfzp #{backup_filename} #{src_path}" | |
| `#{cmd}` | |
| purge_old_backups(name, rotate) | |
| end | |
| def purge_old_backups(backup_name, rotate) | |
| files = [] | |
| Dir.glob("#{backup_name}-*.tar.gz").each do |f| | |
| files << f | |
| end | |
| files.sort! | |
| (files.size - rotate).times.each do | |
| puts "Removing #{files.first} ..." | |
| FileUtils.rm(files.shift) | |
| end | |
| end | |
| def export_notes(export_to, format: "ENEX") | |
| osascript <<-EOS | |
| set dest to "#{export_to}" | |
| with timeout of (1 * 60 * 60) seconds | |
| tell application "Evernote" | |
| -- Set date to 1990 so it finds all notes | |
| set matches to find notes "created:19900101" | |
| -- export to file set above | |
| export matches to dest format #{format} with tags | |
| end tell | |
| end timeout | |
| EOS | |
| end | |
| tmp_path = File.expand_path("~/Documents/export_enex") | |
| export_notes(tmp_path, format: "ENEX") | |
| backup_path = File.expand_path(BACKUP_PATH) | |
| FileUtils.mkdir(backup_path) unless Dir.exist?(backup_path) | |
| copy_backup(tmp_path, backup_path, rotate: 3) | |
| FileUtils.rm_rf(tmp_path) | |
| if Date.today.wday == 1 # Monday | |
| tmp_path = File.expand_path("~/Documents/export_html") | |
| export_notes(tmp_path, format: "HTML") | |
| copy_backup(tmp_path, backup_path, rotate: 1) | |
| FileUtils.rm_rf(tmp_path) | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment