diff --git a/update-restic.sh b/update-restic.sh new file mode 100644 index 0000000..38598ca --- /dev/null +++ b/update-restic.sh @@ -0,0 +1,48 @@ +#!/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 + +# Clone the repository +git clone $GIT_REPO +cd restic-docker + +# 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