Q:

How to backup and restore system-level directories

I want to backup the directories as below:
/usr
/lib
/etc
/lib64
/boot
/sbin
/bin

Is there a simply to do so?
What I need is to backup these directories and I can restore them whenever I need.
I just knew about dd but it seems that dd is to backup a whole disk.

A:

DD is risky command and not recommended:
https://unix.stackexchange.com/a/141037/165083
You could use rsync instead of it but im using something like this for making backup and uploading it to AWS s3:
function backup() {
    ###Path###
    pathToContent=${directory}
    pathToBackup=/home/backup/$(date +"%Y-%m-%d").${directory}.tar.gz
    ###Backup###
    echo "Creating tar.gz file for ${pathToContent}"
    tar -czf ${pathToBackup} ${pathToContent}
    echo "Uploading ${pathToBackup} to AWS s3"
    aws s3 cp --no-progress ${pathToBackup} s3://<BUCKET>
    echo "Removing ${pathToBackup}"
    rm ${pathToBackup}
}

cd <directory>
for directory in $(ls -d */ | cut -f1 -d'/');
    do
        echo "BACKUP FOR $directory starting now."
        backup $directory
        echo "Backup for ${directory} completed successfuly."
    done

Please change <directory> :)

