2026-09-22 18:09:33 +01:00
# motion_sync
2026-09-22 21:18:40 +01:00
...is a little bash script for rpi + a USB webcam that I wrote to upload motioneye vids and pics to you box account every minute for security reasons.
2026-09-22 18:09:33 +01:00
2026-09-22 21:18:40 +01:00
The idea behind it is that a thief / malicious person might be knowledgeable of the pi / motioneye setup and either cut the power to the premise before breaking in
or go to the setup pi / webcam and disconnect it from power / disconnect the USB webcam or the LAN cable...
With my script even if a break-in happens, the thief / malicious person will be captured and if properly "UPS-ed" (pi+router) have no chance in "not" getting captures
uploaded to cloud storage.
I use motioneye because is open source and free, and lightweight, so can run on a pi.
I use Box because the free account gives us 10GB
I use a Pi because it is cheap and has WiFi and LAN and USB, so a cheap WebCam will do.
It works in conjuction with motioneye and with an rclone box service I also created in /etc/systemd/user/rclone-box.service
2026-09-22 18:09:33 +01:00
## Getting started
2026-09-22 21:18:40 +01:00
Install motioneye, create a box account and do not use it for anything else.
2026-09-22 18:09:33 +01:00
2026-09-22 21:18:40 +01:00
Install rclone on the pi and connect it to Box (google it)
2026-09-22 18:09:33 +01:00
2026-09-22 21:18:40 +01:00
Once Motioneye is working and saving to /var/lib/motioneye create a systemd service that keeps Box rclone alive:
2026-09-22 18:09:33 +01:00
2026-09-22 21:18:40 +01:00
mkdir -p ~/.config/systemd/user
2026-09-22 18:09:33 +01:00
2026-09-22 21:18:40 +01:00
nano ~/.config/systemd/user/rclone-box.service
2026-09-25 10:52:12 +01:00
'''
2026-09-22 21:18:40 +01:00
[Unit]
Description=Rclone Box Mount
Documentation=man:rclone(1)
After=network-online.target
Wants=network-online.target
2026-09-22 18:09:33 +01:00
2026-09-22 21:18:40 +01:00
[Service]
Type=notify
ExecStart=/usr/bin/rclone mount box: %h/box \
--config %h/.config/rclone/rclone.conf \
--vfs-cache-mode full \
--vfs-cache-max-size 10G \
--vfs-cache-max-age 24h \
--dir-cache-time 72h \
--allow-other \
--umask 022
ExecStop=/bin/fuermount -u %h/box
Restart=on-failure
RestartSec=10s
2026-09-22 18:09:33 +01:00
2026-09-22 21:18:40 +01:00
[Install]
WantedBy=default.target
2026-09-25 10:52:12 +01:00
'''
2026-09-22 21:18:40 +01:00
Finally, create 2 crontab entries, one to keep the Box OAuth token alive and one to run my script every minute:
2026-09-22 18:09:33 +01:00
2026-09-22 21:18:40 +01:00
sudo crontab -e
2026-09-22 18:09:33 +01:00
2026-09-22 21:18:40 +01:00
# Keep Box OAuth token alive (runs weekly, e.g., every Sunday at 3 AM)
0 3 * * 0 rclone lsd box: >/dev/null 2>&1
2026-09-22 18:09:33 +01:00
2026-09-22 21:18:40 +01:00
# Sync motion recordings and cleanup (runs every minute)
* * * * * /home/$USER/sync_motion.sh >/dev/null 2>&1
2026-09-22 18:09:33 +01:00
2026-09-22 21:18:40 +01:00
My script does the following for you:
2026-09-22 18:09:33 +01:00
2026-09-22 21:18:40 +01:00
# 1. Ensures log directory exists
2026-09-22 18:09:33 +01:00
2026-09-22 21:18:40 +01:00
# 2. Keeps log file under ~5 MB by trimming it when it gets too large
2026-09-22 18:09:33 +01:00
2026-09-22 21:18:40 +01:00
# 3. Remuxes MKV to MP4 (so the videos work on the pi motioneye webserver)
2026-09-22 18:09:33 +01:00
2026-09-22 21:18:40 +01:00
# 4. Local cleanup (Batch Trim down to 9GB)
2026-09-22 18:09:33 +01:00
2026-09-22 21:18:40 +01:00
# 5. Syncs to BOX with rclone (Reflects local 9GB state & deletes missing remote files)