Package: dslr
Database Snapshot, List, and RestoreTake lightning fast snapshots of your local Postgres databases.What is this?DSLR is a tool that allows you to quickly take and restore database snapshots
when you're writing database migrations, switching branches, or messing with
SQL.It's meant to be a spiritual successor toStellar.Important:DSLR is intended for development use only. It is not advisable to
use DSLR on production databases.PerformanceDSLR is much faster than the standardpg_dump/pg_restoreapproach to snapshots.DSLR is 8x faster at taking snapshots and 3x faster at restoring snapshots compared to thepg_dump/pg_restoreapproach.Testing methodologyI spun up Postgres 12.3 using Docker, created a test database, and filled it with 1GB of random data using this script:CREATETABLElarge_test(num1bigint,num2doubleprecision,num3doubleprecision);INSERTINTOlarge*test(num1,num2,num3)SELECTround(random()*10),random(),random()\_142FROMgenerate_series(1,20000000)s(i);I used the following commands to measure the execution time:time dslr snapshot my-snapshot
time dslr restore my-snapshot
time pg_dump -Fc -f export.dump
time pg_restore --no-acl --no-owner export.dumpI ran each command three times and plotted the mean in the chart.Here's the raw data:CommandRunExecution time (seconds)dslr snapshot14.79724.65032.927dslr restore15.84024.12233.331pg_dump137.345236.227336.233pg_restore113.304213.148313.320Installpip install DSLR psycopg2 # or psycopg2-binaryInstall using pipxpipx install DSLR[psycopg2] # or psycopg2-binaryNote: The DSLRexportandimportsnapshot commands requirepg_dumpandpg_restoreto be present in yourPATH, so you will need the Postgres CLI
utilities if you want to use those commands.Shell completionBashAdd this to~/.bashrc:eval "$(_DSLR_COMPLETE=bash_source dslr)"ZshAdd this to~/.zshrc:eval "$(_DSLR_COMPLETE=zsh_source dslr)"FishAdd this to~/.config/fish/completions/dslr.fish:eval (env _DSLR_COMPLETE=fish_source dslr)This is the same file used for the activation script method below. For Fish it’s probably always easier to use that method.Using eval means that the command is invoked and evaluated every time a shell is started, which can delay shell responsiveness. To speed it up, write the generated script to a file, then source that.BashSave the script somewhere._DSLR_COMPLETE=bash_source dslr > ~/.dslr-complete.bashSource the file in ~/.bashrc.. ~/.dslr-complete.bashZshSave the script somewhere._DSLR_COMPLETE=zsh_source dslr > ~/.dslr-complete.zshSource the file in ~/.zshrc.. ~/.dslr-complete.zshFishSave the script to ~/.config/fish/completions/foo-bar.fish:_DSLR_COMPLETE=fish_source dslr > ~/.config/fish/completions/dslr.fishConfigurationYou can tell DSLR which database to take snapshots of in a few ways:DATABASE_URLIf theDATABASE_URLenvironment variable is set, DSLR will use this to connect
to your target database.exportDATABASE_URL=postgres://username:password@host:port/database_namedslr.tomlIf adslr.tomlfile exists in the current directory, DSLR will read its
settings from there. DSLR will prefer this over the environment variable.url='postgres://username:password@host:port/database_name'--urloptionFinally, you can explicitly pass the connection string via the--urloption.
This will override any of the above settings.Usage$ dslr snapshot my-first-snapshot
Created new snapshot my-first-snapshot

$ dslr restore my-first-snapshot
Restored database from snapshot my-first-snapshot

$ dslr list

  Name                Created            Size
 ─────────────────────────────────────────────
  my-first-snapshot   2 minutes ago   3253 kB

$ dslr rename my-first-snapshot fresh-db
Renamed snapshot my-first-snapshot to fresh-db

$ dslr delete some-old-snapshot
Deleted some-old-snapshot

$ dslr export my-feature-test
Exported snapshot my-feature-test to my-feature-test_20220730-075650.dump

$ dslr import snapshot-from-a-friend_20220730-080632.dump friend-snapshot
Imported snapshot friend-snapshot from snapshot-from-a-friend_20220730-080632.dumpHow does it work?DSLR takes snapshots by cloning databases using Postgres'Template
Databasesfunctionality. This is the main source of DSLR's speed.This means that taking a snapshot is just creating a new database using the main
database as the template. Restoring a snapshot is just deleting the main
database and creating a new database using the snapshot database as the
template. So on and so forth.ContributorsLicenseMIT