Last active
May 22, 2017 06:14
-
-
Save suewonjp/b421e66f6a87dee728aae3f670154f5d to your computer and use it in GitHub Desktop.
Bash Functions for Simple Directory Backup
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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__
folderAll 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.
The backup snapshots will be named as follows to show their time of saving: