Search
 
SCRIPT & CODE EXAMPLE
 

SHELL

bash home backup script linux

#!/bin/bash
    
# This bash script is used to backup a user's home directory to /tmp/.
function backup {
    
    if [ -z $1 ]; then
        user=$(whoami)
    else 
        if [ ! -d "/home/$1" ]; then
                echo "Requested $1 user home directory doesn't exist."
                exit 1
        fi
        user=$1
    fi 
    
    input=/home/$user
    output=/tmp/${user}_home_$(date +%Y-%m-%d_%H%M%S).tar.gz
    
    function total_files {
        find $1 -type f | wc -l
    }
    
    function total_directories {
        find $1 -type d | wc -l
    }
    
    function total_archived_directories {
        tar -tzf $1 | grep  /$ | wc -l
    }
    
    function total_archived_files {
        tar -tzf $1 | grep -v /$ | wc -l
    }
    
    tar -czf $output $input 2> /dev/null
    
    src_files=$( total_files $input )
    src_directories=$( total_directories $input )
    
    arch_files=$( total_archived_files $output )
    arch_directories=$( total_archived_directories $output )
    
    echo "########## $user ##########"
    echo "Files to be included: $src_files"
    echo "Directories to be included: $src_directories"
    echo "Files archived: $arch_files"
    echo "Directories archived: $arch_directories"

    if [ $src_files -eq $arch_files ]; then
        echo "Backup of $input completed!"
        echo "Details about the output backup file:"
        ls -l $output
    else
        echo "Backup of $input failed!"
    fi
}
    
for directory in $*; do
    backup $directory 
    let all=$all+$arch_files+$arch_directories
done;
    echo "TOTAL FILES AND DIRECTORIES: $all"
Comment

PREVIOUS NEXT
Code Example
Shell :: check size of folder linux 
Shell :: Git lab global setup 
Shell :: squash commits git 
Shell :: install python mac m1 
Shell :: linux disk usage by directory one level 
Shell :: create a new git repository on the command line 
Shell :: get the latest from remote git 
Shell :: download file via ssh with port 
Shell :: Rename File Extension In folder 
Shell :: grep capture group 
Shell :: git patch staged to file 
Shell :: how to set two wallpaper in ubuntu 
Shell :: pull down a branch locally 
Shell :: django view sql behind migration 
Shell :: insert text before cursor in vi 
Shell :: pycocotools install error 
Shell :: digitalocean connect via ssh 
Shell :: Linux get bluetooth mac address 
Shell :: digit sum number in c 
Shell :: task manager ubuntu 
Shell :: arch linux grub boot loader installed 
Shell :: install docker machine 
Shell :: bash modify file text 
Shell :: bash tokenize string 
Shell :: redis cache start 
Shell :: kali linux download 
Shell :: Failed to connect to repository : Error performing git command: git ls-remote -h 
Shell :: git squase to rename author 
Shell :: scp bash command 
Shell :: remove homebrew tap 
ADD CONTENT
Topic
Content
Source link
Name
7+2 =