Created
November 16, 2012 09:17
-
-
Save pixie79/4085782 to your computer and use it in GitHub Desktop.
Rackspace Cloud File - push large files to cloud daily as a backup
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
################ | |
# This script was written to take a large file over 2G and split it into smaller files then push these to Rackspace's cloud files daily. | |
# The following day it will remove the next backup once the current backup has been pushed. | |
# It expects the filename and path to the file to be backed up to be passed to it as a calling argument | |
# Sample path - /opt/backup/store/Server1.Thu.2 # Where Thu is day of the week of the backup and 2 is the week of the month. | |
# Update XXXX with the appropriate values in the appropriate places | |
#!/usr/bin/env ruby | |
require 'rubygems' | |
require 'cloudfiles' | |
require 'pathname' | |
require 'find' | |
file = ARGV[0] | |
p = Pathname.new(file) | |
filename = p.basename | |
flock = "/opt/backup/flock/#{filename}" | |
def check_valid_file(file, filename) | |
if File.basename(filename) =~ /\.tmp|\.txt|\.jpg|\.noboot/ | |
abort "Not a file we archive: #{filename}" | |
end | |
unless File.exists?(file) | |
abort "File not found: #{filename}" | |
end | |
end | |
def check_lock(flock) | |
if File.exists?(flock) | |
abort "Flock found for: #{flock}" | |
end | |
end | |
def lock_file(flock) | |
File.open(flock, "w") {} | |
end | |
def split_file(file, filename) | |
`/usr/bin/split -d -b 2G #{file} /opt/backup/tmp/#{filename}.` | |
end | |
def unlock(delete_file) | |
unlock_file_split = delete_file.split('.') | |
unlock_file = "/opt/backup/flock/#{unlock_file_split[0]}.#{unlock_file_split[1]}.#{unlock_file_split[2]}" | |
puts "Unlocking: #{unlock_file}" | |
if File.exists?(unlock_file) | |
File.delete(unlock_file) | |
end | |
end | |
def rm_file(filename, container) | |
cust = filename.to_s().split(/\./)[0] | |
puts "Filename to check for deletion: #{cust}" | |
cloud_files = container.objects | |
ok_to_delete = false | |
delete_list = Array.new | |
cloud_files.each do |cloud_file| | |
if cloud_file =~ /#{cust}/ | |
puts "Matched: #{cloud_file}" | |
if cloud_file =~ /#{filename.to_s()}/ | |
ok_to_delete = true | |
else | |
puts "File can be deleted: #{cloud_file}" | |
delete_list.push(cloud_file) | |
end | |
end | |
end | |
if ok_to_delete | |
unless delete_list.empty? | |
delete_list.each do |delete_file| | |
puts " Deleting File from rackspace: #{delete_file}" | |
object = container.delete_object delete_file | |
unlock(delete_file) | |
end | |
else | |
puts "Nothing to delete" | |
end | |
else | |
"Current Backup Failure: Backup not present" | |
end | |
end | |
def file_upload(path, container) | |
up_filename = Pathname.new(path).basename | |
puts "Uploading filename: #{up_filename}" | |
object = container.create_object "#{up_filename}", false | |
object.write File.open(path) | |
puts "file uploaded" | |
File.delete(path) | |
end | |
def find_files_toupload(filename, container) | |
Find.find('/opt/backup/tmp') do |path| | |
if File.basename(path)[0] == '..' | |
Find.prune | |
elsif path =~ /#{filename}/ | |
file_upload(path, container) | |
else | |
next | |
end | |
end | |
end | |
check_valid_file(file, filename) | |
check_lock(flock) | |
lock_file(flock) | |
cf = CloudFiles::Connection.new(:username => "XXXX", :api_key => "XXXX", :auth_url => CloudFiles::AUTH_UK) | |
container = cf.container('XXXX') | |
split_file(file, filename) | |
find_files_toupload(filename, container) | |
rm_file(filename, container) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment