Skip to content

Commit

Permalink
Improved README.md, etcd-backup.sh and etcd-restore.sh files and adde…
Browse files Browse the repository at this point in the history
…d .gitignore file.
  • Loading branch information
Abdullah Khawer committed Oct 28, 2022
1 parent 1f1bdbd commit 730daa8
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 54 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
# Add any directories, files, or patterns you don't want to be tracked by version control.
.git
.git
etcd-backup.json
etcd-backup-unformatted.json
etcd-backup-json*.tar
35 changes: 26 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

## Introduction

Simple etcd Backup Restore is a repository having simple bash/shell scripts to backup or restore data in etcd in JSON format.
Simple etcd Backup Restore is a repository having simple bash/shell scripts to backup/restore data from/to etcd using JSON format.

Basically, the backup script actually fetches all the keys along with their values from the latest revision and store them in a file in a JSON format while the restore script actually puts all the keys along with their values using the JSON format file that we just prepared.

Expand Down Expand Up @@ -37,28 +37,45 @@ Following are the components used in this framework:
### Backup

```
etcd-backup.sh - Backup data in etcd in JSON format.
etcd-backup.sh - Backup data from etcd in .json format file. Compress and/or upload it in .tar format to AWS S3 bucket if desired.
Usage: etcd-backup.sh [Options]
Options:
-h, --help show brief help
--host=ETCD_HOST specify etcd host (e.g., 172.168.0.4:2379)
--user=ETCD_USER specify etcd username (e.g., root)
--password=ETCD_PASSWORD specify etcd password (e.g., password)
--compress=ENABLE_COMPRESSION specify whether to compress data or not (e.g., true)
--s3-bucket=BACKUP_S3_BUCKET specify AWS S3 bucket name with path (e.g., my-s3-bucket/backups)
--s3-bucket=BACKUP_S3_BUCKET_PATH specify AWS S3 bucket name with path if any (e.g., my-s3-bucket/backups)
Examples:
etcd-backup.sh
etcd-backup.sh --host=172.168.0.4:2379
etcd-backup.sh --host=172.168.0.4:2379 --compress=true
bash etcd-backup.sh
bash etcd-backup.sh --host=172.168.0.5:2379
bash etcd-backup.sh --host=172.125.0.5:2379 --compress=true
bash etcd-backup.sh --host=172.168.0.5:2379 --compress=true --s3-bucket=my-s3-bucket/backups
```

### Restore

```
???
etcd-restore.sh - Restore data to etcd from .json format file. Decompress and/or download it in .tar format from AWS S3 bucket if desired.
Usage: etcd-restore.sh [Options]
Options:
-h, --help show brief help
--host=ETCD_HOST specify etcd host (e.g., 172.168.0.4:2379)
--user=ETCD_USER specify etcd username (e.g., root)
--password=ETCD_PASSWORD specify etcd password (e.g., password)
--decompress=ENABLE_DECOMPRESSION specify whether to decompress data or not (e.g., true)
--s3-bucket=BACKUP_S3_BUCKET_PATH_FILE specify AWS S3 bucket name with path and file name if any (e.g., my-s3-bucket/backups/etcd-backup-json-2022-10-28-16-50.tar)
Examples:
bash etcd-restore.sh
bash etcd-restore.sh --host=172.168.0.5:2379
bash etcd-restore.sh --host=172.125.0.5:2379 --decompress=true
bash etcd-restore.sh --host=172.168.0.5:2379 --decompress=true --s3-bucket=my-s3-bucket/backups/etcd-backup-json-2022-10-28-16-50.tar
```

#### *Any contributions, improvements and suggestions will be highly appreciated.*
58 changes: 30 additions & 28 deletions etcd-backup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,30 @@ TIMESTAMP=`date +%F-%H-%M`
ETCD_HOST="0.0.0.0:2379"
ETCD_USER=""
ETCD_PASSWORD=""
ENABLE_COMPRESSION=false
BACKUP_S3_BUCKET=""
ENABLE_COMPRESSION=""
BACKUP_S3_BUCKET_PATH=""

