Search code examples
powershellbackupsftpwinscp

WinSCP - SFTP script to backup the newest files


I have call recordings on AWS but I'm only given 10 GB of space so I would like to have a script which will SFTP to the Amazon AWS and only copy the newest files to my NAS sitting in the office. I can than use Windows Task Scheduler to run the scripts once a week.

The idea is that after all space on AWS is populated it starts to deleting the oldest recordings, so I want to prevent it and only use AWS as a buffer to store the current recordings and my NAS as a main storage. My script will be running every week/month to copy only the newest files in order to prevent loosing any recordings.

Below I have described the logic of how I would like the script to behavior:

Establish an SFTP session

if destination directory doesn't contain any files create a full backup first

if destination does contain some files copy only missing files

Looking forward for any ideas, thanks!


Solution

  • The easiest solution is to use get command with -neweronly switch:

    open sftp://user:password@example.com/
    get -neweronly /remote/path/* C:\local\path\*
    

    For a very similar results, you can also use synchronize command:

    synchronize local /remote/path C:\local\path
    

    If you want to synchronize only recent files, you can add -filemask=>2D or similar.
    See https://winscp.net/eng/docs/file_mask#size_time