- First create a timestamp that looks something like (YearMonthDay-Hour:Minute:Second) or (20200512-02:30:00). This will give you a unique identifier.
- Then cd into the directory you want to back up.
- Create a tar.bz2 (bzip2 compression) file.
- Optionally, delet the original files.
Example:
#! /bin/bash
TIMESTAMP=$(date "+%Y%m%d-%H:%M:%S")
cd /motion
# j - bzip compress
# c - create
# f - filename
tar -jcf archive-$TIMESTAMP.tar.bz2 /path-to/files/*
rm -r /path-to/files/*
Example using bz2 compression:
tar -jcf backup.tar.gz /path/to/files/to/backup/
Note 1 If you are backing up to a connected device such as a USB drive, don't backup directly to the device itself. Backup to the mount point.
Example:
tar -jcf /mount/user/somedir/backup.tar.gz /path/to/files/to/backup/
Note 2 To exclude certain directories put exclude flags before the directory/files to backup.
Example:
tar -jcf backup.tar.gz --exclude 'node_modules' --exclude 'dir-name1' --exclude 'dir-name2' /path/to/files/to/backup/
Note 3 add the -v flag to get verbose output