egregius.be

Urban Exploration, PHP and others…

Synology btrfs take snapshot only when needed

Earlier this week I bought a spare 4TB drive. Reason for this was to copy the data from my Synology DS1513+ that isn’t backed up in the cloud so I could convert the 8 year old ext4 volume to btrfs. Now that it’s done and everything is restored I had a look at the snapshot feature of btrfs. Damn that’s fast and interesting. A snapshot takes only couple of seconds to complete, in my opinion the size and number of files doesn’t matter much. With this feature I could get rid of my old versioning system created with rsync.

Of course there are downsides on the Synology implementation, but hey, it wouldn’t be me if I would found things working exactly as I expected.

Let me try to explain what I find a downside. Quite simple: the scheduler. You can set the default scheduler options like daily, every Friday, etc. And as a frequency every 5 minutes, every hour, every 4 hours and numerous other options. Great you would think… But I don’t want a snapshot every hour. I don’t need that. My diskstation is mainly used as a backup of our OneDrive, I don’t need a hourly snapshot and not even a daily snapshot. OneDrive does that fine on it’s own.
Another problem I found was that if a schedule is missed it doens’t get executed asap. My diskstation goes into sleep mode when it’s not used. It’s only powered up when we need something from it or weekly at Friday night to take backups of all the different systems in the house and the webserver.

With those issues in mind and the knowledge of a previously created script I started writing a shell script that is executed at shutdown of the diskstation. The script checks several shared folders for the last modified file. The timestamp of that file is stored in a file. If the timestamp is different of the previous timestamp the snapshot is taken. Easy, fast, comfortable. No more unneeded snapshots, no more wasted space no mather how small a snapshot is.

Anyway, the script is available at github: https://gist.github.com/Egregius/3fc5afef60f91de7a141d9efc010fbc9
The script is put in the scheduler at shutdown. With the scheduled wake at Friday night I have at least every week a snapshot of all OneDrive accounts, if something is changed.