Backup your files with tar

If you need a one-liner command to backup your files you can use tar, its quite powerful in its usage and you can also exclude items you dont want.

I have a lot of git repos or terraform directories, node-modules etc. that takes up a lot of space, and you can use regular expressions to exclude them.

Backup Example

If you want to backup your ~/workspace directory, but you would like to exclude files such as:

  • *.ldb
  • .git/*
  • node_modules/*
  • etc

You can use this:

tar -zcvf ~/backups/backup-$(date +%F).tar.gz \
  --exclude --exclude "*.ldb" \
  --exclude "*/.git/*" \
  --exclude "*/.terraform/*" \
  --exclude "*/site-packages/*" \
  --exclude "*/node_modules/*" ~/workspace'

You can also create a alias for that:

alias backupnow='tar -zcvf ~/backups/backup-$(date +%F).tar.gz --exclude --exclude "*.ldb" --exclude "*/.git/*" --exclude "*/.terraform/*" --exclude "*/site-packages/*" --exclude "*/node_modules/*" ~/workspace'

now you should be able to run backupnow on demand.