joeyates / imap-backup

Backup and Migrate IMAP Email Accounts
MIT License
1.37k stars 75 forks source link

folder exclude list #87

Closed acavalin closed 2 years ago

acavalin commented 3 years ago

Hello,

could you add the support for a folder exclude list?

While backing up my Gmail account I wish to exclude all [Gmail] subfolders except [Gmail]/Sent Mail, with this feature I will be able to exclude [Gmail]/All Mail, [Gmail]/Important, [Gmail]/Bin, [Gmail]/Starred, [Gmail]/Spam, [Gmail]/Drafts.

Thank you

joeyates commented 3 years ago

Hi @acavalin

This would certainly be useful, but I'm worried about the complexity it might introduce. It would require the intorduction of a wildcard-based blacklist to the config.

Currently, imap-backup works on a whitelist-based approach: you indicate a list of folders to download (and the default is all of them).

Mixing a whitelist and a blacklist risks being complex in terms of UX.

In your specific use case, could you simply have [Gmail]/Sent Mail as the only item in the folder list?

acavalin commented 3 years ago

In your specific use case, could you simply have [Gmail]/Sent Mail as the only item in the folder list?

Hi, that won't do bacause I have many labels and sublabels other than [Gmail]/Sent Mail and I wish to backup them all except Gmail "special" labels to avoid email duplication.

Right now to overcome this issue I wrote a tiny script that lists all labels with imap-backup folders then excludes the special ones and populates the "folders" array in ~/.imap-backup/config.json: it works but it's not the best solution.

joeyates commented 3 years ago

Thanks the clarification.

Let's close for now - if someone comes up with a PR for this, we can see how it pans out!

slhck commented 2 years ago

@acavalin Would you be willing to share that script of yours? I was also looking for an exclude option given the fact that I subscribe to some mailing lists which I do not want to back up, and I have so many labels it's hard to add them all manually.

slhck commented 2 years ago

I would add that in terms of UX it might even be better to have the exclude option, since whitelabeling means you have to remember which labels you want backed up. Say you add a label in the future, do you always remember to update your imap-backup config accordingly? I probably would forget it.

That said I know it's a larger feature.

acavalin commented 2 years ago

@acavalin Would you be willing to share that script of yours?

Sure, here it is:

#!/usr/bin/env ruby
$VERBOSE = nil

%w{ bundler/inline json yaml shellwords }.each{|l| require l }

gemfile do
  source 'https://rubygems.org'
  gem 'imap-backup', require: false
end

# load config
CFG = "#{ENV['HOME']}/.imap-backup/config.json"
cfg = JSON.parse(File.read(CFG), symbolize_names: true)
# get folders list and exclude unused Gmail folders
dirs = `imap-backup folders | grep "^\\s"`.split("\n").sort.map(&:strip).
 reject{|i| i.start_with?('[Gmail]') && !i.end_with?('Sent Mail') }
raise 'no folders' unless dirs.size > 0
cfg[:accounts][0][:folders] = dirs.map{|i| {name: i} }
# update config
File.open(CFG, 'w'){|f| f.write JSON.pretty_generate(cfg) }

# delete old INBOX dump & sync all folders
Dir[cfg[:accounts][0][:local_path]+'/INBOX.*'].each{|f| File.unlink f }
system 'imap-backup'
system 'sync'

# backup any mbox file whose size changed
Dir.chdir(cfg[:accounts][0][:local_path]) do
  cfg_sizes = "sync-email.yml"
  old_sizes = YAML::load_file cfg_sizes rescue {}
  new_sizes = Dir['**/*.mbox'].sort.
    inject({}){|h, i| dirs.include?(i[0..-6]) ? h.merge(i => File.size(i)) : h }
  # compress files whose size changed
  new_sizes.each do |fname, s|
    next if fname != 'INBOX.mbox' && old_sizes[fname] == s
    name = fname[0..-6].shellescape
    dest = "/path/to/bkup/#{fname[0..-6].tr '/', '~'}.txz".shellescape
    system "tar -cf - #{name}.* | pigz | pv -N #{name} -btr > #{dest}"
  end
  # update file with sizes
  File.open(cfg_sizes, 'w'){|f| f.puts new_sizes.to_yaml }
end

take a look at line 16, there you can use a series of grep_v instead of reject, or insert inside the reject as many conditions as you want.

joeyates commented 2 years ago

Having blacklists seems like an interesting thing to handle. I'll reopen to track this while we look into the matter.

joeyates commented 2 years ago

I've just released version 8.0.0.

In the new release, folders can be backlisted or whitelisted. You can opt to choose folders to exclude from backups, as against the default behaviour where you choose folders to include.