# handle input
while test $# -gt 0; do
case "$1" in
-h|--help)
echo "etcd-backup.sh - Backup data in etcd in JSON format."
echo " "
echo "etcd-backup.sh - Backup data from etcd in .json format file. Compress and/or upload it in .tar format to AWS S3 bucket if desired."
echo ""
echo "Usage: etcd-backup.sh [Options]"
echo " "
echo ""
echo "Options:"
echo "-h, --help show brief help"
echo "--host=ETCD_HOST specify etcd host (e.g., 172.168.0.4:2379)"
echo "--user=ETCD_USER specify etcd username (e.g., root)"
echo "--password=ETCD_PASSWORD specify etcd password (e.g., password)"
echo "--compress=ENABLE_COMPRESSION specify whether to compress data or not (e.g., true)"
echo "--s3-bucket=BACKUP_S3_BUCKET specify AWS S3 bucket name with path (e.g., my-s3-bucket/backups)"
echo " "
echo "--s3-bucket=BACKUP_S3_BUCKET_PATH specify AWS S3 bucket name with path if any (e.g., my-s3-bucket/backups)"
echo ""
echo "Examples:"
echo "etcd-backup.sh"
echo "etcd-backup.sh --host=172.168.0.4:2379"
echo "etcd-backup.sh --host=172.168.0.4:2379 --compress=true"
echo "bash etcd-backup.sh"
echo "bash etcd-backup.sh --host=172.168.0.5:2379"
echo "bash etcd-backup.sh --host=172.125.0.5:2379 --compress=true"
echo "bash etcd-backup.sh --host=172.168.0.5:2379 --compress=true --s3-bucket=my-s3-bucket/backups"
exit 0
;;
--host*)
Expand All @@ -48,7 +49,7 @@ while test $# -gt 0; do
shift
;;
--s3-bucket*)
export BACKUP_S3_BUCKET=`echo $1 | sed -e 's/^[^=]*=//g'`
export BACKUP_S3_BUCKET_PATH=`echo $1 | sed -e 's/^[^=]*=//g'`
shift
;;
*)
Expand All @@ -61,29 +62,30 @@ done
ETCDCTL_BASE_COMMAND="etcdctl --endpoints=$ETCD_HOST"
if [ "$ETCD_USER" ] && [ "$ETCD_PASSWORD" ]
then
ETCDCTL_BASE_COMMAND="etcdctl --user=$ETCD_USER --password=$ETCD_PASSWORD --endpoints=$ETCD_HOST"
ETCDCTL_BASE_COMMAND="etcdctl --user=$ETCD_USER --password=$ETCD_PASSWORD --endpoints=$ETCD_HOST"
fi

# take etcd backup in .json file
index=1
echo "{" > etcd-backup-unformatted.json
eval "$ETCDCTL_BASE_COMMAND" get "" --prefix=true \
| while read LINE; do index=$((index+1)) && if [ $((index%2)) -eq 0 ]; then printf "\"${LINE}\":"; else echo "\"${LINE}\""; fi; done \
| sed '$!s/$/,/' >> etcd-backup-unformatted.json
echo "}" >> etcd-backup-unformatted.json
cat etcd-backup-unformatted.json | jq '.' > etcd-backup.json
echo "etcd backup in JSON format saved in etcd-backup.json file."
echo "{" > etcd-backup.json
eval "$ETCDCTL_BASE_COMMAND get '' --prefix=true" \
| while read LINE; do index=$((index+1)) && if [ $((index%2)) -eq 0 ]; then printf "\"${LINE}\":"; else echo "\"${LINE}\""; fi; done \
| sed '$!s/$/,/' >> etcd-backup.json
echo "}" >> etcd-backup.json
JSON_DATA=$(cat etcd-backup.json)
echo "$JSON_DATA" | jq '.' > etcd-backup.json
echo "etcd backup in .json format saved in etcd-backup.json file."

# compress backup file into .tar adding timestamp in name
if [ "$ENABLE_COMPRESSION" ]
# compress backup .json file into .tar file with timestamp in name
if [ "$ENABLE_COMPRESSION" = "true" ]
then
tar -cf etcd-backup-json-$TIMESTAMP.tar etcd-backup.json
echo "etcd backup file compressed in etcd-backup-json-$TIMESTAMP.tar file."
tar -cf etcd-backup-json-$TIMESTAMP.tar etcd-backup.json
echo "etcd backup .json file compressed in etcd-backup-json-$TIMESTAMP.tar file."
fi

# upload compressed files to S3
if [ "$BACKUP_S3_BUCKET" ]
# upload compressed backup .tar file on AWS S3 bucket
if [ "$BACKUP_S3_BUCKET_PATH" ]
then
/usr/local/bin/aws s3 cp etcd-backup-json-$TIMESTAMP.tar s3://$BACKUP_S3_BUCKET/etcd-backup-json-$TIMESTAMP.tar
echo "etcd backup uploaded on AWS S3 bucket."
fi
aws s3 cp etcd-backup-json-$TIMESTAMP.tar s3://$BACKUP_S3_BUCKET_PATH/etcd-backup-json-$TIMESTAMP.tar
echo "etcd compressed backup .tar file uploaded on AWS S3 bucket."
fi
97 changes: 81 additions & 16 deletions etcd-restore.sh
Original file line number Diff line number Diff line change
@@ -1,24 +1,89 @@
#!/bin/bash
set -e

if [ -z "$1" ]; then echo "ERROR: JSON Backup File Name from S3 is Empty." && exit 0; fi
# create default variables
TIMESTAMP=`date +%F-%H-%M`
ETCD_HOST="0.0.0.0:2379"
ETCD_USER=""
ETCD_PASSWORD=""
ENABLE_DECOMPRESSION=""
BACKUP_S3_BUCKET_PATH_FILE=""

