Skip to content

Instantly share code, notes, and snippets.

@olivertappin
Created February 13, 2019 14:21
Show Gist options
  • Save olivertappin/dcf861546991e38ca76528ed22d4ab47 to your computer and use it in GitHub Desktop.
Save olivertappin/dcf861546991e38ca76528ed22d4ab47 to your computer and use it in GitHub Desktop.
Export all data from the Consul KV store into consul kv put commands
<?php
$output = shell_exec('consul kv export');
if (null === $output) {
echo 'Could not run `consul kv export`. Is consul installed?' . PHP_EOL;
exit(1);
}
$data = json_decode($output, true);
foreach ($data as $item) {
$value = base64_decode($item['value']);
// Skip items with empty values
if ('' === $value) {
// Optional output to remove these keys
// echo 'consul kv delete ' . $item['key'] . ' ' . $value . PHP_EOL;
continue;
}
// Quote values containing special characters
if ($value !== htmlspecialchars($value)) {
$value = '"' . $value . '"';
}
echo 'consul kv put ' . $item['key'] . ' ' . $value . PHP_EOL;
}
exit(0);
@EagleEyeJohn
Copy link

that'll need addslashes() or something around $value - line 24

@karlbaillie
Copy link

good work Ollie, this is amazing

@pospelov-v
Copy link

issue:

# consul kv delete --recurse
Success! Deleted keys with prefix:
# consul kv put test_key1 test_value1
Success! Data written to: test_key1
# consul kv put test_key2 "test value 2"
Success! Data written to: test_key2
# php consul-kv-export.php
consul kv put test_key1 test_value1
consul kv put test_key2 test value 2

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