1

For now, I'm using motion to create snapshots and place them in /home/pi/motion. I am using FileZilla to be able to access that directory and to then move the images inside.

Is there a way I can make this process automatic, or a program that can do this for me? I am connected to the Raspberry Pi headless using SSH.

  • Have you looked at rsync ?unix.stackexchange.com/questions/14191/… Have a look at this question. I believe rsync is available on mac. And like, rob suggested, You can write a script which gets executed every minute or something that would run rsync to grab new files from Rpi. Hope it helps. – dhruvvyas90 Jul 30 '15 at 13:50
1

in the motion.conf file is the setting

on_picture_save

I used the following to automate motion images being uploaded to dropbox and then deleting them from the Pi

on_picture_save /home/pi/dropbox/Dropbox-Uploader/dropbox_uploader.sh -f /home/pi/.dropbox_uploader upload %f %f && rm %f

I would suggest you work out a script using rsync if you do not want to use dropbox.

For more information about the dropbox uploader read this blog post http://raspi.tv/2013/how-to-use-dropbox-with-raspberry-pi

| improve this answer | |
1

I have a script that I use for that purpose using sftp. It's below. You'll probably need to change some things but it should work fine as long as you have not changed the default file name formats. It also deletes the files after they have been moved. I run it daily at midnight using cron. It moves everything that's not from the current day. This way it cleans up after itself if you miss a few days, and it doesn't mess with files that may be in the process of writing.

You'll need to enable sftp on your mac. Go into System Preferences -> Sharing and enable remote login. You need to know our Mac's IP address on your local network, your userid on your Mac, and your Pwd on your Mac. There is a guide here. You'll also want to log into your Mac from your Pi via ssh at least once before running the script. This will store the ssh key for you. The script will fail if you don't do this.

This is not the most secure solution as your Mac's user/pwd is in clear text in the file. There are workarounds to this but it wasn't worth the trouble in my case.

You'll also need to install pysftp and path.py.

sudo pip install pysftp path.py

If you don't have pip installed then fix that

sudo apt-get install python-pip

Please no comments on my comments / lack thereof. This wasn't meant for public consumption. I'm also not a programmer, so excuse the poor programming.

Minimal things you need to change:

  • python location / version on the first line
  • user / pwd / destination server / dest directory
  • source dir

Hope it's useful for you.


#!/usr/local/bin/python3
import sys
import datetime
import pysftp
import re
from path import path

SOURCE_DIR = '/home/weather/sec_video'
DEST_DIR = '/mnt/usbstorage/security'

today = datetime.date.today()

def now():
    return datetime.datetime.now()


print("{1} Moving footage not from {0}".format(today, now()))

# We search for file names with this pattern to identify files from today
today_str = "{0}{1:02d}{2:02d}".format(today.year, 
        today.month, today.day)

# Get the search pattern to identify the day to which a file belongs
# file names look like this "14-20150702090025.avi"
# Pattern looks for 4 digits of year, then two digits of month, then two of day
# pattern is anchored in file name with '-'
p = re.compile("-(\d\d\d\d)(\d\d)(\d\d)")

files_to_move = []
files_to_delete = []

src_dir = path(SOURCE_DIR)
for f in src_dir.files():
    if today_str in f.name or 'lastsnap' in f.name:
        print("{1} Not moving {0}".format(f.name, now()))
        continue
    files_to_move.append(f.name)
    files_to_delete.append(f)

with pysftp.Connection('192.168.0.224', username='USER', 
        password='PASSWORD') as sftp:
    with pysftp.cd(SOURCE_DIR):
        for i in files_to_move:
            sftp.chdir(DEST_DIR)
            # First get the date part of the file name
            m = p.search(i)
            if m: # we found a matching pattern
                # Now create dir if doesn't exist and cd to that dir
                # group(xx) corresponds to the chars matching the ()'s
                # in the compile statment from above
                dir_name = "{0}-{1}-{2}".format(m.group(2), m.group(2), 
                        m.group(1))
                if not sftp.isdir(dir_name):
                    print("{0} Directory {1} does not exist, creating".format(
                            now(), dir_name))
                    sftp.mkdir(dir_name)
                sftp.chdir(dir_name)

            if not sftp.exists(i):
                print("{1} Moving {0}".format(i, now()))
                sftp.put(i, preserve_mtime=True)
            else:
                print("{1} Not moving {0} - already exists remotely".format(
                    i, now()))

for f in files_to_delete:
    print("{1} Removing {0}".format(f.name, now()))
    f.remove()
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.