I've got a question that I can't seem to find the answer to.
I'm trying to write a shell script to do a full system backup using tar. Obviously, there are several folders I do no want to backup (/dev, /mnt, /proc, /tmp, and so on). There are a fair number of folders I do no want backed up so I'm using the 'exclude-from' flag to keep the command relatively clean.
The problem is, it's not working.
Here are the tar flags I'm using:
--create
--file $BACKUP_FILE
--preserve-permissions
--same-owner
--numeric-owner
--listed-incremental $INCREMENTAL_FILE
--level=0
--xz
--directory /
--exclude-caches
--exclude-from $EXCLUDE_FILE
-v
Here is my exclude file:
/dev
/lost+found
/media
/mnt
/proc
/run
/sys
/tmp
/var/spool
/var/run
/var/tmp
When I run my script it still backups up those folders I don't want backed up. Switching to a bunch of '--exclude' flags doesn't seem to work (and I would rather not use them). I've tried changing '--exclude-from $EXCLUDE_FILE' to '--exclude-from=$EXCLUDE-FILE', I've made sure there's no whitespace after each line and a few other things (can't remember them, things are starting to blur together).
I've been reading up on tar patterns so see if there is something I'm missing and I haven't come across anything yet. I'm tried looking to see if this has something to do with tar stripping leading slashes for security purposes but I haven't found anything yet.
While Googling the only thing I found that sort of helps was one guy that was doing the same thing and solved it by removing the leading '/' from his exclude file entries. I've tried it and it works, but it excludes ALL folders rather than the root ones. I will likely have a 'media' folder in my home and I want to back that one up but not the /media folder so this isn't a proper solution.
Although the ArchWiki includes instructions on how to perform a full system backup with tar, you probably want to use rsync instead. The ArchWiki also describes how to perform a full system backup with rsync. As root, run:
rsync -aAXv --exclude={"/dev/*","/proc/*","/sys/*","/tmp/*","/run/*","/mnt/*","/media/*","/lost+found"} /* /path/to/backup/folder
The -aAX
set of options will ensure that 'archive mode' is used. This ensures that symbolic links, devices, permissions and ownerships, modification times, ACLs and extended attributes are preserved.
Note that rsync can also read exclude patterns from file, so your use case may look something like:
rsync -aAXv --exclude-from=FILE /* /path/to/backup/folder
If you plan on backing up your system somewhere other than /mnt
or /media
, do not forget to add it to the list of exclude patterns to avoid an infinite loop. Also, if there are any bind mounts in the system, they should be excluded as well, so that the bind mounted contents are copied only once.
So go figure, I found a possible solution 30 minutes after posting. If I change my exclude file entries to start with './' (rather than '/') it seems to work. This makes sense because I'm changing the directory to root so ./media === /media.
However, I'm not going to mark this as the answer just yet. I would very much like to keep my exclude file entries as '/' rather than './' so that it's obvious that I'm excluding the the root folders. Yes, I can leave a comment in the exclude file saying the current directory is the root directory, but if I can I would prefer to avoid it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With