Avoid tar: Removing leading `/' from member names message in backup scripts

Posted by Orville Bennett on 11 October 2014
Read time: about 3 minutes

I recently figured out how to get rid of an annoying message that came from the tar backup program that I'd like to share. I have servers that I've set up to do hosting for some clients. Since they're actually paying for this service I also perform regular backups. This would be pretty annoying to do manually every day so I have a cron job run the backup script each day.

The script I wrote used tar to create the backup file but was also printing out the warning

tar: Removing leading `/' from member names

This was incredibly annoying because the end result was an email from cron, with that message, every single time the script ran. I had two backup scripts run every day. Eventually, even though that message didn't affect the backups at all --- they were still working --- I decided to look into it to remove the message and prevent cron from emailing me spurious messages.

That was the prologue. Here's what you came for: The Solution.

My first try at this, after looking up the documentation, still failed, and I had no idea why. According to the documentation for tar using the -C switch was the solution.

-C directory
	In c and r mode, this changes the directory before adding the following files.

According to the docs, I could instruct tar to switch directories first, and if it did that, it wouldn't need to strip the / from names instead. I changed my script from the previous format of

tar -cjf $destination.tar.bz2 /file/path1 /file/path2

And changed it to

tar -C / -cjf $destination.tar.bz2 /file/path1 /file/path2

And nothing changed! Disappointing to say the least. I went back to my standard web developer debugging tool when things get hard and created a test case. A simple backup command with the least arguments possible that still created a viable backup.

After doing that I realized where I went wrong. You see even though I was switching the the root directory /, I was still including the leading / with the file paths I was backing up. Since this was a script and the file paths to backup up were actually defined early on they weren't in view and I didn't even think about them being present.

After telling tar to switch to the / directory and then removing the leading / from the file paths the error did, indeed go away. You can see a modified, fixed backup command below:

tar -C / -cjf $destination.tar.bz2 file/path1 file/path2

As you can see once we switch to / we then use relative file paths to point tar to the location of the directories (or files) we want backed up.

There you have it, the way to prevent the tar: Removing leading '/' from member names message is a two-fold process.

  1. Invoke tar with the -C switch pointed at the root directory, i.e. -C /
  2. Change your file paths from absolute to relative by removing all the leading slashes / from them.

Comments? @reply to @opinion8d_logic on twitter.