Skip to content

Instantly share code, notes, and snippets.

@jayunit100
Last active July 15, 2022 21:05
Show Gist options
  • Save jayunit100/095f1d638cceb5ba0920095b9601cf42 to your computer and use it in GitHub Desktop.
Save jayunit100/095f1d638cceb5ba0920095b9601cf42 to your computer and use it in GitHub Desktop.
backup restore prototype
################ DO NOT RUN THIS SCRIPT EVERR IT WILL BREAK YOUR CLUSTER !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
### TODO, read in these parameters...
management_nodes=( "10.92.6.140" )
function backup_etcd {
echo "killing etcd"
for node in ${management_nodes[@]}; do
# ssh capv@${t} systemctl stop kubelet
echo "backup etcd to etcd_backup directory"
ssh capv@$node "sudo rm -rf etcd_backup ; sudo mkdir etcd_backup"
ssh capv@$node "sudo cp -r /var/lib/etcd/ etcd_backup"
echo "done, size =="
ssh capv@$node "sudo du --summarize --human-readable etcd_backup"
done
}
function kill_etcd {
for node in ${management_nodes[@]}; do
# another backup just to be careful
folder=`date +"%s"`
echo "wiping out etcd data !!!!!!!!!!"
ssh capv@$node "mkdir -p etcd_backup/$folder ; pushd etcd_backup/$folder ; kill `ps -ax | grep etcd | grep peer | cut -d' ' -f 1` ; mv /var/lib/etcd/ ."
popd
done
}
function snapshot_etcd {
rm jay.db
for node in ${management_nodes[@]}; do
ssh capv@$node "pushd etcd_backup/ ; rm jay.db ; sudo /usr/bin/etcdctl --endipoints='https://localhost:2379' --cacert='/etc/kubernetes/pki/etcd/ca.crt' --cert='/etc/kubernetes/pki/etcd/server.crt' --key='/etc/kubernetes/pki/etcd/server.key' snapshot save jay.db"
ssh capv@$node "pushd etcd_backup/ ; ls -altrh jay.db"
done
}
function restore_etcd {
# TODO do this for all nodes in a for loop
# only do this on one node
for node in ${nodes[@]}; do
ssh capv@$node ls m1* ; rm -rf m1* ; echo "removed the m1 files..."
ssh capv@$node ETCDCTL_API=3 etcdctl snapshot restore jay.db --name m1 --initial-cluster m1=https://${node}:2380 --initial-cluster-token etcd-cluster-1 --initial-advertise-peer-urls=https://${node}:2380 ;
done
}
snapshot_etcd
#backup_etcd
#kill_etcd
#wait_for_k8s_to_die
#restore_etcd
#wait_for_k8s_to_come_back
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment