There's no need to stop weewx in my experience so you don't need any while loop.
This one comes up periodically every year or so and there are 'many' threads with pointers to other folks' scripts and approaches in the archives of this google group. That said, I do it a couple ways - I do nightly copies (gzipped) of all my databases - and I do occasional copies of all the pertinent software excluding databases. Here are my two scripts for a 'pip' installation. Hopefully the comments are self-explanatory. I renamed them to end in .txt to make clicking to read them easier. Note the 'exclude' in the script that backs up the software itself which skips backing up the database(s) which the other script handles nightly. On Monday, September 21, 2026 at 4:24:07 PM UTC-7 David Schulz wrote: > OK, That's not how you do a code block here - apologies. And no edit!?!?! > > On Tuesday, September 22, 2026 at 9:22:44 AM UTC+10 David Schulz wrote: > >> Hi >> I'm attempting to write my first bash script to backup weewx. I'm >> struggling with the syntax of a condition for a while loop that checks if >> weewx has stopped or is still stopping. >> >> I don't really understand the syntax of the example conditions I can find >> for various tutorials on using while to create a loop. I cannot find >> anywhere an exact example of using 'systemctl is-active' as the condition >> for a while loop. >> >> Appreciate any feedback from people more learned than I. >> >> TIA >> >> Here's what I have at the moment. If I run this script when I know weewx >> won't be writing to the database, the commands to zip the files into the >> backup directory all work OK. >> >> I don't think I want to be copying the database file in the middle of a >> write. So I have a separate script with the backup section removed just >> trying to test the stopping, check has stopped and start parts of the >> script. I've been unsuccessfully futzing around with different condition >> syntax and actively trying to run it when weewx is active and the stop >> command will take a while to execute while the weewx main loop completes. >> But TBH I'm just wandering about in the dark. >> >> ``` >> #!/bin/bash >> # >> # Stop weewx service >> echo $(date): Weewx is stopping >> /var/log/weewx-backup.log >> systemctl stop weewx >> # >> # Check service has stopped >> while [ systemctl -q is-active weewx ]; >> do >> sleep 1 >> echo $(date): Weewx is still stopping >> /var/log/weewx-backup.log >> done >> echo $(date): Weewx has stopped >> /var/log/weewx-backup.log >> # >> # Copy files to backup folder >> echo $(date): Backup is starting >> /var/log/weewx-backup.log >> tar -czvf /mnt/backup/weewx-conf.tar.gz /etc/weewx >> tar -czvf /mnt/backup/weewx-db.tar.gz /var/lib/weewx/weewx.sdb >> echo $(date): Backup is complete >> /var/log/weewx-backup.log >> # >> # Start weewx service >> systemctl start weewx >> echo $(date): Weewx is starting >> /var/log/weewx-backup.log >> ``` >> > -- You received this message because you are subscribed to the Google Groups "weewx-user" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion visit https://groups.google.com/d/msgid/weewx-user/a3388c06-b942-4742-aaf3-afa57156ea93n%40googlegroups.com.
backup-weewx-software.sh.txt
Description: application/shellscript
weewx-backup-copy.sh.txt
Description: application/shellscript
