Backup and deploy with shell script

Asked

Viewed 72 times

2

I have a productive environment for which I upload html, css etc. However, every time I upload a file I must first make a backup of existing occurrences.

I’m trying to build a script that does the following:

  • Read the directory of upload and search in production if that file exists, if yes make a backup
  • Copy the files to production and generate a list (txt) of the copied files
  • If backup is required, I need the script to remove everything that was copied (to remove new elements if they exist) and copy back the backup files

However, I’m pretty lost since shell script is not my strong suit. Would anyone have any idea how to produce this script?

I currently have this code snippet:

#!/bin/bash

if [ ! $1 ]
then
        echo "Informe o nome do diretorio de upload"
        exit 0
fi

DirUpload=/home/upload/$1
DirProducao=/home/producao


cd $DirUpload
find . | sed 's/\.\///g' > upload.txt

for i in `cat $DirUpload/upload.txt`
do
        if [ -d $DirProducao/$i ]
        then
                echo "Diretorio: ${i}"
        elif [ -f $DirProducao/$i ]
        then
                echo "arquivo: ${i}"
        else
                echo "nao encontrado ${DirProducao}/${i}"
        fi
done

For now I am using the above script to generate a txt with the list to be loaded, based on it I am checking what is currently in production to be able to copy to the backup

  • The upload directory is already production ?

  • upload is the directory that contains the files to be loaded for production.

  • The first part I did quietly, maybe I finish the rest later.

1 answer

2

This script will help you, you can edit it at will:

#!/bin/bash

# Pasta Backup
backup="/home/arcadian/workspace/works/backup"
#Pasta Prod
prod="/home/arcadian/workspace/works/prod"
#Pasta Upload
upload="/home/arcadian/scripts/shell"
#Arquivo txt
txt="/home/arcadian/copiados.txt"

main(){
# Funções, basta comentar pra usar a que deseja.    
deploy
rollback
}

deploy(){

if [ -e $txt ];then
    rm -f $txt;
fi

    for i in $(ls $upload); do
    for a in $(ls $prod); do
        if [[ $a == $i ]];then
            cd $prod
            cp $a $backup
            ls $a >> ~/copiados.txt
            cp -rf $upload/$i $prod
        fi  
    done
done
}

rollback(){
for i in $(cat $txt); do
    rm -rf $prod/$i
    cp -rf $backup/$i $prod
done
}

main

Browser other questions tagged

You are not signed in. Login or sign up in order to post.