Skip to content

Instantly share code, notes, and snippets.

@kolyabres
Created February 28, 2019 20:12
Show Gist options
  • Save kolyabres/eea91f10ff01bf7dda29cff30fbd207a to your computer and use it in GitHub Desktop.
Save kolyabres/eea91f10ff01bf7dda29cff30fbd207a to your computer and use it in GitHub Desktop.
#!/usr/bin/env php
<?php
$snapshotsFolder = '/home/kolyabres/bin/snapshots';
if(!file_exists($snapshotsFolder)) {
mkdir($snapshotsFolder);
}
if($argc < 2) {
print_r('too few arguments');
exit(1);
}
$command = $argv[1];
if($command === 'save') {
$name = $argv[2];
unlink($snapshotsFolder."/".$name);
exec(sprintf('pg_dump -h localhost -p 5432 -d ehr -U ehr > %s/%s', $snapshotsFolder,$name));
} elseif ($command === 'load') {
$name = $argv[2];
exec(sprintf('echo "drop owned by ehr" | psql -U ehr -h localhost -p 5432 -d ehr'));
exec(sprintf('psql -U ehr -h localhost -p 5432 -d ehr < %s/%s', $snapshotsFolder, $name));
} elseif ($command === 'delete') {
$name = $argv[2];
if(!file_exists($snapshotsFolder."/".$name)) {
print_r("snapshot not found\n");
exit(1);
}
unlink($snapshotsFolder."/".$name);
} elseif ($command === 'list') {
foreach(scandir($snapshotsFolder) as $fileName) {
if($fileName === '.' || $fileName === '..' || $fileName==='') {
continue;
}
printf("%s\n", $fileName);
}
} else {
print_r("unknown command\n");
exit(1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment