Skip to content

Instantly share code, notes, and snippets.

@jmlrt
Last active August 29, 2015 14:21
Show Gist options
  • Save jmlrt/bba42cad9eced1145f51 to your computer and use it in GitHub Desktop.
Save jmlrt/bba42cad9eced1145f51 to your computer and use it in GitHub Desktop.
Duplicity Rakefile
# Rakefile
# Use env.rb for environment variables
require "./env"
# Duplicity command
$cmd = "/usr/local/bin/duplicity --ssh-options=\"-oIdentityFile=#{$keyfile}\""
# Print message
def msg(task, dir)
msg = "*** #{task.upcase} - #{dir.upcase} ***"
puts "-" * msg.length
puts msg
puts "-" * msg.length
end
# List directories on backup server
def listdir
return %x[ssh -i #{$keyfile} #{$rmtsrv} ls #{$backuproot}]
end
# Check if directory exists on backup server
def exists(dir)
sh "ssh -i #{$keyfile} #{$rmtsrv} test -d #{$backuproot}/#{dir}", verbose: false
end
# Match source directory to backup directory if needed
def getsrc(dir)
src = case dir
when "documents" then dir.capitalize
when "movies" then dir.capitalize
when "photos" then "Pictures/JM_Photos.photoslibrary/Masters"
else dir
end
return src
end
task :default => :listdir
desc "list directories to backup"
task :listdir do
puts listdir
end
desc "backup directory in argument"
task :backup, :dir do |t, args|
msg(t.name, args.dir)
exists(args.dir)
src = getsrc(args.dir)
sh "ulimit -n 1024 && export PASSPHRASE=#{$passphrase} && \
#{$cmd} --full-if-older-than #{$delay} --volsize #{$volsize} \
#{$homedir}/#{src} scp://#{$rmtsrv}/#{$backuproot}/#{args.dir}" \
, verbose: false
end
desc "backup all directories"
task :backupall do
dirs = listdir.split
dirs.each do |d|
Rake::Task["backup"].invoke("#{d}")
Rake::Task["backup"].reenable
end
end
desc "purge old backups for directory in argument"
task :purge, :dir do |t, args|
msg(t.name, args.dir)
exists(args.dir)
sh "export PASSPHRASE=#{$passphrase} && #{$cmd} \
remove-older-than #{$delay} \
scp://#{$rmtsrv}/#{$backuproot}/#{args.dir}", verbose: false
end
desc "list files backuped for directory in argument"
task :list, :dir do |t, args|
msg(t.name, args.dir)
exists(args.dir)
sh "export PASSPHRASE=#{$passphrase} && #{$cmd} \
list-current-files scp://#{$rmtsrv}/#{$backuproot}/#{args.dir}" \
, verbose: false
end
desc "verify backup for directory in argument"
task :verify, :dir do |t, args|
msg(t.name, args.dir)
exists(args.dir)
src = getsrc(args.dir)
sh "export PASSPHRASE=#{$passphrase} && #{$cmd} verify \
--compare-data scp://#{$rmtsrv}/#{$backuproot}/#{args.dir} \
#{$homedir}/#{src}", verbose: false
end
desc "get backup status for directory in argument"
task :status, :dir do |t, args|
msg(t.name, args.dir)
exists(args.dir)
sh "export PASSPHRASE=#{$passphrase} && #{$cmd} collection-status \
scp://#{$rmtsrv}/#{$backuproot}/#{args.dir}", verbose: false
end
# env.rb
# environnement variable for Rakefile
$homedir = ""
$rmtsrv = ""
$backuproot = ""
$keyfile = ""
$passphrase = ""
$delay = ""
$volsize = ""
@jmlrt
Copy link
Author

jmlrt commented May 26, 2015

This is a Rakefile to manage my backup on remote server via duplicity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment