From 77ea0435d415585bc400a2bdb44c48ed05d2d0e7 Mon Sep 17 00:00:00 2001 From: Tim van der Staaij Date: Sat, 2 Jan 2016 22:20:59 +0100 Subject: [PATCH] Make partial backups optional (#3) --- config.yaml | 5 +++++ telegram-history-dump.rb | 37 +++++++++++++++++++++---------------- 2 files changed, 26 insertions(+), 16 deletions(-) diff --git a/config.yaml b/config.yaml index f31a09c..55a4e33 100644 --- a/config.yaml +++ b/config.yaml @@ -84,8 +84,13 @@ # because of a large media file media_timeout: 0 + ## Advanced behavior settings ## + # Setting this to true will record backup progress in a json file in the + # output directory, and use this to backup incrementally (only new messages) + track_progress: false + # Replacement string for the characters in dialog names which are potentially # problematic in filenames # You can set this to '' if the dialog names are unique enough diff --git a/telegram-history-dump.rb b/telegram-history-dump.rb index aeeb949..f8d3a07 100755 --- a/telegram-history-dump.rb +++ b/telegram-history-dump.rb @@ -182,19 +182,22 @@ def format_dialog_list(dialogs) $progress = {} $progress_snapshot = {} -progress_file = File.join(get_backup_dir, 'progress.json') -progress_json = File.exists?(progress_file) ? File.read(progress_file) : '{}' -progress_hash = JSON.parse(progress_json) -if progress_hash['dumper'] && progress_hash['dumper'] != $config['dumper'] - raise 'Dumper conflict: configured for "%s" but progress file reads "%s". '\ +if $config['track_progress'] + progress_file = File.join(get_backup_dir, 'progress.json') + progress_json = File.exists?(progress_file) ? File.read(progress_file) : '{}' + progress_hash = JSON.parse(progress_json) + if progress_hash['dumper'] && progress_hash['dumper'] != $config['dumper'] + raise 'Dumper conflict: configured for "%s" but progress file reads "%s". '\ 'Either use the same dumper or delete the output directory.'\ % [progress_hash['dumper'], $config['dumper']] -end -(progress_hash['dialogs'] || {}).each do |k,v| - $progress[k] = DumpProgress.from_hash(v) - $progress_snapshot[k] = DumpProgress.from_hash(v) + end + (progress_hash['dialogs'] || {}).each do |k,v| + $progress[k] = DumpProgress.from_hash(v) + $progress_snapshot[k] = DumpProgress.from_hash(v) + end end + $log.info('Loading dumper module \'%s\'' % $config['dumper']) require_relative 'dumpers/%s/dumper.rb' % $config['dumper'] $dumper = Dumper.new @@ -237,13 +240,15 @@ def format_dialog_list(dialogs) end end -$log.info('Saving progress file') -progress_hash = { - :dumper => $config['dumper'], - :dialogs => $progress -} -progress_json = JSON.pretty_generate(progress_hash) + "\n" -File.write(progress_file, progress_json) +if $config['track_progress'] + $log.info('Saving progress file') + progress_hash = { + :dumper => $config['dumper'], + :dialogs => $progress + } + progress_json = JSON.pretty_generate(progress_hash) + "\n" + File.write(progress_file, progress_json) +end $dumper.end_backup if cli_opts.kill_tg