Synchronize directories between computers using rsync (and SSH)

https://twitter.com/climagic/status/363326283922419712

I found this command line magic gem some time ago and was using it ever since.

I started using it for synchronizing directories between computers on the same network. But it felt kind of clunky and cumbersome to get the slashes right so that it wouldn’t nest those directories and copy everything. Since both source and destination machine had the same basic directory layout, I thought ‘why not make it easier?’ … e.g. like this:

sync-to other-pc ~/Documents
sync-to other-pc ~/Music --exclude '*.wav'
sync-from other-pc ~/Music --dry-run --delete

It uses rsync for the heavy lifting but does the tedious source and destination mangling for you. 😀

You can find the code in this Gist.

#!/usr/bin/env python3
#
# Author: Riyad Preukschas <riyad@informatik.uni-bremen.de>
# License: Mozilla Public License 2.0
#
# Synchronize directories between computers using rsync (and SSH).
#
# INSTALLATION:
# Save this script as something like `sync-to` somewhere in $PATH.
# Link it to `sync-from` in the same location. (i.e. `ln sync-to sync-from`)
import os
import re
import shlex
import subprocess
import sys
PROGRAM_NAME = os.path.basename(sys.argv[0])
RSYNC = 'rsync'
RSYNC_BASIC_OPTIONS = [
'--rsh="ssh"', '--partial', '--progress', '--archive', '--human-readable']
RSYNC_EXCLUDE_PATTERNS = ['.DS_Store', '.localized']
#
# helpers
#
def print_usage_and_die():
print(re.sub(r'^[ ]{8}', '',
f"""
Synchronize directories between computers using rsync (and SSH).
Usage: {PROGRAM_NAME} HOST DIR [options]
HOST any host you'd use with SSH
DIR must be available on both the local and the remote machine
Options:
You can pass any options rsync accepts.
-v, --verbose will also print the command that'll be used to sync
Examples:
sync-to other-pc ~/Documents
sync-to other-pc ~/Music --exclude '*.wav'
sync-from other-pc ~/Music --dry-run --delete
""".strip(),
flags=re.MULTILINE
))
exit(1)
def is_verbose():
return any(
re.search(r'^--verbose|-\w*v\w*$', arg) is not None
for arg
in sys.argv
)
#
# main
#
def main():
#
# parse options
#
if len(sys.argv) < 3:
print_usage_and_die()
host = sys.argv[1]
dir = sys.argv[2]
rsync_excludes = [f"--exclude='{pattern}'" for pattern in RSYNC_EXCLUDE_PATTERNS]
rsync_user_options = sys.argv[3:]
if re.search(r'from$', PROGRAM_NAME):
rsync_src_dest = [f"{host}:{dir}/", dir]
elif re.search(r'to$', PROGRAM_NAME):
rsync_src_dest = [f"{dir}/", f"{host}:{dir}"]
else:
print('Error: unknown command')
print_usage_and_die()
#
# copy
#
exec_args = RSYNC_BASIC_OPTIONS + rsync_excludes + rsync_user_options + rsync_src_dest
if is_verbose():
print(f"{RSYNC} {' '.join(RSYNC_BASIC_OPTIONS + rsync_excludes)} {shlex.join(rsync_user_options + rsync_src_dest)}")
os.execvp(RSYNC, exec_args)
#subprocess.run([RSYNC] + exec_args)
if __name__ == '__main__':
main()
view raw sync-to-from hosted with ❤ by GitHub

Convert any file VLC can play to mp3

I just felt the need for a script that could extract the audio track of a video, transcode it and save it as an mp3 file … 2 hours later I was finished (get the Gist). 😀 It uses VLC to do hard work. 😉

Thanks to Kris Hom for the inspiration. 🙂

