restic-docker/update-restic.sh
Laurent Le Houerou 0d908159c1 Update update-restic.sh to use a temporary directory for cloning and cleanup
- Modify the script to create a unique temporary directory for cloning the repository
- Change the script to remove the temporary directory after execution
- Ensure the repository is cloned into the temporary directory and cleaned up after the script is done running
2023-03-27 10:34:22 +04:00

53 lines
1.7 KiB
Bash

#!/bin/bash
# Set variables
GIT_REPO="https://git.lehouerou.net/laurent/restic-docker"
DOCKER_REGISTRY="registry.lehouerou.net/restic-backup"
DOCKERFILE="Dockerfile"
EMAIL_RECIPIENT="laurent@lehouerou.net"
EMAIL_SUBJECT="Restic Docker image updated"
POSTFIX_CONFIG="/etc/postfix/main.cf"
# Install necessary packages
sudo apt-get update
sudo apt-get install -y git curl jq postfix
# Configure Postfix if not already configured
if ! grep -q "^relayhost" $POSTFIX_CONFIG; then
echo "Please configure Postfix and try again."
exit 1
fi
# Create a temporary directory and clone the repository
TEMP_DIR=$(mktemp -d)
git clone $GIT_REPO $TEMP_DIR
cd $TEMP_DIR
# Get the latest version of Restic
LATEST_VERSION=$(curl --silent "https://api.github.com/repos/restic/restic/releases/latest" | jq -r .tag_name | tr -d v)
# Get the current Restic version from the Dockerfile
CURRENT_VERSION=$(grep -oP 'ENV RESTIC_VERSION=\K[^"]+' $DOCKERFILE)
# Compare versions and update Dockerfile if needed
if [ "$LATEST_VERSION" != "$CURRENT_VERSION" ]; then
sed -i "s/ENV RESTIC_VERSION=$CURRENT_VERSION/ENV RESTIC_VERSION=$LATEST_VERSION/" $DOCKERFILE
# Commit the change and push to the repository
git add $DOCKERFILE
git commit -m "Update Restic to version $LATEST_VERSION"
git push
# Build the new Docker image and push it to the registry
docker build -t $DOCKER_REGISTRY:$LATEST_VERSION .
docker push $DOCKER_REGISTRY:$LATEST_VERSION
# Send email notification
echo "The Restic Docker image has been updated to version $LATEST_VERSION and pushed to $DOCKER_REGISTRY" | mail -s "$EMAIL_SUBJECT" $EMAIL_RECIPIENT
else
echo "The Restic Docker image is already up to date (version $CURRENT_VERSION)."
fi
# Clean up the temporary directory
cd ..
rm -rf $TEMP_DIR