Script for Nightly SnapRAID Syncs and Scrubs14 min read

UPDATE: I have an updated script available here.

I needed a simple, reliable way to run diffs, syncs, and scrubs on my SnapRAID array nightly without me manually having to run commands each time. Β This SnapRAID script does that, and will stop if there are issues, and it provides a nightly email alert of the status of the array. Β Let’s take a look at getting this setup.

You will likely not want to use this as is, because your parity files are likely named differently than mine. Also, I use Mutt to send out system emails in lieu of the /usr/bin/mail like the default script uses. If you want to use Mutt, you just need to install it. I have updated my updated script again for 6.0.

I normally make a scripts directory for my root user, and put all scripts there.

Here is the revised script… again. I have added rudimentary scrubbing capabilities to the script. I had it coded for incremental and full scrubbing, but after getting it working, the idea seemed stupid to me, so I rolled back to a weekly whole array scrub. Here’s the revised version. Let me know if you run into any issues or have ideas for improvements in the comments.

When you are done, make it executable.

Finally, add it to the root crontab to run every night at 11:30pm.

and paste…


I love learning new things and trying out the latest technology.

You may also like...

37 Responses

  1. lockheed says:

    Hi. I wondered if you could help me out. I tried using this script, after adopting it to my arch box, but after it run for few minutes, it quits with the following:

    I doubt it has anything to do with the fact that my data partitions are on BTRFS, while parity disk is ext4.

    • Zack says:

      What output do you get when you when snapraid diff or snapraid sync. Also, if this is a brand new snapraid array, you will want to run a full sync in a tmux session first before you try to use this script via cronjob.

  2. Syphonx says:

    Hey, was wondering if you could help with the following error I get when the script runs:

    [Sat Sep 17 04:30:01 SAST 2016] Running DIFF Command.
    [Sat Sep 17 04:30:01 SAST 2016] ERROR – failed to get one or more count values. Unable to proceed. Exiting script.

    I can run the diff command fine manually, there are usually a couple hundred changes every night due to Sonarr updating NFO’s. I can also run sync fine manually so not sure why the script is failing.

    Diff Command:

    WARNING! Inodes are not persistent for disks: ‘d2’, ‘d3’, ‘d4’, ‘d5’, ‘d6’. Move operations won’t be optimal.

    169121 equal
    6 added
    0 removed
    453 updated
    0 moved
    0 copied
    0 restored
    There are differences!

    Anything I need to edit in this part of the script?

  3. codgedodger says:

    Amazing script! Thanks Zack, one question though. It will email me perfectly, but in the email with all the information laid out nicely (thanks for that) I have this stated at the end. Now granted this is a new snapraid array and doesn’t have much data on it yet but just curious what it means by this:

    SnapRAID SYNC Job finished on Mon Oct 3 23:33:00 EDT 2016
    WARNING – check output of SYNC job. Could not detect marker . Not proceeding with SCRUB job.

  4. codgedodger says:

    root@ubuntu14:~# snapraid scrub
    Self test…
    Loading state from /var/snapraid.content…
    Using 8 MiB of memory for the FileSystem.
    Using 112 MiB of memory for 32 blocks of IO cache.
    Nothing to do
    Saving state to /var/snapraid.content…
    Saving state to /media/data/disk1/snapraid.content…
    Saving state to /media/data/disk2/snapraid.content…
    Verifying /var/snapraid.content…
    Verifying /media/data/disk1/snapraid.content…
    Verifying /media/data/disk2/snapraid.content…

    • codgedodger says:

      When snapraid sync was ran it would come with this error

      WARNING! All the files previously present in disk ‘d2’ at dir ‘/media/data/disk2/’
      are now missing or rewritten!

      Just ran snapraid –force-empty sync there’s nothing it could harm since this whole 85TB array only has 300gb worth of content on it which is easily replaced if worst comes to worst. I’m letting it start the sync right now. Might be awhile. lol

  5. mtompkins says:

    Recommend a minor change:

    This will ignore any comment lines that may exist. When I make a change in a config file often I comment out the original during testing until I’m sure of the expected result. This will allow your search to continue to work during that interim phase.

    • Zack says:

      Thanks! I actually have an even more involved version of this on my local copy, because I’m using SnapRAID v11 Beta’s split parity functionality like this.

      I would agree that yours is a useful addition, so I will update the tutorial. Thanks!

      • mtompkins says:

        Thanks for that – when we all move to v11 it will definitely come in handy.

        I thought I would also share my version of the script which evolved from your core.
        Major differences (that come to mind):
        – Parsed and created functions for manageability
        – Added programmatic management of services
        — As an example, I run Sophos AV on-access scanning. This greatly impacts the snapraid processes as files are accessed. I’ve added a routine to stop & start the av (which can easily be extended to any systemctl services). The trap (next bullet) restores the service(s) on a clean exit or if CTRL-C
        – Added a trap for ‘cleanup’ (restoring services)
        – Moved to markdown / HTML email (and have put in a request for snapraid to export graphs and tables in that format)

        Feel free to incorporate anything you like. URL:

        • Zack says:

          Thanks for the post! I have really intended to break out this script into functions to make it more readable. This has been the case where it works for me, so I haven’t fiddled with it πŸ™‚ There are still a few more pieces in your script that I would like to “functionize”, but this is awesome, and a good examples. I have already extended my local version to manage pausing/unpausing certain Docker containers while the script starts and stops, so this is another great inclusion on your part. The idea of integrating a markdown output into SnapRAID is also great (I saw your post on the forums).

        • Zack says:

          After looking at this script more. This is very well done! I added only one function to pull that out of the main function. Also, I changed the snapraid bin path to point to /usr/local/bin/snapraid like the default installed version. I also replaced mailx with mutt again and removed the markdown support on my local version (I just need simple, plain text here).

          Finally, I changed the search for the parity files to create an array, and then iterate through it to test the existence of all parity files. This could be simplified further, but it would make it more difficult to read and understand.

        • Zack says:

          I have posted a revised version of your script, with credit, as a new post. I have included all of the modifications I made to support dual parity and the things I mentioned previously here.

          • mtompkins says:

            I really liked the old script but think your new version is a significant improvement in readability and management. Kudos to your efforts all around. Thanks also for the credit.

            I’m sure the community will appreciate the changes.

            [Aside: I also found myself borrowing from your drive standby script which works well in concert with snapraid – thx again!]

          • Zack says:

            Thanks for all of updates with the new script. It works very well πŸ™‚

  6. rebels1405 says:

    This blog has been awesome! I can’t thank you enough for taking the time to put this together. It has made it so much easier for someone with very little linux experience to put together a nice server. I do have one question that I hope you can help me with…I had my server set up and everything was working well with the automated nightly sync and e-mails that I would get each night. Due to to a networking issue, I had to set everything back up from scratch, the snapraid drives all did amazing and they picked back up like nothing had even happened. I can’t tell you how relieved I was that I didn’t lose anything even with a complete new OS set up from scratch. Here is my question: I followed all of the steps again, but for some reason, none of my cron jobs are running anymore. I followed your instructions again, and I had the same issue last time where they did’t work, but I stumbled on the answer somewhere else, but I can’t seem to figure it out again. Any ideas on what I can try to get my scripts running each night? I was logged in a root when I created the /root/scripts and when I did the crontab -e setup. I can run the scripts manually and I get the e-mail and everything runs, but they are not running automatically. Any ideas? Thanks for everything!

    • Zack says:

      Hello, sorry for the delayed response. Have you set the script as executable?

      Also, you may want to expand your available paths in your crontab. Try to add this above the minute, hour, day, etc line…

  7. phuriousgeorge says:

    I see you’re stopping nzbget and sonarr. Would it be possible to add a user-configurable array of services to stop during a sync? I’m working on adding specific functions for other docker services I have running, but thought it might be good to be able to easily configure later on.

    Again, great work, I absolutely love this blog and habe shared it amongst similar enthusiasts!

  8. lockheed says:

    Great script, but I am having a problem. I need to rise a threshold level of removed files, because it does not work as is:
    SnapRAID DIFF Job finished on Sun 15 Jan 05:56:59 CET 2017
    SUMMARY of changes – Added [12] – Deleted [143] – Moved [0] – Copied [0] – Updated [0]
    Number of deleted files (143) exceeded threshold (50).
    Forced sync is enabled.
    1 warning(s) till forced sync. NOT proceeding with sync job.

    How can I do that?

    • Zack says:

      You just need to modify the script and increase the value in this line to be greater than 143.

    • Zack says:

      Or, you can just manually run the sync by hand one time if you have deleted a bunch of files. This is probably safer than modifying the script just for one run.

  9. Dulanic says:

    Should the “service” idea be clarified? I had to dig into the code to determine if it meant a true service or a container.

    • Zack says:

      Hello, thanks for the comment. I have the new version of this script < ahref="" target="_blank">here that mentions these are for Docker containers. Although, I would love to create scripts that covers ever scenario, these scripts are just things I use myself that I publish for others when I’m not working my real job or with my wife and kids πŸ™‚

      If you have ideas, I’ll happily listen, but offering code yourself would be much more helpful. In both scripts, managing systemd services could be easily accomplished by removing the Docker components and adding calls to /usr/sbin/service.

  10. farrinux says:


    I have a small bug with “Script for Nightly SnapRAID Syncs and Scrubs” It’s with the email. I use sSMTP to send mail. The script works except for how it treats sSMTP. sSMTP requires a “To:” a “From” and then the “Subject” when sending. When I run the script it does not set the email address correctly. Even though the email variable is set correctly. I’ve included the error message below. (See Last Line)
    “WARNING! Content files on the same disk: ‘/var/snapraid.content’ and ‘/var/snapraid.2content’.
    WARNING! Content files on the same disk: ‘/sraid1/snapraid.content’ and ‘/sraid1/snapraid.2content’.
    WARNING! Content files on the same disk: ‘/sraid2/snapraid.content’ and ‘/sraid2/snapraid.2content’.
    WARNING! Content files on the same disk: ‘/media1/snapraid.content’ and ‘/media1/snapraid.2content’.
    ssmtp: RCPT TO: (553 5.1.2> is not a valid RFC-5321 address. n16sm4110560ywh.105 – gsmtp)”
    The “COMPLETED” on the first part of the email address is being pulled From “$EMAIL_SUBJECT_PREFIX INFO – $JOBS_DONE Jobs COMPLETED” I think the problem is with the order of the “subject ” and “Email Address” but not sure.
    One last thing, as you can see above from the errors, that I got warnings about the “Content” files. I set snapraid up to have 2 content files but I put both content files on several different disks, is this allowable? ie. each disk has a content 1 and content2 file.

  11. mon0 says:

    sorry, forgot to add the error
    Oct 16 07:15:48 nas sSMTP[30416]: RCPT TO: (501 5.1.3 Bad recipient address syntax)

    My previous snapraid install was on 18.04 and everything was running smooth.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.