Skip to content

Instantly share code, notes, and snippets.

@niklas
Created May 19, 2014 15:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save niklas/66805c73eec503bd0694 to your computer and use it in GitHub Desktop.
Save niklas/66805c73eec503bd0694 to your computer and use it in GitHub Desktop.
Backup script using git
#!/usr/bin/env ruby
medium = ARGV.shift || 'Spinn'
user = ENV['USER']
hostname = `hostname`.chomp
path = "/media/#{user}/#{medium}"
rc = "#{ENV['HOME']}/.bashrc-backup"
unless File.directory?(path)
raise RuntimeError, "please mount #{medium}"
end
git_path = "#{path}/git/#{user}-#{hostname}-home.git/"
unless File.directory?(git_path)
raise RuntimeError, "repository does not exist: #{git_path}"
end
ENV['BACKUP_RUNNING'] = "true"
ENV['GIT_DIR'] = git_path
ENV['prompt_char']="backup >"
ENV['HISTFILE']="#{ENV['HOME']}/.backup_bash_history"
puts "Starting Backup env"
commands = <<-EOMAN
. ~/.bashrc
unalias -a
alias s="git status --untracked-files=all --ignore-submodules=all"
alias ss="s | less"
alias a="git add -A"
alias r="git reset HEAD"
alias ci="a ~/.gitignore && git commit -m"
i() {
git rm --cached -r $1
echo $1 >> ~/.gitignore
}
snap() {
ci "snapshot $(date -R)"
}
alias -p
echo "i=ignore given files"
echo 'awaiting you command'
EOMAN
File.open(rc, 'w') { |f| f.write commands }
exec(%Q~bash --rcfile #{rc} -i~)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment