Bash script to back up files
This is not a real answer, but I would pick apart your question into 4 smaller tasks.


Set up permissions / ACL so you don't need sudo if at all possible
Just set up a script to tar the files locally - and date stamp the backup
Set up a script to rsync the backup directory from the local server to the NAS. You can use the --delete option or --delete-after if you want to expire old backups over time.
Set up a script to purge dated backups in the local backup folder


Also - if you can't avoid using root /sudo - add this line tmutil snapshot before any delete and check one time to allow for local backups tmutil enablelocal in general so you have a backup if a script deletes too much. Time Machine will allow you recover rapidly from any errors in the logic of the scripts. 

Lastly - for debugging bash - run bash -x script or set debugging set -x to see the values of the variables and see if you have a path error / encoding error or something else amiss.