Skip to content

Instantly share code, notes, and snippets.

@musik
Created December 24, 2012 08:05
Show Gist options
  • Save musik/4368299 to your computer and use it in GitHub Desktop.
Save musik/4368299 to your computer and use it in GitHub Desktop.
近来用盛大云存储备份本地数据文件,每日同步。盛大云有个python命令行工具,里边的sync命令可以很便捷的把本地新增的备份文件同步到云端。但本地已删除的备份文件云端并不会同步删除。以下这个脚本是用来清除本地已不存在而云端尚存的旧文件。
#!/usr/bin/env ruby
require 'rubygems'
require 'pp'
require 'sndacs'
SRC= '/home/backup/db/'
BUCKET= 'muzik_backup'
CONFIG ={
:access_key_id => '**',
:secret_access_key => '**'
}
def copy_dir dir,bucket
Dir.foreach dir do |d|
next if %(. ..).include? d
realpath = "#{dir}/#{d}"
if File.directory?(realpath)
copy_dir realpath,bucket
else
copy_file realpath,bucket
end
end
end
def clean_dir dir,bucket
files = `find #{dir} -type f`.split("\n").collect{|r| relative_filename r}
files += `find #{dir} -type d`.split("\n").collect{|r| "#{relative_filename r}/"}
#pp files
bucket.objects.each do |o|
next if files.include? o.key
o.destroy
end
end
def copy_file file,bucket
relpath = relative_filename file
object = (bucket.objects.find relpath rescue nil)
file_data = File.read(file)
unless object.nil?
if Digest::MD5.hexdigest(file_data) == object.etag
#pp "#{relpath} exists"
return
end
end
object = bucket.objects.build relpath
object.content = file_data
object.save
end
def relative_filename path
path.sub(SRC,'').sub(/^\//,'')
end
api = Sndacs::Service.new CONFIG
bucket = api.buckets.find BUCKET
#copy_dir SRC,bucket
clean_dir SRC,bucket
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment