Skip to content

Instantly share code, notes, and snippets.

@vitalyp
Last active August 29, 2015 14:07
Show Gist options
  • Save vitalyp/35d87b123c5efbd05918 to your computer and use it in GitHub Desktop.
Save vitalyp/35d87b123c5efbd05918 to your computer and use it in GitHub Desktop.
shell backup alias
# EXPORT VARIABLES
export PROJECTS_PATH=~/dev/current
export MASTER_PROJECT_NAME=ultimate-proto
export MASTER_PROJECT_PATH=$PROJECTS_PATH/$MASTER_PROJECT_NAME
# STARTUP ALIASES
cd $MASTER_PROJECT_PATH
##################
# PROJECT BACKUPER:
# Author: @vitalyp
# Env variables require:
# $MASTER_PROJECT_PATH : path to your working project (for example: ~/home/projects/automizely)
# $MASTER_PROJECT_NAME : name of your working project (for example: automizely)
# $OVERRIDE_BACKUPS_DIR: if set, overrides default backups dir (for example: ~/home/projects/automizely/../backups)
# Additional info:
# STDOUT redirect: http://stackoverflow.com/a/3403786/1005661
# ASCII CODING: http://patorjk.com/software/taag/#p=display&f=JS%20Stick%20Letters&t=BESTPRACTICE%20BACKUPE
# BASH time formatting: http://www.cyberciti.biz/faq/linux-unix-formatting-dates-for-display/
# BASH color codes: http://misc.flogisoft.com/bash/tip_colors_and_formatting
# green="\e[32m"
# cyan="\e[36m"
# blue="\e[34m"
# lightblue="\e[94m"
# black="\e[30m"
# red="\e[31m"
# darkgray="\e[90m"
# bold="\e[1m"
# norm="\e[0m"
##
__BESTPRACTICE__backupProject() {
exec 2>&1
# init variables:
projectPath=$MASTER_PROJECT_PATH
projectName=$MASTER_PROJECT_NAME
archiveFileFormat="%d%b%Y_%Hh%Mm%Ss" #-- produces timestamp-formatted string: "30Sep2014_11h54m07s"
colGRAY="\e[90m"
colNORM="\e[0m"
colGREEN="\e[32m"
# print logo
echo "*-------BESTPRACTICE BACKUPER--------*"
echo "target project: $projectPath"
echo -e '\e[36m
__ ___ __ ___ __ __ __ ___ __ ___
|__) |__ /__` | |__) |__) /\ / ` | | / ` |__
|__) |___ .__/ | | | \ /~~\ \__, | | \__, |___
__ __ __ ___ __
|__) /\ / ` |__/ | | |__) |__ |__)
|__) /~~\ \__, | \ \__/ | |___ | \
'
# reset color
echo -e "\e[0m"
#create global stash folder, if not exists
stashPath="$projectPath/../$projectName"_stash
if [ -n "$OVERRIDE_BACKUPS_DIR" ]
then
stashPath=$OVERRIDE_BACKUPS_DIR
echo "..! override backups dir with $stashPath"
fi
echo -e ".. prepare to create project backups folder: $colGRAY $stashPath .. $colNORM"
mkdir -p $stashPath
echo -e ".. project backups folder exists: $colGRAY $stashPath $colNORM"
#create project stash folder
timestamp=$(date +"$archiveFileFormat")
backupPath=$stashPath/$timestamp
echo -e ".. prepare to create backup folder: $colGRAY $backupPath $colNORM.."
mkdir -p $backupPath
echo -e ".. backup folder created: $colGRAY $backupPath $colNORM"
#project files copying
echo -e ".. prepare to copy project file $colGRAY $projectPath to $colGRAY $backupPath $colNORM.."
cp -avr $projectPath $backupPath >-
echo -e ".. project files copied to backup folder: $colGRAY $backupPath $colNORM"
#gzip backup files
archiveFilePath=$stashPath/$timestamp.tar.gz
echo -e ".. prepare to create project archive file: $colGRAY $archiveFilePath $colNORM.."
tar -zcvf $archiveFilePath $backupPath &> /dev/null
echo -e ".. archive created: $colGRAY $archiveFilePath"
#delete backup folder
echo -e ".. prepare to remove backup folder: $colGRAY $backupPath $colNORM .."
rm -rf -- "$backupPath" >-
echo -e ".. backup folder removed: $colGRAY $backupPath $colNORM"
echo -e "$colGREEN .. FINISHED $colNORM Version backup Successfully Created: $colGRAY $backupPath $colNORM"
echo -e ".. To extract archive, type 'tar -xzvf file.tar.gz'"
}
alias stash=__BESTPRACTICE__backupProject
@vitalyp
Copy link
Author

vitalyp commented Sep 30, 2014

BackupEr

This is a pretty quick your project backuper!
Really, easy to use!
To Use it, simply place its content in the end of ~/home/.bashrc

INFO: was created after I mistakely delete a not-yet-committed huge source file in the end of day, before comitting, and Local History suddenly doesn't made any trick..! (!!!SURPRIzE!!!)

This is not-for-noobs usage. If you noob, don't touch it, and go away :)

VERY VERY IMPORTANT!!

This code contains rm -rf -- "$backupPath" >-``statement. If you not sure, or set *incorrect*PROJECTS_PATHandMASTER_PROJECT_NAME` values, ( points to system root nightmare ) you will corrupt your computer.

VERY IMPORTANT to change 2 environment variables values, located in the beginning of the script:

export PROJECTS_PATH=~/projects
export MASTER_PROJECT_NAME=remote_aliaser

`PROJECTS_PATH`  - should point to your root project folder.
`MASTER_PROJECT_NAME`  - your backuping project folder name

This is enough, relaunch terminal window, and exec in bash prompt command:

$ stash

Enjoy with se_X_y colored ASCii graphics, and detailed logs.. WOW~!
If SUCCESS (it will be always - tested many times!) -> Visit your PROJECTS_PATH folder: you will find new Backup folder there, automatically created. Inside this folder, -pretty_timestamped- archive file is created and ready for storage your project in zipped archive. If you relaunch stash command again and again, and again new backups will be added to already existed.

NOTICE 1: it will backups entirely project folder. Backuping of unnecessary *.log and /tmp files will be performed also!

NOTICE 2: can be scheduled with 'cron' , to make automatically project shots.

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