Class: Gitti::GitBackup

Inherits:
Object
  • Object
show all
Defined in:
lib/gitti/backup/backup.rb

Defined Under Namespace

Classes: Tool

Instance Method Summary collapse

Constructor Details

#initialize(root = '~/backup', daily: false) ⇒ GitBackup

Returns a new instance of GitBackup.



18
19
20
21
22
23
24
25
26
27
# File 'lib/gitti/backup/backup.rb', line 18

def initialize( root= '~/backup', daily: false )
  @root = File.expand_path( root )
  pp @root

  ## use current working dir for the log path; see do_with_log helper for use
  @log_path = File.expand_path( '.' )
  pp @log_path

  @daily = daily
end

Instance Method Details

#backup(repos) ⇒ Object



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
# File 'lib/gitti/backup/backup.rb', line 36

def backup( repos )
  count_orgs  = 0
  count_repos = 0

  total_repos  = repos.size

  ##  default to adding folder per day ## e.g. 2015-11-20
  backup_path = "#{@root}"
  backup_path <<  "/#{Date.today.strftime('%Y-%m-%d')}"  if @daily
  pp backup_path

  FileUtils.mkdir_p( backup_path )   ## make sure path exists

  repos.each do |org, names|
    org_path = "#{backup_path}/#{org}"
    FileUtils.mkdir_p( org_path )   ## make sure path exists

    names.each do |name|
      puts "[#{count_repos+1}/#{total_repos}] #{org}@#{name}..."

      repo = GitHubRepo.new( org, name )  ## owner, name e.g. rubylibs/webservice

      success = Dir.chdir( org_path ) do
                  if Dir.exist?( "#{repo.name}.git" )
                    GitMirror.open( "#{repo.name}.git" ) do |mirror|
                      do_with_retries { mirror.update }   ## note: defaults to two tries
                    end
                  else
                    do_with_retries { Git.mirror( repo.https_clone_url ) }
                  end
                end

      ## todo/check:  fail if success still false after x retries? -- why? why not?

      count_repos += 1
    end
    count_orgs += 1
  end

  ## print stats
  puts "  #{count_repos} repo(s) @ #{count_orgs} org(s)"
end

#daily=(value) ⇒ Object

auto-add “daily” date folder / dir

e.g. 2015-11-20 using Date.today.strftime('%Y-%m-%d')


32
# File 'lib/gitti/backup/backup.rb', line 32

def daily=(value) @daily=value; end

#do_with_log(&blk) ⇒ Object

private helpers



83
84
85
86
87
88
89
90
91
92
93
# File 'lib/gitti/backup/backup.rb', line 83

def do_with_log( &blk )
  blk.call
  true  ## return true  ## success/ok
rescue GitError => ex
  puts "!! ERROR: #{ex.message}"

  File.open( "#{@log_path}/errors.log", 'a' ) do |f|
    f.write "#{Time.now} -- #{ex.message}\n"
  end
  false ## return false  ## error/nok
end

#do_with_retries(retries: 2, sleep: 5, &blk) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/gitti/backup/backup.rb', line 96

def do_with_retries( retries: 2, sleep: 5, &blk )
  retries_count = 0
  success = false
  loop do
    success = do_with_log( &blk )
    retries_count += 1
    break if success || retries_count >= retries
    puts "retrying in #{sleep}secs... sleeping..."
    sleep( sleep )  ## sleep 5secs or such
  end
  success   ## return if success or not  (true/false)
end