Bash script to back up files I am new to bash, so I'm a bit lost with this script I made to backup some directories from the local machine into a NAS. The NAS will contain only a certain number of backed up files, so before copying a new one I delete the oldest of the existing.

First of all the script defines all the paths, directory and file names into variables

# Defines directories
ORIGEN='/Library/path-to-directories/'
DESTINAS='/Volumes/path-to-backup/'
DESTITAR='/Library/path-to-temp/'

# Defines names of the files and folders to delete (OLDDIR) and copy (NEWDIR)
OLDTAR=`/bin/ls /Volumes/path-to-backup/ | head -n 1`
NEWDIR=`/bin/ls /Library/path-to-directories/ | tail -n 1`


Then using the variable names it:
Creates a tar compressed file from the original directory. The tar file is kept in a separate, temporary directory.

/usr/bin/sudo /usr/bin/tar -czf "$DESTITAR$NEWDIR.tgz" "$ORIGEN$NEWDIR"


Deletes the older of the tar files from the NAS:

/usr/bin/sudo /bin/rm $DESTINAS$OLDTAR


Copies the tar file into the NAS:

/usr/bin/sudo /bin/cp -Rp "$DESTITAR$NEWDIR.tgz" $DESTINAS


Deletes the tar file from the temp directory:

/usr/bin/sudo /bin/rm -f "$DESTITAR$NEWDIR.tgz"


The script is run unattended by a user that has been duly authorized through changes in the sudoers file. Everything runs smoothly except in this step:

/usr/bin/sudo /bin/rm $DESTINAS$OLDTAR


The log file shows no errors but there's nothing in the place of the $OLDTAR variable, like it is not resolved, so it actually executes:

/usr/bin/sudo /bin/rm /Volumes/path-to-backup/


However if I run the commands in the terminal one by one I get it to work well. It may be a problem of user permissions?
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.