Created
May 6, 2011 07:22
-
-
Save hechien/958555 to your computer and use it in GitHub Desktop.
Git Repositories auto pull for backup
This file contains 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
#!/usr/bin/ruby | |
USER = "git" | |
HOST = "ssh://#{USER}@github.com/" | |
COMMAND = "git clone #{HOST}" | |
EMAIL = ["xxx@xxx.com", "ooo@xxx.com"] | |
repos = [] | |
repos << %w"1 2 3 4" | |
repos << %w"5" | |
repos.flatten! | |
MAIL_TEST = false | |
$_logString = %{} | |
def gotoDir(dirname) | |
# Check target directory is exist in current directory. | |
unless Dir.entries('.').include?(dirname) | |
Dir.mkdir(dirname) | |
end | |
Dir.chdir(dirname) | |
setLog("Change directory to #{dirname}.\nNow: #{`pwd`}") | |
end | |
def setLog(_string, command = false) | |
unless command | |
$_logString += "[#{Time.now}] #{_string}\n\n" | |
puts _string | |
end | |
if command | |
result = `#{_string}` | |
$_logString += "[#{Time.now}]\n\n#{result}\n\n" | |
end | |
end | |
def sendMail | |
email_string = "" | |
i = 0 | |
EMAIL.map do |mail| | |
case i | |
when 0 | |
email_string += "#{mail}" | |
else | |
email_string = "-c #{mail} " + email_string | |
end | |
i+=1 | |
end | |
`echo "#{$_logString}" | mail -s "Git Backup Finished." #{email_string}` | |
end | |
gotoDir("GIT_BACKUP") | |
# Check if repo is not exist, git clone it. | |
repos.each do |repo| | |
break if MAIL_TEST | |
next if Dir.exist?(repo) | |
setLog("Ready to clone repo: #{repo}") | |
setLog("#{COMMAND}#{repo}.git") | |
setLog("#{COMMAND}#{repo}.git", true) | |
end | |
setLog("Nothing to clone or clone has been finished.") | |
setLog("Ready to pull the newest version from repo's remote.") | |
repos.each do |repo| | |
break if MAIL_TEST | |
Dir.chdir(repo) | |
setLog("Ready to pull: #{repo}") | |
setLog("git pull origin master", true) | |
Dir.chdir("../") | |
end | |
setLog("Action finished.") | |
gotoDir("log") | |
File.open("#{Time.now.strftime("%Y-%m-%d_%H-%M-%S")}.log", "a+") do |f| | |
f << $_logString | |
end | |
#sendMail() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment