#!/bin/bash backup_dir="/var/dobackup-pool" command="dobackup" function showHelp { echo "usage: $command filepath_relative_or_absolute [comment]" echo "The backup file will be stored in the directory $backup_dir." echo "Its filename (once stored) will the the absolute path (in which the character / is replaced by two dashes) + the actual filename + a timestamp" echo "The comment will be stored in the same directory; the filename will be the one of the backuped file plus the postfix 'comment'." echo "" exit } function getAbsolutePath { input_path=$(readlink -f $1) } # input arguments input_filename=$1 comment=$2 ############################################ ## BEGIN - SHOW HELP ############################################ show_help=0 if [ "$input_filename" = "-h" ] then showHelp fi if [ "$input_filename" = "--help" ] then showHelp fi if [ "$input_filename" = "" ] then showHelp fi ############################################ ## END - SHOW HELP ############################################ # Does the backup directory already exist? if [ ! -d $backup_dir ] then mkdir -p $backup_dir fi # Does the input filename exist? if [ ! -e $input_filename ] then echo "ERROR: Input file doesn't exist." exit fi # We are ready to do the actual backup getAbsolutePath $input_filename; #echo $input_path; # Replacing '/' with '--'; this lets the input filename be an absolute path input_path_replacement=$(echo $input_path | sed 's/\//--/g') #echo $input_path_replacement; backup_filename=$backup_dir/$input_path_replacement-$(date +%Y%m%d)-$(date +%H%M%S) #echo $backup_filename; cp $input_path $backup_filename #echo "" # checking the existance of the output file file_created=0 if [ -e $backup_filename ] then file_created=1 echo "Backup file created - $backup_filename" else echo "Backup file NOT created - Some problems occurred." fi if [ "$comment" != "" ] then backup_comment_filename=$backup_dir/$input_path_replacement-$(date +%Y%m%d)-$(date +%H%M%S)-comment touch $backup_comment_filename echo $comment > $backup_comment_filename echo "Backup file comment created - $backup_comment_filename" fi if [ $file_created == 1 ] then # echo "" echo "Rollback:" echo "mv $backup_filename $input_path" echo "" fi