Skip to content

Instantly share code, notes, and snippets.

@suewonjp
Last active May 22, 2017 06:14
Show Gist options
  • Save suewonjp/b421e66f6a87dee728aae3f670154f5d to your computer and use it in GitHub Desktop.
Save suewonjp/b421e66f6a87dee728aae3f670154f5d to your computer and use it in GitHub Desktop.
Bash Functions for Simple Directory Backup
checkBackupTarget() {
[ ! "$1" ] \
&& echo "Usage: $FUNCNAME [ directory-to-backup ]" \
&& echo "Note: the path should be relative" \
&& return 2
local tgtPath=$1
[ ! -d "$tgtPath" ] \
&& printf "$FUNCNAME : \"%s\" does NOT exist!!!\n" "$tgtPath" \
&& return 1
[[ ${tgtPath:0:1} = "/" || ${tgtPath:0:1} = "~" ]] \
&& echo "$FUNCNAME : Absolute path is not allowed!!!" \
&& return 1
return 0
}
pushBackup() {
checkBackupTarget $1 || return 1
local tgtPath=$1
local absSrcPath=$( realpath "$tgtPath" )
local backupPath="$HOME/__backup__$absSrcPath"
local dateTime=$( date "+%y-%m-%d_%Hh%Mm%Ss" )
mkdir -p "$backupPath" \
&& backupPath="${backupPath}/${dateTime}.bz" \
&& tar jcf "$backupPath" "$tgtPath" \
&& echo "$FUNCNAME : new backup created as => $backupPath"
}
printBackups() {
checkBackupTarget $1 || return 1
local tgtPath=$1
local backupPath="$HOME/__backup__$PWD/$tgtPath"
[ ! -d "$backupPath" ] && printf "$FUNCNAME : \"%s\" does NOT exist!!!\n" "$backupPath" && return 1
__stored_backup_files__=() ### Output from this function
cd $backupPath
local iii
for file in *; do
iii=${#__stored_backup_files__[@]}
printf "\t[%d] %s\n" $iii $file
__stored_backup_files__[$iii]=$file
done;
cd "$OLDPWD"
}
alias listBackups='printBackups'
deleteBackup() {
printBackups $1
echo "$FUNCNAME : Please pick a version to delete from ( 0..$(( ${#__stored_backup_files__[@]} - 1 )) )"
echo "$FUNCNAME : Or press n to quit without doing anything"
local index
read index
[[ $index = "n" || $index -lt 0 || $index -ge ${#__stored_backup_files__[*]} ]] && echo "$FUNCNAME : Exit without doing anything" && return
local tgtPath=$1
local backupPath="$HOME/__backup__$PWD/$tgtPath"
local targetFile="${backupPath}/${__stored_backup_files__[$index]}"
unset __stored_backup_files__
echo "$FUNCNAME : You have chosen \"$targetFile\""
tput setaf 6
rm -rfi "$targetFile"
tput sgr 0
}
pullBackup() {
printBackups $1
local tgtPath=$1
local backupPath="$HOME/__backup__$PWD/$tgtPath"
echo "$FUNCNAME : Please pick a version to restore from ( 0..$(( ${#__stored_backup_files__[@]} - 1 )) )"
echo "$FUNCNAME : Or press n to quit without doing anything"
read index
[[ $index = "n" || $index -lt 0 || $index -ge ${#__stored_backup_files__[*]} ]] && echo "$FUNCNAME : Exit without doing anything" && return
targetFile=${__stored_backup_files__[$index]}
unset __stored_backup_files__
echo "$FUNCNAME : You have chosen $targetFile"
if [ -e "$tgtPath" ]; then
# If $tgtPath exists, rename it rather than overwriting it
local dateTime=$( date "+%y-%m-%d_%Hh%Mm%Ss" )
local tmpPath="$dateTime--$tgtPath"
printf "$FUNCNAME : '%s' exists... renaming it to '%s'\n" "$tgtPath" "$tmpPath"
mv "$tgtPath" "$tmpPath"
fi
tar jxf "$backupPath/$targetFile" && printf "$FUNCNAME : Now '%s' has been restored from '%s'\n" "$tgtPath" "$targetFile"
}
@suewonjp
Copy link
Author

suewonjp commented Jun 7, 2016

HOW TO:

These Bash functions manage multiple backups for an arbitrary directory and all of its subdirectories.
All backup files (snapshots) will be compressed and stored at ~/__backup__ folder

All commands accept a name of a directory to backup.
I recommend you to run the commands from the target folder's parent directory.
[NOTE] Specifying an absolute path as a parameter is not allowed.

  • Create a backup for the current snapshot of a folder
    • $ pushBackup my-work
  • List all snapshots
    • $ listBackups my-work
  • Restore the folder from one of the snapshots
    • $ pullBackup someData
  • Delete all the snapshot
    • $ deleteBackup someData

The backup snapshots will be named as follows to show their time of saving:

$ listBackups  my-work
    [0] 16-04-20_12h06m49s.bz
    [1] 16-04-21_12h42m57s.bz
    [2] 16-04-25_12h36m21s.bz
    [3] 16-04-29_16h05m19s.bz
    [4] 16-04-29_18h43m33s.bz
    [5] 16-04-30_12h21m15s.bz
    [6] 16-05-02_11h12m14s.bz

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment