dev/provisioning/modules/puppi/files/scripts/archive.sh
changeset 28 b0b56e0f8c7f
equal deleted inserted replaced
27:a2342f26c9de 28:b0b56e0f8c7f
       
     1 #!/bin/bash
       
     2 # archive.sh - Made for Puppi
       
     3 
       
     4 # Sources common header for Puppi scripts
       
     5 . $(dirname $0)/header || exit 10
       
     6 
       
     7 # Show help
       
     8 showhelp () {
       
     9     echo "This script is used to backup or restore contents to/from puppi archivedir"
       
    10     echo "It has the following options:"
       
    11     echo "-b <backup_source> - Backups the files to be changed in the defined directory"
       
    12     echo "-r <recovery_destination> - Recovers file to the provided destination"
       
    13     echo "-s <copy|move> - Specifies the backup strategy (move or copy files)"
       
    14     echo "-t <tag> - Specifies a tag to be used for the backup"
       
    15     echo "-d <variable> - Specifies the runtime variable that defines the predeploy dir"
       
    16     echo "-c <yes|no> - Specifies if you want compressed (tar.gz) archives. Default: yes"
       
    17     echo "-o 'options' - Specifies the rsync options to use during backup. Use it to specify custom "
       
    18     echo "               exclude patterns of files you don't want to archive, for example"
       
    19     echo "-m <full|diff> - Specifies the backup type: 'full' backups all the files in backup_source,"
       
    20     echo "                 'diff' backups only the files deployed"
       
    21     echo "-n <number> - Number of copies of backups to keep on the filesystem. Default 5"
       
    22     echo 
       
    23     echo "Examples:"
       
    24     echo "archive.sh -b /var/www/html/my_app -t html -c yes"
       
    25 }
       
    26 
       
    27 # Arguments check
       
    28 if [ "$#" = "0" ] ; then
       
    29     showhelp
       
    30     exit
       
    31 fi
       
    32 
       
    33 # Default settings
       
    34 compression=yes
       
    35 backuptag=all
       
    36 strategy=copy
       
    37 backupmethod=full
       
    38 bakret=5
       
    39 
       
    40 while [ $# -gt 0 ]; do
       
    41   case "$1" in
       
    42     -b)
       
    43       backuproot=$2
       
    44 #      [ $deploy_root ] && backuproot=$deploy_root # This is needed to allow override of $deploy_root via puppi cmd. But breaks on puppi::project::maven
       
    45       action=backup
       
    46       shift 2 ;;
       
    47     -r)
       
    48       backuproot=$2
       
    49 #      [ $deploy_root ] && backuproot=$deploy_root # This is needed to allow override of $deploy_root via puppi cmd. But breaks on puppi::project::maven
       
    50       action=recovery
       
    51       shift 2 ;;
       
    52     -t)
       
    53       backuptag=$2
       
    54       shift 2 ;;
       
    55     -s)
       
    56       case "$2" in
       
    57         mv) strategy="move" ;;
       
    58         move) strategy="move" ;;
       
    59         *) strategy="copy" ;;
       
    60       esac
       
    61       shift 2 ;;
       
    62     -m)
       
    63       case "$2" in
       
    64         diff) backupmethod="diff" ;;
       
    65         *) backupmethod="full" ;;
       
    66       esac
       
    67       shift 2 ;;
       
    68     -c)
       
    69       case "$2" in
       
    70         yes) compression="yes" ;;
       
    71         y) compression="yes" ;;
       
    72         *) compression="none" ;;
       
    73       esac
       
    74       shift 2  ;;
       
    75     -d)
       
    76       predeploydir="$(eval "echo \${$(echo $2)}")"
       
    77       shift 2 ;;
       
    78     -o) 
       
    79       rsync_options=$2
       
    80       shift 2 ;;
       
    81     -n)
       
    82       bakret=$2
       
    83       shift 2 ;;
       
    84     *)
       
    85       showhelp
       
    86       exit
       
    87       ;;
       
    88   esac
       
    89 done
       
    90 
       
    91 
       
    92 # Backup and Restore functions
       
    93 backup () {
       
    94     mkdir -p $archivedir/$project/$tag/$backuptag
       
    95     if [ $archivedir/$project/latest ] ; then
       
    96         rm -f $archivedir/$project/latest
       
    97     fi
       
    98     ln -sf $archivedir/$project/$tag $archivedir/$project/latest
       
    99 
       
   100     filelist=$storedir/filelist
       
   101     cd $predeploydir
       
   102     find . | cut -c 3- | grep -v "^$" > $filelist
       
   103 
       
   104     if [ "$strategy" = "move" ] ; then 
       
   105         for file in $(cat $filelist) ; do
       
   106             mv $backuproot/$file $archivedir/$project/$tag/$backuptag/
       
   107         done
       
   108         if [ "$backupmethod" = "full" ] ; then
       
   109             rsync -a $rsync_options $backuproot/ $archivedir/$project/$tag/$backuptag/
       
   110         fi
       
   111     else
       
   112         if [ "$backupmethod" = "full" ] ; then
       
   113             rsync -a $rsync_options $backuproot/ $archivedir/$project/$tag/$backuptag/
       
   114         else
       
   115             rsync -a $rsync_options --files-from=$filelist $backuproot/ $archivedir/$project/$tag/$backuptag/
       
   116         fi
       
   117     fi
       
   118 
       
   119     if [ "$compression" = "yes" ] ; then
       
   120         cd $archivedir/$project/$tag/$backuptag/
       
   121         tar -czf ../$backuptag.tar.gz .
       
   122         cd $archivedir/$project/$tag/
       
   123         rm -rf $archivedir/$project/$tag/$backuptag/
       
   124     fi
       
   125 }
       
   126 
       
   127 recovery () {
       
   128     if [ ! $rollbackversion ] ; then
       
   129         echo "Variable rollbackversion must exist!"
       
   130         exit 2 
       
   131     fi
       
   132 
       
   133     if [ -d $archivedir/$project ] ; then
       
   134         cd $archivedir/$project
       
   135     else 
       
   136         echo "Can't find archivedir for this project"
       
   137         exit 2
       
   138     fi
       
   139 
       
   140     if [ "$compression" = "yes" ] ; then
       
   141         cd $backuproot/
       
   142         tar -xzf $archivedir/$project/$rollbackversion/$backuptag.tar.gz .
       
   143     else 
       
   144         rsync -a $rsync_options $rollbackversion/$backuptag/* $backuproot
       
   145     fi
       
   146 
       
   147 }
       
   148 
       
   149 delete_old () {
       
   150     # We don't count the "latest" symlink
       
   151     bakret=$(expr $bakret + 1 )
       
   152 
       
   153     cd $archivedir/$project
       
   154 
       
   155     ddirs=$(ls -1p 2>/dev/null | wc -l)
       
   156     while [ $ddirs -gt $bakret ]
       
   157     do
       
   158         victim=$(ls -tr 2>/dev/null | head -1)
       
   159         rm -rf $victim && echo "Deleted old $archivedir/$project/$victim"
       
   160         ddirs=$(ls -1p 2>/dev/null | wc -l)
       
   161     done
       
   162 }
       
   163 
       
   164 # Action!
       
   165 case "$action" in
       
   166     backup) backup ; delete_old ;;
       
   167     recovery) recovery ;;
       
   168 esac