In case you have a local instance of Gitea to manage version control of your scripts, apps and/or websites, you can also manage your scripts this way.
The idea is to have a copy of all the work in /opt and commit that to Gitea. We will create a simple deploy.sh script that can be used in the future if you download a newer version and want to deploy it from the otherwise passive git repo on /opt to the actual ‘live folders’.
IMPORTANT: Once implemented, you should not be making any changes directly to the live locations (such as under /etc/apcupsd) but within /opt and then using the deploy.sh script to copy them (for testing) before committing the changes locally and to your origin server.
- Assuming that you have an instance of Gitea, you would need to make a copy of the scripts into one folder. The approach below will show you how – let’s utilize
/optfor this purpose.
# Create a new folder sudo mkdir -p /opt/ups-scripts # Copy your existing scripts there sudo cp /usr/local/sbin/ups_manager.sh /opt/ups-scripts sudo cp /usr/local/sbin/push_ups_to_kuma.sh /opt/ups-scripts # Copy the on & onbattery scripts to your new to-be-git managed folder sudo cp /etc/apcupsd/onbattery /opt/ups-scripts sudo cp /etc/apcupsd/offbattery /opt/ups-scripts
- Create a
deploy.shscript:
nano /opt/ups-scripts/deploy.sh #!/bin/bash # # This script deploys all custom scripts from the /opt/ups-scripts (SOURCE) # to their live (DESTINATION) locations. # # Run this script any time you make a change in the /opt/ups-scripts repo. # set -e # Exit immediately if any command fails echo "Starting deployment of UPS scripts..." SOURCE_DIR="/opt/ups-scripts" # === LIVE LOCATIONS === DEST_CRON_SCRIPTS="/usr/local/sbin" DEST_APC_SCRIPTS="/etc/apcupsd" # --- 1. Deploy Cron/Sbin Scripts --- echo "Deploying cron scripts to $DEST_CRON_SCRIPTS..." sudo cp "$SOURCE_DIR/ups_manager.sh" "$DEST_CRON_SCRIPTS/ups_manager.sh" sudo cp "$SOURCE_DIR/push_ups_to_kuma.sh" "$DEST_CRON_SCRIPTS/push_ups_to_kuma.sh" # Ensure they are executable sudo chmod +x "$DEST_CRON_SCRIPTS/ups_manager.sh" sudo chmod +x "$DEST_CRON_SCRIPTS/push_ups_to_kuma.sh" # --- 2. Deploy apcupsd Event Scripts --- echo "Deploying apcupsd scripts to $DEST_APC_SCRIPTS..." sudo cp "$SOURCE_DIR/onbattery" "$DEST_APC_SCRIPTS/onbattery" sudo cp "$SOURCE_DIR/offbattery" "$DEST_APC_SCRIPTS/offbattery" # Ensure they are executable sudo chmod +x "$DEST_APC_SCRIPTS/onbattery" sudo chmod +x "$DEST_APC_SCRIPTS/offbattery" echo "" echo "--------------------------------" echo "Deployment successful!" echo "Run 'git commit' and 'git push' to save your changes." echo "--------------------------------"
- Add them to Gitea
cd /opt/ups-scripts sudo chown -R your_user:your_user . git init # In case your default branch is called 'master' rather than 'main' git branch -m main git add . git commit -m "Initial commit of all UPS management scripts." # You will need to create the repository on your Gitea server first git remote add origin <http://your-gitea-server/your-user/ups-scripts.git> git push -u origin main
- You might ask ‘why not to just symlink the live destinations, such as in
/etc/apcupsd? You totally could but what happens if you run agit pulland then have a conflict? On a production server that is time-sensitive to disruption, this could be an issue – for this reason, I recommend to follow the best practices from start. Enjoy!
Voila, all done! Just remember to following the GitOps method of deployment:
- Edit the files in
/optand then run thedeploy.shscript. - If you are happy with the changes, then commit them and push them.
- If you make changes to the repo in Gitea, then run
git pullon your local instance and, once confirmed that it downloaded properly (no conflicts, etc.), then run thedeploy.shscript again.
