2023-03-27 06:22:44 +00:00
|
|
|
#!/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
|
|
|
|
|
2023-03-27 06:34:22 +00:00
|
|
|
# Create a temporary directory and clone the repository
|
|
|
|
TEMP_DIR=$(mktemp -d)
|
|
|
|
git clone $GIT_REPO $TEMP_DIR
|
|
|
|
cd $TEMP_DIR
|
2023-03-27 06:22:44 +00:00
|
|
|
|
|
|
|
# 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
|
2023-03-27 06:34:22 +00:00
|
|
|
|
|
|
|
# Clean up the temporary directory
|
|
|
|
cd ..
|
|
|
|
rm -rf $TEMP_DIR
|