#!/usr/bin/env ruby -rubygems
#
# Author: Riyad Preukschas <riyad@informatik.uni-bremen.de>
# License: Mozilla Public License 2.0
#
# Transcode any file ffmpeg can play to mp3.
require 'optparse'
require 'shellwords'
#
# helpers
#
def puts_error(message)
puts "Error: #{message}"
end
def puts_warning(message)
puts "Warning: #{message}"
end
#
# parse options
#
options = {
:bit_rate => 192,
}
begin
OptionParser.new do |opts|
opts.banner = "Usage: convert2mp3 [options] FILE [<FILE ...>]"
opts.separator ""
opts.separator "Conversion Options:"
opts.on("-f", "--[no-]force", "Overwrite mp3 file if it exists already") do |f|
options[:force_overwrite] = f
end
opts.on("-o", "--output-directoy [DIRECTORY]", "Save converted files to this directory") do |out_dir|
unless File.directory?(out_dir)
puts_error %Q(Wrong output directory. "#{out_dir}" is not a directory.)
exit 1
end
options[:output_dir] = out_dir
end
opts.separator ""
opts.separator "mp3 Options:"
opts.on("-b", "--bit-rate [RATE]", Integer, "Bit rate in kbit/s (default: #{options[:bit_rate]})") do |b|
options[:bit_rate] = b
end
opts.separator ""
opts.separator "General Options:"
opts.on_tail("-h", "--help", "Show this message") do
puts opts
exit 0
end
opts.on_tail("-v", "--[no-]verbose", "Be more verbose") do |v|
options[:verbose] = v
end
end.parse!
rescue OptionParser::InvalidOption => e
puts_error e.message
exit 1
end
#
# gather source files
#
if ARGV.empty?
puts_error "No files given."
exit 1
end
source_files = []
while !ARGV.empty?
source_path = ARGV.shift
unless File.exists?(source_path)
puts_warning %Q(Skipping "#{source_path}". It doesn't exist.)
next
end
unless File.file?(source_path)
puts_warning %Q(Skipping "#{source_path}". It's not a file.)
next
end
source_files << source_path
end
#
# ffmpeg command line options
#
FFMPEG = 'ffmpeg'
TRANSCODING = '-vn -ab BIT_RATEk -vbr on -f mp3'.
gsub('BIT_RATE', options[:bit_rate].to_s)
VERBOSITY = options[:verbose] ? '-v info' : '-v quiet'
if `which #{FFMPEG} > /dev/null` && $? != 0
puts_error "Couldn't find ffmpeg."
exit 1
end
source_files.each do |source_file|
source_dir = File.dirname(source_file)
output_dir = options[:output_dir] || source_dir
output_file = File.join(output_dir, "#{File.basename(source_file, ".*")}.mp3")
if File.exist?(output_file)
if options[:force_overwrite]
puts_warning %Q(Output file exists. "#{output_file}" will be overwritten.) if options[:verbose]
else
puts_warning %Q(Output file exists already. Skipping "#{output_file}".)
next
end
end
puts %Q(Converting "#{source_file}")
unless system("#{FFMPEG} #{VERBOSITY} -y -i #{source_file.shellescape} #{TRANSCODING} #{output_file.shellescape}")
puts_error "Conversion failed. Try using the -v option for more detailed output."
end
puts if options[:verbose]
end
#!/usr/bin/env ruby -rubygems
#
# Author: Riyad Preukschas <riyad@informatik.uni-bremen.de>
# License: Mozilla Public License 2.0
#
# Transcode any file VLC can play to mp3.
require 'optparse'
require 'mkmf'
require 'shellwords'
#
# helpers
#
def puts_error(message)
puts "Error: #{message}"
end
def puts_warning(message)
puts "Warning: #{message}"
end
#
# parse options
#
options = {
:bit_rate => 192,
:sampling_rate => 44100,
}
begin
OptionParser.new do |opts|
opts.banner = "Usage: convert2mp3 [options] FILE [<FILE ...>]"
opts.separator ""
opts.separator "Conversion Options:"
opts.on("-f", "--[no-]force", "Overwrite mp3 file if it exists already") do |f|
options[:force_overwrite] = f
end
opts.on("-o", "--output-directoy [DIRECTORY]", "Save converted files to this directory") do |out_dir|
unless File.directory?(out_dir)
puts_error %Q(Wrong output directory. "#{out_dir}" is not a directory.)
exit 1
end
options[:output_dir] = out_dir
end
opts.separator ""
opts.separator "mp3 Options:"
opts.on("-b", "--bit-rate [RATE]", Integer, "Bit rate in kbit/s (default: #{options[:bit_rate]})") do |b|
options[:bit_rate] = b
end
opts.on("-s", "--sampling-rate [RATE]", Integer, "Sampling rate in Hz (default: #{options[:sampling_rate]})") do |s|
options[:sampling_rate] = s
end
opts.separator ""
opts.separator "General Options:"
opts.on_tail("-h", "--help", "Show this message") do
puts opts
exit 0
end
opts.on_tail("-v", "--[no-]verbose", "Be more verbose") do |v|
options[:verbose] = v
end
end.parse!
rescue OptionParser::InvalidOption => e
puts_error e.message
exit 1
end
#
# gather source files
#
if ARGV.empty?
puts_error "No files given."
exit 1
end
source_files = []
while !ARGV.empty?
source_path = ARGV.shift
unless File.exists?(source_path)
puts_warning %Q(Skipping "#{source_path}". It doesn't exist.)
next
end
unless File.file?(source_path)
puts_warning %Q(Skipping "#{source_path}". It's not a file.)
next
end
source_files << source_path
end
#
# VLC command line options
#
# using find_executable() will create a mkmf.log file :(
vlc_paths = [
'vlc',
'/Applications/VLC.app/Contents/MacOS/VLC',
File.expand_path('~/Applications/VLC.app/Contents/MacOS/VLC'),
]
VLC = vlc_paths.find { |path| find_executable0(path) }
INTERFACE = '-I dummy'
PLAYBACK = '--no-loop --no-repeat --play-and-exit'
TRANSCODING = '--sout="#transcode{vcodec=dummy,acodec=mp3,ab=BIT_RATE,channels=2,samplerate=SAMPLING_RATE}:standard{access=file,mux=raw,dst=\'DESTINATION\'}"'.
gsub('BIT_RATE', options[:bit_rate].to_s).
gsub('SAMPLING_RATE', options[:sampling_rate].to_s)
VERBOSITY = options[:verbose] ? '' : '-q 2>/dev/null 1>/dev/null'
if VLC.nil?
puts_error "Couldn't find VLC."
exit 1
end
source_files.each do |source_file|
source_dir = File.dirname(source_file)
output_dir = options[:output_dir] || source_dir
output_file = File.join(output_dir, "#{File.basename(source_file, ".*")}.mp3")
if File.exist?(output_file)
if options[:force_overwrite]
puts_warning %Q(Output file exists. "#{output_file}" will be overwritten.) if options[:verbose]
else
puts_warning %Q(Output file exists already. Skipping "#{output_file}".)
next
end
end
puts %Q(Converting "#{source_file}")
unless system("#{VLC} #{PLAYBACK} #{INTERFACE} #{TRANSCODING.gsub('DESTINATION', output_file.gsub(/'/, ""))} #{source_file.shellescape} #{VERBOSITY}")
puts_error "Conversion failed. Try using the -v option for more detailed output."
end
puts if options[:verbose]
end

Update 2014-03-01:

  • Check whether VLC is installed
  • Should also work on Linux now
  • Increase default bit rate to 192kbit/s
  • Fixed bug where the file/playlist would repeat endlessly

Update 2014-11-05:

  • Also look for VLC in “~/Application/”

Update 2016-03-05