# Create variables
BACKUP_S3_BUCKET_NAME=${BACKUP_S3_BUCKET_NAME}
ETCD_HOST=$(hostname -i):2379
ETCD_USER=$(/usr/local/bin/aws ssm get-parameter --name ${ETCD_USERNAME_PARAMETER_NAME} --with-decryption --output text --query Parameter.Value)
ETCD_PASSWORD=$(/usr/local/bin/aws ssm get-parameter --name ${ETCD_PASSWORD_PARAMETER_NAME} --with-decryption --output text --query Parameter.Value)
# handle input
while test $# -gt 0; do
case "$1" in
-h|--help)
echo "etcd-restore.sh - Restore data to etcd from .json format file. Decompress and/or download it in .tar format from AWS S3 bucket if desired."
echo ""
echo "Usage: etcd-restore.sh [Options]"
echo ""
echo "Options:"
echo "-h, --help show brief help"
echo "--host=ETCD_HOST specify etcd host (e.g., 172.168.0.4:2379)"
echo "--user=ETCD_USER specify etcd username (e.g., root)"
echo "--password=ETCD_PASSWORD specify etcd password (e.g., password)"
echo "--decompress=ENABLE_DECOMPRESSION specify whether to decompress data or not (e.g., true)"
echo "--s3-bucket=BACKUP_S3_BUCKET_PATH_FILE specify AWS S3 bucket name with path and file name if any (e.g., my-s3-bucket/backups/etcd-backup-json-2022-10-28-16-50.tar)"
echo ""
echo "Examples:"
echo "bash etcd-restore.sh"
echo "bash etcd-restore.sh --host=172.168.0.5:2379"
echo "bash etcd-restore.sh --host=172.125.0.5:2379 --decompress=true"
echo "bash etcd-restore.sh --host=172.168.0.5:2379 --decompress=true --s3-bucket=my-s3-bucket/backups/etcd-backup-json-2022-10-28-16-50.tar"
exit 0
;;
--host*)
export ETCD_HOST=`echo $1 | sed -e 's/^[^=]*=//g'`
shift
;;
--user*)
export ETCD_USER=`echo $1 | sed -e 's/^[^=]*=//g'`
shift
;;
--password*)
export ETCD_PASSWORD=`echo $1 | sed -e 's/^[^=]*=//g'`
shift
;;
--decompress*)
export ENABLE_DECOMPRESSION=`echo $1 | sed -e 's/^[^=]*=//g'`
shift
;;
--s3-bucket*)
export BACKUP_S3_BUCKET_PATH_FILE=`echo $1 | sed -e 's/^[^=]*=//g'`
shift
;;
*)
break
;;
esac
done

# Download compressed backup json file from S3
/usr/local/bin/aws s3 cp s3://$BACKUP_S3_BUCKET_NAME/backups/$1 etcd-snapshot-json.tar
# prepare etcdctl command
ETCDCTL_BASE_COMMAND="etcdctl --endpoints=$ETCD_HOST"
if [ "$ETCD_USER" ] && [ "$ETCD_PASSWORD" ]
then
ETCDCTL_BASE_COMMAND="etcdctl --user=$ETCD_USER --password=$ETCD_PASSWORD --endpoints=$ETCD_HOST"
fi

# Decompress backup json file
tar -xf etcd-snapshot-json.tar etcd-snapshot.json
# download compressed backup .tar file from on AWS S3 bucket
if [ "$BACKUP_S3_BUCKET_PATH_FILE" ]
then
aws s3 cp s3://$BACKUP_S3_BUCKET_PATH_FILE etcd-backup-json.tar
echo "etcd compressed backup .tar file downloaded from AWS S3 bucket."
fi

# Restore etcd from backup json file
cat etcd-snapshot.json \
# decompress backup .tar file into .json file
if [ "$ENABLE_DECOMPRESSION" = "true" ]
then
tar -xf etcd-backup-json.tar etcd-backup.json
echo "etcd backup .tar file decompressed in etcd-backup.json file."
fi

# restore etcd backup from .json file
cat etcd-backup.json \
| jq -r $'keys[] as $k | "\'\($k)\' \'\(.[$k])\'"' \
| while read LINE; do eval "/usr/bin/etcd/etcdctl --user=$ETCD_USER --password=$ETCD_PASSWORD --endpoints=$ETCD_HOST put ${LINE}"; done
echo "etcd restored from etcd-snapshot.json"
| while read LINE; do eval "$ETCDCTL_BASE_COMMAND put ${LINE}"; done
echo "etcd backup in JSON format restored from etcd-backup.json file."

# Delete local files
rm -rf etcd-snapshot*
# delete local files
rm -rf etcd-snapshot*

0 comments on commit 730daa8

Please sign in to comment.