Skip to content

Instantly share code, notes, and snippets.

@brasic
Last active January 6, 2023 04:49
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brasic/0a0819fc24b5a1e7fe6bef85afe1288e to your computer and use it in GitHub Desktop.
Save brasic/0a0819fc24b5a1e7fe6bef85afe1288e to your computer and use it in GitHub Desktop.
transfer a codespaces dev mysql instance to tmpfs.
#!/usr/bin/env ruby
# frozen_string_literal: true
require "fileutils"
module MemorizeMysql
WD = "/var/lib"
MYSQL = "/var/lib/mysql"
def self.call
if File.readlines("/etc/mtab").any? { |line| line.include?("/var/lib/mysql") }
die "already in memory"
end
if Process.euid != 0
exec("sudo #{$PROGRAM_NAME}")
end
Dir.chdir(WD)
run "supervisorctl stop mysql"
tarball = nil
if !Dir.empty?(MYSQL)
tarball = "mysql-#{Time.now.to_i}.tar.xz"
puts "mysql dir is nonempty, archiving it to #{tarball}"
run "tar cJf #{tarball} #{MYSQL}"
run "rm -r #{MYSQL}/*"
end
tarball ||= Dir['/var/lib/mysql*.tar.xz'].max_by { |x| File.mtime(x) }
if !tarball
die "could not find tarball to reinflate"
end
run "mount -t tmpfs -o size=5G tmpfs #{MYSQL}"
run "tar xf #{tarball}"
FileUtils.chown_R "mysql", "mysql", MYSQL
FileUtils.chmod 0700, MYSQL
run("supervisorctl start mysql")
end
def self.run(cmd)
puts "-> #{cmd}"
system(cmd, exception: true)
end
def self.die(msg)
puts msg
exit 1
end
end
if __FILE__ == $PROGRAM_NAME
MemorizeMysql.call
end
@brasic
Copy link
Author

brasic commented Jan 6, 2023

See https://github.com/brasic/moneta for a slightly more useful version of this

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