DEV Community

Henk van den Brink
Henk van den Brink

Posted on

Backup MotionEye to OneDrive

Problem

MotionEye doesn't have an integration for OneDrive, so you can't backup files as easy as when using Google Drive.

But since I have 1TB of storage on OneDrive, I do want to use it :-)

Solution

So the solution is some scripting and rclone

There are a few steps that needs to be done.

  1. Setup new remote in rclone
  2. Create script that does the syncing
  3. Create log directory
  4. Update MotionEye to use the script
  5. Done.

1. Setup rclone

Either login as the user that is used by MotionEye (in my case motion)
Or execute it as root and later copy the rclone conf (and chown to right user)

Please follow the instruction from here to setup RClone OneDrive remote.
It is fairly easy, if your server does not have a browser you do need a second machine that does have a browser to finish the setup.

2. Create script for sync

Go to /etc/libmotioneye/
create a new file: backupToOneDrive.sh
ie nano backupToOneDrive.sh

!/bin/bash
requiredUser="motion" #Edit to suit username of the RCLone config
remoteFolderName="onedrive:backups/motioneye" #Edit to suit your OneDrive remote folder
remoteFolder=$1
if [ $remoteFolder = "Achtertuin" ]; then #Edit to suit your camera name in MotionEye
localFolder="Camera1" #Edit to suit you local camera folder
fi
if [ $remoteFolder = "Bijkeuken" ]; then #Edit to suit your camera name in MotionEye
localFolder="Camera2" #Edit to suit you local camera folder
fi

#Add more cameras here as required#
if [ $remoteFolder = "" ]; then
echo "ERROR, ROOT Folder" $rootFolder "NOT FOUND" > /var/log/RClone/error.txt
exit
else
rm -f /var/log/RClone/error.txt
fi
DIR="/etc/motioneye" # get cur dir of this script
progName=$(basename -- "$0")
cd $DIR
if pidof -o %PPID -x "$progName"; then
echo "WARNING - $progName Already Running. Only One Allowed." >> /var/log/RClone/upload.txt
echo "WARNING - $progName Already Running. Only One Allowed."
else

localCamFolder="/var/lib/motioneye/"$localFolder
remoteCamFolderName=$remoteFolderName"/"$remoteFolder
#echo $localCamFolder "to" $remoteCamFolderName
if [ ! -d "$localCamFolder" ] ; then # Check if Local sync Folder Exists
echo $localCamFolder "Folder NOT Found " $num
exit 1
fi
nowDay=$(date +"%d")
nowSuffix="th"
if [ $nowDay -eq 1 ]; then
nowSuffix=$nowDay"st"
fi
if [ $nowDay -eq 21 ]; then
nowSuffix=$nowDay"st"
fi
if [ $nowDay -eq 21 ]; then
nowSuffix=$nowDay"st"
fi
nowDate=$(date +"%A,%d$nowSuffix %B %Y")
logDate=$(date +"%d-%m-%Y")
nowTime1=$(date +"%T")
#echo "Upload Started on" $nowDate "at" $nowTime1
#echo $num "- Sending" $localCamFolder "to" $remoteCamFolderName
sudo -u $requiredUser rclone move -v --log-file /var/log/RClone/RCloneLog.txt $localCamFolder $remoteCamFolderName
nowTime2=$(date +"%T")
#echo "Upload Started on" $nowDate "at" $nowTime1 "- FINISHED AT" $nowTime2
echo $logDate "Started" $nowTime1 "Finished" $nowTime2 >> /var/log/RClone/uploads.txt
echo $localCamFolder "to" $remoteCamFolderName >> /var/log/RClone/uploads.txt
fi

echo "Finished uploading files"
exit 0

Enter fullscreen mode Exit fullscreen mode

note: This base of the script comes from this ticket at GitHub. So big thanks to him for creating it.

The script is altered a bit, since the original didn't work for me on Debian at least.
And it would not sync 'beyond the day'.

Let's walk through the script a bit.

In essence what it does is, it moves all the movies recorded by MotionEye from local to remote folder.
In order to do that, there are some settings that needs to be set.

There are a few important settings and options that needs to be set.

  • requiredUser
  • remoteFolderName

requiredUser
This is the user that will execute the rclone command.
So this must be the user that is used to start the motioneye process.
note: it needs to have sudo right, more on that later

remoteFolderName
You can define the folder name that must be used as root in OneDrive.

And the camera section:
MotionEye saves the movies in a folder called Camera*ID*
If you upload that, it can be hard to know which Camera is Camera1
So there is some transformation done to map the ID directory to a nice name.

Currently there are two, but you can remove or add a new one.
Just copy the if until fi

if [ $remoteFolder = "Garden" ]; then #Edit to suit your camera name in MotionEye
localFolder="Camera1" #Edit to suit you local camera folder
fi
if [ $remoteFolder = "Kitchen" ]; then #Edit to suit your camera name in MotionEye
localFolder="Camera2" #Edit to suit you local camera folder
fi
Enter fullscreen mode Exit fullscreen mode

And that is it. Script is ready, let's move on.

I am running MotionEye inside a LXC container in Proxmox and want to limit the storage, so I move the files to OneDrive to free up space.
If you don't want to move, replace it with copy in the rclone command.

3. Create log dir

Now create the log directory for rclone:

mkdir `/var/log/RClone`
Enter fullscreen mode Exit fullscreen mode

Make sure that the execution user has rights, either login as that user or chown the file.

4. Update MotionEye to use the script.

Go to your MotionEye instance.

Open the settings of a Camera.
Go to the File Storage tab
Click Run a command
And use your newly created script:

sh /etc/motioneye/backupToOneDrive.sh Garden
Enter fullscreen mode Exit fullscreen mode

The last part of the command is the name that maps to the camera ID.
So if you specified Camera1 in the script to be the Garden, use that.

5. Done!

Done. Now it will move all your recordings to your OneDrive

This is an initial setup and will update this over time, as the camera mapping part can be a bit more elegant (Use the name specified in MotionEye)
And probably having this script on GitHub with version control would help.

But for now, good luck!

Top comments (0)