- create a file in /etc/systemd/system/ota.service
[Unit]
Description=Your Flask Application
After=network.target
[Service]
User=pi
WorkingDirectory=/home/thuhuong/ota_demo
ExecStart=/home/thuhuong/ota_demo/env/bin/python /home/thuhuong/ota_demo/__init__.py
Restart=always
[Install]
WantedBy=multi-user.target
- enable the service: sudo systemctl start ota.service
- install package: sudo apt-get install git rsync
rsync
is a powerful tool for copying and synchronizing files and directories between two locations in a fast and efficient way. It stands for "remote sync" and is commonly used for backup and mirroring tasks.
-
Incremental Transfers:
rsync
only copies the differences between the source and the destination, which minimizes the amount of data transferred and speeds up the process. -
Local and Remote Syncing: It can be used to sync files locally (on the same machine) or remotely (between different machines over a network).
-
Preserves File Attributes: It can preserve file permissions, modification times, symbolic links, and other attributes.
-
Bandwidth Efficient: It compresses data during transfer and reduces the amount of data sent over the network.
-
Versatile Options:
rsync
provides numerous options for customizing the sync process, such as excluding specific files, handling partial transfers, and running as a daemon.
The general syntax for rsync
is:
rsync [options] source destination
-
Sync a Local Directory:
rsync -a /path/to/source/ /path/to/destination/
-a
: Archive mode, which preserves permissions, timestamps, and symbolic links.
-
Sync to a Remote Server:
rsync -avz /path/to/source/ user@remote_host:/path/to/destination/
-a
: Archive mode.-v
: Verbose, provides detailed output.-z
: Compress data during transfer.
-
Sync from a Remote Server:
rsync -avz user@remote_host:/path/to/source/ /path/to/destination/
In the OTA update script provided, rsync
is used to synchronize the updated application files from the ota_updates
directory to the main application directory. This ensures that only the changes are copied, which makes the update process efficient.
rsync -a ~/ota_updates/ /path/to/your/application/
This command:
- Uses
-a
(archive mode) to preserve file attributes. - Copies all updated files from
~/ota_updates/
to/path/to/application/
.
#!/bin/bash
cd ~/ota_updates
git fetch
LOCAL=$(git rev-parse HEAD)
REMOTE=$(git rev-parse @{u})
if [ $LOCAL != $REMOTE ]; then
echo "Repository is outdated. Updating…"
git pull
rsync -a ~/ota_updates/ /path/to/your/application/
sudo systemctl restart your_application.service
else
echo "Repository is up to date."
fi
cron
is a time-based job scheduler in Unix-like operating systems. It enables users to schedule scripts or commands to run automatically at specified intervals or times. It's widely used for automating repetitive tasks such as system maintenance, backups, and monitoring.
- Scheduling Tasks: Allows scheduling tasks to run at specific times, dates, or intervals.
- Automated Execution: Tasks run automatically without user intervention.
- Versatility: Can schedule tasks to run daily, weekly, monthly, or at more complex intervals.
- User-Specific Cron Jobs: Each user can have their own crontab (cron table) file to schedule their tasks.
- Cron Daemon (
crond
): The background service that checks the crontab files and executes the scheduled tasks. - Crontab File: The configuration file where users define their scheduled tasks. Each line in the file represents a cron job.
The crontab file format consists of five time-and-date fields followed by the command to be executed:
* * * * * command_to_run
- - - - -
| | | | |
| | | | +----- Day of the week (0 - 7) (Sunday is both 0 and 7)
| | | +------- Month (1 - 12)
| | +--------- Day of the month (1 - 31)
| +----------- Hour (0 - 23)
+------------- Minute (0 - 59)
-
Run a script every day at midnight:
0 0 * * * /path/to/script.sh
-
Run a script every hour:
0 * * * * /path/to/script.sh
-
Run a script every 5 minutes:
*/5 * * * * /path/to/script.sh
-
Run a script at 2:30 PM every day:
30 14 * * * /path/to/script.sh
-
Edit Crontab File:
crontab -e
This opens the crontab file in the default text editor.
-
List Crontab Entries:
crontab -l
Displays the current user's crontab entries.
-
Remove Crontab File:
crontab -r
Deletes the current user's crontab file.
In your OTA update setup, you use cron
to run the update script at regular intervals to check for updates and apply them if available. Here’s how it fits in:
-
Creating the Crontab Entry: Open the crontab file for editing:
crontab -e
-
Add the Update Script to Run Every Minute:
* * * * * /home/pi/ota_updates/update.sh
This ensures the update script runs every minute to check for updates and apply them if needed.
Here's a detailed guide to set up OTA (Over-The-Air) updates for your Python application running on Raspberry Pi with the code hosted on GitHub.
-
Install the necessary dependencies: You need
git
andrsync
to manage your code updates.sudo apt-get install git rsync
-
Clone your GitHub repository to your Raspberry Pi: Replace
your_username
andyour_repository
with your actual GitHub username and repository name.git clone https://github.com/your_username/your_repository.git cd your_repository
-
Create a new directory for your OTA updates:
mkdir ~/ota_updates
-
Copy the latest version of your application to the
ota_updates
directory: Replace/path/to/your/application
with the actual path to your application.rsync -a /path/to/your/application/ ~/ota_updates/
-
Create a script that will check for updates and download them if necessary:
nano ~/ota_updates/update.sh
Add the following content to the script:
#!/bin/bash cd ~/ota_updates git fetch LOCAL=$(git rev-parse HEAD) REMOTE=$(git rev-parse @{u}) if [ $LOCAL != $REMOTE ]; then echo "Repository is outdated. Updating…" git pull rsync -a ~/ota_updates/ /path/to/your/application/ sudo systemctl restart your_application.service else echo "Repository is up to date." fi
Make the script executable:
chmod +x ~/ota_updates/update.sh
-
Set the script to run automatically using cron:
crontab -e
Add the following line to the crontab file to run the script every minute:
* * * * * /home/pi/ota_updates/update.sh
-
Test the script:
bash ~/ota_updates/update.sh
Ensure the latest version of your application is running on your Raspberry Pi.
-
Installing Dependencies:
git
: Used for cloning and updating your repository.rsync
: Used for copying files and directories.
-
Cloning Repository:
- Clones the GitHub repository to your Raspberry Pi.
-
Creating
ota_updates
Directory:- A separate directory to manage the OTA updates.
-
Copying Application to
ota_updates
:- Ensures that the latest version of your application is in the
ota_updates
directory.
- Ensures that the latest version of your application is in the
-
Creating the Update Script:
- The script checks for updates by comparing the local and remote repository states.
- If an update is found, it pulls the changes and syncs them to the application directory.
- Restarts the service to apply updates.
-
Setting up Cron Job:
- Adds a cron job to run the update script every minute, ensuring the application is always up-to-date.
-
Testing the Script:
- Manually runs the script to verify that it works correctly.
- Version Control: Use Git to manage your code, allowing easy tracking and rollback of changes.
- Staging Environment: Test updates in a staging environment before deploying to production.
- Secure Connection: Use HTTPS for Git operations to ensure a secure connection.
For more detailed documentation, you can refer to: