Skip to content

Instantly share code, notes, and snippets.

@lexuzieel
Created February 24, 2022 13:14
Show Gist options
  • Select an option

  • Save lexuzieel/29717b046e72c115d82ea9dad2affe95 to your computer and use it in GitHub Desktop.

Select an option

Save lexuzieel/29717b046e72c115d82ea9dad2affe95 to your computer and use it in GitHub Desktop.
Backup git diff or file to S3
#!/bin/bash
input=""
prefix=""
bucket=""
while [[ "$#" -gt 0 ]]; do
case $1 in
-p | --prefix)
shift
prefix="/$1"
;;
-b | --bucket)
shift
bucket="$1"
;;
*)
input="$1"
;;
esac
shift
done
if [ -z "$bucket" ]; then
echo "Bucket name is required"
exit 1
fi
if [ -d "$input" ]; then
cd $input
if [ ! -d .git ]; then
echo "Specified directory is not a git repository"
exit 1
fi
diff=$(git diff --name-only)
files=($diff)
pwd="$(pwd)"
echo "Backing up modified tracked files in '$pwd' to S3 bucket '$bucket'..."
for file in "${files[@]}"; do
path="$pwd/$file"
aws s3 cp "$path" "s3://$bucket$prefix$path"
done
elif [ -f "$input" ]; then
pwd="$(pwd)"
path="$pwd/$input"
echo "Backing up individual file to S3 bucket '$bucket'..."
aws s3 cp "$path" "s3://$bucket$prefix$path"
else
echo "No file or directory '$input'"
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment