Home > database >  Unable to use --exclude-from when piping tar to gpg for encrypted backup
Unable to use --exclude-from when piping tar to gpg for encrypted backup

Time:07-07

I am writing a bash script I can use to backup my home directory, and obviously there are a lot of hidden directories I don't need to backup. So I made a file simply called 'excludes'. I call it in my script for backing up and it doesn't register and immediately starts backing up my Download folder, which I want to exclude.

Here is the script:

#!/bin/bash
DATE=$(date  %Y-%m-%d-%H%M%S)
BACKUP_DIR="/run/media/user/backups"
SOURCE="/home/user"
tar -cvzpf - $SOURCE --exclude-from="/home/user/excludes" | gpg -c > $BACKUP_DIR/backup-$DATE.tar.gz.gpg

If I just compress the directory the --exclude-from works fine. But when I pipe it to pgp it ignores it and tries to compress everything, which includes a lot of stuff that I don't want in the backup.

I feel like I'm just not understanding how piping works here or something simple. Any help is appreciated.

CodePudding user response:

Well... piping is not going to affect the outcome of what is in the backup... but one thing likely to lead to trouble, and which could be related to your issues, is that you should always avoid making tar backups with absolute paths. The reason is, when you restore, tar will want to restore to the original path, and if the original path is absolute, you'll have to restore over the top of your home folder, which you may not always want to do. And this could be affecting your script if you're running it from different directories, or it can't find the exclude file, so I'd recommending using cd first to go where you want.

#!/bin/bash
DATE=$(date  %Y-%m-%d-%H%M%S)
BACKUP_DIR="/run/media/user/backups"
SOURCE="/home/user"
cd "$SOURCE"
tar -cvzpf - . --exclude-from="excludes" | gpg -c > $BACKUP_DIR/backup-$DATE.tar.gz.gpg

CodePudding user response:

I modified my own code and switched to an array for excluding. This didn't fix the problem, but it did make me find it. I realized there was a directory that had spaces in it and when I copied it to my exclude file I hadn't caught the apostrophes in it and didn't escape them.

Modified and working script is basically same as above. It functions as expected. It was formatted correctly:

#!/bin/bash
DATE=$(date  %Y-%m-%d-%H%M%S)
BACKUP_DIR="/backups"
SOURCE="dir"
cd "$SOURCE"

tar --exclude={'dir-to-exclude'} -cvzpf - $SOURCE | gpg -c > $BACKUP_DIR/backup-$DATE.tar.gz.gpg
  • Related