-
-
Save pandada8/d878e3c91c9e3d451848afd1ead245bc to your computer and use it in GitHub Desktop.
etcd on tmpfs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ETCD_LISTEN_CLIENT_URLS=https://0.0.0.0:2379 | |
ETCD_ADVERTISE_CLIENT_URLS=https://<replace-with-your-own> | |
ETCD_CERT_FILE=<replace-with-your-own> | |
ETCD_KEY_FILE=<replace-with-your-own> | |
ETCD_CA_FILE=<replace-with-your-own> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
do_snapshot() { | |
sleep 10 | |
env | |
while true | |
do | |
(set -x; etcdctl --endpoints="$ETCD_ADVERTISE_CLIENT_URLS" --cacert="$ETCD_CA_FILE" --cert="$ETCD_CERT_FILE" --key="$ETCD_KEY_FILE" snapshot save "$HELPER_SNAPSHOT_DIR"/$(date +%s).db) | |
(cd "$HELPER_BACKUP_DIR" && ls -tp | grep -v '/$' | tail -n +100 | xargs -d '\n' rm -- {}) | |
sleep 60; | |
done | |
} | |
do_backup() { | |
epoch=$(stat --printf='%Y' /var/lib/etcd/current/member/snap/db) | |
(set -x; cp /var/lib/etcd/current/member/snap/db "$HELPER_BACKUP_DIR"/"$epoch".db) | |
} | |
do_restore() { | |
if [[ $(find "$HELPER_SNAPSHOT_DIR" -name '*.db' -print | wc -l) == "0" ]]; then | |
echo "no snapshot exists, quit" | |
exit 0 | |
# TODO: init ? | |
fi | |
if [[ -e ${ETCD_DATA_DIR}/member/snap ]]; then | |
echo "file exists, skip restore from snapshot" | |
exit 0 | |
fi | |
latest=$(find "$HELPER_SNAPSHOT_DIR" -name '*.db' -print0 | xargs -r -0 ls -1 -t | head -1) | |
(set -x; etcdutl snapshot restore "$latest" --data-dir "$ETCD_DATA_DIR" --skip-hash-check) | |
} | |
case $1 in | |
start) | |
do_snapshot & | |
exec etcd | |
;; | |
restore) | |
do_restore | |
;; | |
backup) | |
do_backup | |
;; | |
*) | |
echo "Usage: $0 {snapshot|restore|backup}" | |
exit 1 | |
;; | |
esac; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Unit] | |
Description=etcd key-value store | |
Documentation=https://github.com/etcd-io/etcd | |
After=network-online.target local-fs.target remote-fs.target time-sync.target | |
Wants=network-online.target local-fs.target remote-fs.target time-sync.target | |
RequiresMountsFor=/var/lib/etcd/current/ | |
ConditionPathIsMountPoint=/var/lib/etcd/current/ | |
[Service] | |
User=root | |
Type=notify | |
Environment=ETCD_DATA_DIR=/var/lib/etcd/current/ | |
Environment=ETCD_NAME=%m | |
Environment=HELPER_SNAPSHOT_DIR=/var/lib/etcd/snapshots/ | |
EnvironmentFile=/etc/default/etcd | |
ExecStartPre=/usr/local/bin/etcd-helper.sh restore | |
ExecStart=/usr/local/bin/etcd-helper.sh start | |
ExecStopPost=/usr/local/bin/etcd-helper.sh backup | |
Restart=always | |
RestartSec=10s | |
LimitNOFILE=40000 | |
[Install] | |
WantedBy=multi-user.target |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment