Practical Automation
Let's put your new skills to the test by writing a script that creates compressed backups of directories.
The tar Command
tar (Tape Archive) is the standard tool for combining files into a single archive, often compressed with gzip.
# Create a gzip compressed archive
tar -czvf backup.tar.gz /path/to/directory
The Goal
Your script should:
- Accept a directory path as the first argument (
$1). - Check if that directory exists using an
ifstatement. - If it doesn't exist,
echoan error andexit 1. - If it does exist, create a
tar.gzarchive of it in the/tmpdirectory. - Use
$(date +%Y%m%d)to add the current date to the backup filename.
Give it a try!
booting...