Skip to content

Instantly share code, notes, and snippets.

@wemrekurt
Created September 10, 2018 21:06
Show Gist options
  • Select an option

  • Save wemrekurt/78f82312168950c6ece4e7b2ec9ab0e8 to your computer and use it in GitHub Desktop.

Select an option

Save wemrekurt/78f82312168950c6ece4e7b2ec9ab0e8 to your computer and use it in GitHub Desktop.
Google Drive Backup
require "google_drive"
full_path = '/home/emrek/Desktop/Business/bafrahaber/public/'
session = GoogleDrive::Session.from_config("config.json")
files_path = full_path + Date.today.prev_day.strftime('%Y/%m/%d')
def root_path
#return 'Production' if Rails.env.production?
'Bafrahaber_dev'
end
def create_folders(gd, path)
sf = Dir.glob(path+'/*').select {|f| File.directory? f}
subpath = nil
sf.each do |folder|
subpath = create_if_not_exists(gd, folder)
create_folders(subpath, folder)
end
end
def create_if_not_exists(gd_path, path)
folder = path.split('/').last
gd = gd_path.subcollection_by_title(folder).nil? ? gd_path.create_subcollection(folder) : gd_path.subcollection_by_title(folder)
files = Dir.glob(path+'/*').select {|f| !File.directory? f}
files.each do |file|
gd.file_by_title(file.split('/').last).nil? ? gd.upload_from_file(file) : gd.file_by_title(file.split('/').last)
end
return gd
end
def gd_date_path(root_path, list)
folder = list.pop
sub_p = root_path.subcollection_by_title(folder).nil? ? root_path.create_subcollection(folder) : root_path.subcollection_by_title(folder)
if list.size > 0
gd_date_path(sub_p, list)
else
sub_p
end
end
list = Date.today.prev_day.strftime('%Y/%m/%d').split('/').reverse
create_folders(gd_date_path(session.collection_by_title(root_path), list), files_path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment