Skip to content

Commit 0c9c3f7

Browse files
committed
refactoring, README improved, env vars changed, v0.1.1
1 parent bbfbab7 commit 0c9c3f7

File tree

5 files changed

+37
-30
lines changed

5 files changed

+37
-30
lines changed

Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
travis-backup (0.1.0)
4+
travis-backup (0.1.1)
55
activerecord
66
bootsnap
77
pg

README.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# README
22

3-
*travis-backup* is an application that helps with housekeeping and backup for Travis CI database v2.2 and with migration to v3.0 database.
3+
*travis-backup* is an application that helps with housekeeping and backup for Travis CI database v2.2 and with migration to v3.0 database. By default it removes requests and builds with their corresponding jobs and logs, as long as they are older than given threshold says (and backups them in files, if this option is active). Although it can be also run with special modes: `move_logs`, for moving logs from one database to another, and `remove_orphans`, for deleting all orphaned data.
44

55
### Installation and run
66

@@ -75,15 +75,19 @@ backup:
7575
limit: 1000 # builds limit for one backup file
7676
threshold: 6 # number of months from now - data younger than this time won't be backuped
7777
files_location: './dump' # path of the folder in which backup files will be placed
78-
user_id # run only for given user
79-
org_id # run only for given organization
80-
repo_id # run only for given repository
78+
user_id: 1 # run only for given user
79+
org_id: 1 # run only for given organization
80+
repo_id: 1 # run only for given repository
81+
move_logs: false # run in move logs mode - move all logs to database at destination_db_url URL
82+
remove_orphans: false # run in remove orphans mode
8183
```
8284

83-
You can also set these properties using env vars corresponding to them: `IF_BACKUP`, `BACKUP_DRY_RUN`, `BACKUP_LIMIT`, `BACKUP_THRESHOLD`, `BACKUP_FILES_LOCATION`, `USER_ID`, `ORG_ID`, `REPO_ID`.
85+
You can also set these properties using env vars corresponding to them: `IF_BACKUP`, `BACKUP_DRY_RUN`, `BACKUP_LIMIT`, `BACKUP_THRESHOLD`, `BACKUP_FILES_LOCATION`, `BACKUP_USER_ID`, `BACKUP_ORG_ID`, `BACKUP_REPO_ID`, `BACKUP_MOVE_LOGS`, `BACKUP_REMOVE_ORPHANS`.
8486

8587
You should also specify your database url. You can do this the standard way in `config/database.yml` file, setting the `database_url` hash argument while creating `Backup` instance or using the `DATABASE_URL` env var. Your database should be consistent with the Travis 2.2 database schema.
8688

89+
For `move_logs` mode you need also to specify a destination database. You can set it also in `config/database.yml` file, in `destination` subsection, setting the `destination_db_url` hash argument while creating `Backup` instance or using the `BACKUP_DESTINATION_DB_URL` env var. Your destination database should be consistent with the Travis 3.0 database schema.
90+
8791
### How to run the test suite
8892

8993
You can run the test after cloning this repository. Next you should call

lib/config.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,19 +67,19 @@ def set_values(args)
6767
@user_id = first_not_nil(
6868
args[:user_id],
6969
argv_opts[:user_id],
70-
ENV['USER_ID'],
70+
ENV['BACKUP_USER_ID'],
7171
config.dig('backup', 'user_id')
7272
)
7373
@repo_id = first_not_nil(
7474
args[:repo_id],
7575
argv_opts[:repo_id],
76-
ENV['REPO_ID'],
76+
ENV['BACKUP_REPO_ID'],
7777
config.dig('backup', 'repo_id')
7878
)
7979
@org_id = first_not_nil(
8080
args[:org_id],
8181
argv_opts[:org_id],
82-
ENV['ORG_ID'],
82+
ENV['BACKUP_ORG_ID'],
8383
config.dig('backup', 'org_id')
8484
)
8585
@move_logs = first_not_nil(

lib/travis-backup.rb

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -51,21 +51,32 @@ def run(args={})
5151
elsif @config.remove_orphans
5252
remove_orphans
5353
elsif owner_id
54-
Repository.where('owner_id = ? and owner_type = ?', owner_id, owner_type).order(:id).each do |repository|
55-
process_repo(repository)
56-
end
54+
process_repos_for_owner(owner_id, owner_type)
5755
elsif repo_id
58-
repository = Repository.find(repo_id)
59-
process_repo(repository)
56+
process_repo_with_id(repo_id)
6057
else
61-
Repository.order(:id).each do |repository|
62-
process_repo(repository)
63-
end
58+
process_all_repos
6459
end
6560

6661
print_dry_run_report if @config.dry_run
6762
end
6863

64+
def process_repos_for_owner(owner_id, owner_type)
65+
Repository.where('owner_id = ? and owner_type = ?', owner_id, owner_type).order(:id).each do |repository|
66+
process_repo(repository)
67+
end
68+
end
69+
70+
def process_repo_with_id(repo_id)
71+
process_repo(Repository.find(repo_id))
72+
end
73+
74+
def process_all_repos
75+
Repository.order(:id).each do |repository|
76+
process_repo(repository)
77+
end
78+
end
79+
6980
def print_dry_run_report
7081
if @dry_run_report.to_a.map(&:second).flatten.empty?
7182
puts 'Dry run active. No data would be removed in normal run.'
@@ -168,23 +179,15 @@ def remove_orphans_for_table(model_class, table_a_name, table_b_name, fk_name)
168179
})
169180

170181
if config.dry_run
171-
dry_run_report[table_a_name.to_sym] = [] if dry_run_report[table_a_name.to_sym].nil?
172-
dry_run_report[table_a_name.to_sym].concat(for_delete.map(&:id))
173-
dry_run_report[table_a_name.to_sym].uniq!
182+
key = table_a_name.to_sym
183+
dry_run_report[key] = [] if dry_run_report[key].nil?
184+
dry_run_report[key].concat(for_delete.map(&:id))
185+
dry_run_report[key].uniq!
174186
else
175187
model_class.where(id: for_delete.map(&:id)).delete_all
176188
end
177189
end
178190

179-
def process_repo_builds(repository) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
180-
threshold = @config.threshold.to_i.months.ago.to_datetime
181-
current_build_id = repository.current_build_id || -1
182-
repository.builds.where('created_at < ? and id != ?', threshold, current_build_id)
183-
.in_groups_of(@config.limit.to_i, false).map do |builds_batch|
184-
@config.if_backup ? save_and_destroy_builds_batch(builds_batch, repository) : destroy_builds_batch(builds_batch)
185-
end.compact
186-
end
187-
188191
private
189192

190193
def save_and_destroy_builds_batch(builds_batch, repository)

travis-backup.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Gem::Specification.new do |s|
22
s.name = 'travis-backup'
3-
s.version = '0.1.0'
3+
s.version = '0.1.1'
44
s.summary = 'Travis CI backup tool'
55
s.authors = ['Karol Selak']
66
s.required_ruby_version = Gem::Requirement.new(">= 2.3.0")

0 commit comments

Comments
 (0)