class Gitlab::Backup::Repository

The repository to back up.

Attributes

backup_root[R]

@return [String]

the absolute path of the directory to backup the repository to.
repo[R]

@return [Hash]

the hash of repository data from the Gitlab API.
token[R]

@return [String]

the private token for the user with which to backup the repository with.

Public Class Methods

new(repo, token, backup_root) click to toggle source

Creates a new repository.

@param [Hash] repo

the hash of repository data from the Gitlab API.

@param [String] token

the private token for the user with which to backup the repository with.

@param [String] backup_root

the absolute path of the directory to backup the repository to.
# File lib/gitlab-backup/repository.rb, line 35
def initialize(repo, token, backup_root)
  @repo        = repo
  @token       = token
  @backup_root = backup_root
end

Public Instance Methods

backup() click to toggle source

Performs a backup of the repository.

# File lib/gitlab-backup/repository.rb, line 43
def backup
  puts "Backing up: #{repo["name_with_namespace"]}."

  unless Gitlab::Backup.have_git?
    puts "Warning: git not found on PATH. Skipping..."

    return
  end

  clone_or_update
end

Private Instance Methods

clone_or_update() click to toggle source

Performs a full backup of the repository's source code or wiki if the directory to which the backup would occur does not exist. Performs an incremental update (pull) otherwise.

# File lib/gitlab-backup/repository.rb, line 60
def clone_or_update
  path = dir_for_repo
  uri  = repo["ssh_url_to_repo"]

  if File.exist?(path)
    run_incremental_backup(path, uri)
  else
    run_full_backup(uri, path)
  end
end
dir_for_repo() click to toggle source
# File lib/gitlab-backup/repository.rb, line 99
def dir_for_repo
  File.expand_path("#{backup_root}/#{repo["path_with_namespace"]}/src")
end
repo?(path) click to toggle source

Checks whether the specified path is a repository or not.

@param [String] path

the path to check.

@return [Boolean]

true if the path is the same type of repository
that we got from the Gitlab API, false otherwise.
# File lib/gitlab-backup/repository.rb, line 79
def repo?(path)
  FileUtils.cd(path)

  system "git status -s"

  return $? == 0
end
run_full_backup(uri, dest) click to toggle source
# File lib/gitlab-backup/repository.rb, line 95
def run_full_backup(uri, dest)
  system("git", "clone", "--recursive", uri, dest)
end
run_incremental_backup(path, uri) click to toggle source
# File lib/gitlab-backup/repository.rb, line 87
def run_incremental_backup(path, uri)
  return unless repo?(path)

  FileUtils.cd(path)

  system("git", "pull", uri)
end