Earlier when upgrading a WordPress site that is running on a CentOS Linux server I needed to quickly backup the entire site to then perform an upgrade of the core WordPress installation. There are multiple directories located beneath the primary site folder that I didn’t need to backup including some caching directories as well as a database backup directory since all of those directories are not required to restore the site in case of an issue during the WP upgrade. Use the information below to create a tar archive while excluding multiple sub directories.
Create Tar Archive While Excluding Multiple Sub Directories:
Previously I wrote an article on creating a tar archive when excluding a single directory, which is located here, however that article is missing numerous tips that come in handy as well as the process for excluding multiple sub directories. The below example command shows how to exclude multiple sub directories and is followed by a couple tips when excluding directories as well as a real world example after the tips.
Example Linux tar Command Excluding Multiple Directories:
- tar -czvf /path/to/some/dir/tar-archive.tgz --exclude '/path/to/direcotry/to/tar/sub-dir1' --exclude '/path/to/direcotry/to/tar/sub-dir2' /path/to/direcotry/to/tar
As you can see you need to include the –exclude switch for each directory you want to exclude. Also make sure to include single or double quotes around the full path to each directory you are excluding. Last but not least make sure you do not include the trailing slash at the end of the directory you are excluding as I have made that mistake and it won’t exclude the directory properly many times.
Real World Example Linux tar Command Excluding Multiple Directories:
- tar -czvf /var/www/example.com/web.backup/WEB_06-13-2012.tgz --exclude '/var/www/example.com/web/wp-content/cache' --exclude '/var/www/example.com/web/wp-content/widget-cache' --exclude '/var/www/example.com/web/backups' /var/www/example.com/web/
You can exclude as many directories as you need to as long as you use the –exclude switch for each one. Once the tar process is completed you should have a backup file that is compressed without the contents of the directories you excluded.