From 067e2ad3d29a29ad6904faae0e498a408d6a2085 Mon Sep 17 00:00:00 2001 From: "Pavel@Vostro5468" Date: Tue, 2 Nov 2021 11:05:23 +0300 Subject: [PATCH] Initial commit --- .gitignore | 3 ++ backup.sh | 118 ++++++++++++++++++++++++++++++++++++++++++++++++++ config.sample | 9 ++++ 3 files changed, 130 insertions(+) create mode 100644 .gitignore create mode 100755 backup.sh create mode 100644 config.sample diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5134b62 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +compressed +incremental +config diff --git a/backup.sh b/backup.sh new file mode 100755 index 0000000..94b64df --- /dev/null +++ b/backup.sh @@ -0,0 +1,118 @@ +#!/bin/bash + +ABSOLUTE_FILENAME=`readlink -e "$0"` +DIRECTORY=`dirname "$ABSOLUTE_FILENAME"` + +source ${DIRECTORY}/config + +uuid=`uuid` +stamp=`date +"%Y%m%d-%H%M%S-I"` +do_full="" +do_quet=0 + +while getopts "fq" opt; do + case $opt in + f) do_full=1;; + q) do_quet=1;; +# -f -full +# -q -quet (no verbose output) + esac +done + + +if [ -n "${do_full}" ] +then + if [ "${do_quet}" == 0 ] + then + echo "Full backup started" + fi + + stamp=`date +"%Y%m%d-%H%M%S-F"` + rm -f ${prefix}/incremental/* +fi + +if [ ! -d "${prefix}/incremental" ]; then + # Создать папку, только если ее не было + mkdir ${prefix}/incremental +fi + +if [ ! -d "${prefix}/compressed" ]; then + # Создать папку, только если ее не было + mkdir ${prefix}/compressed +fi + +mkdir ${prefix}/${stamp} + +# SQL DB Backup +for sql_item in ${sql_list} +do + if [ "${do_quet}" == 0 ] + then + echo -n "Collecting database ${sql_item//:/ }...." + fi + + #${sqldump_prefix} ${sql_item//:/ } | gzip > ${prefix}/${stamp}/${sql_item//:/.}.sql.gz + + if [[ -f "${prefix}/incremental/${sql_item//:/.}.sql" ]] + then + xsuffix=".diff" + ${sqldump_prefix} ${sql_item//:/ } | diff ${prefix}/incremental/${sql_item//:/.}.sql - > ${prefix}/${stamp}/${sql_item//:/.}.sql.diff + else + ${sqldump_prefix} ${sql_item//:/ } | cat > ${prefix}/${stamp}/${sql_item//:/.}.sql + cp ${prefix}/${stamp}/${sql_item//:/.}.sql ${prefix}/incremental/ + fi + if [ "${do_quet}" == 0 ] + then + echo "OK" + fi +done + +#Archiving SQL for DIFF + +#Files Full Backup +for file_item in ${files_list} +do + xfolder=`echo ${file_item} | awk -F : '{ print $1 }'` + if [[ -n "`echo ${file_item} | awk -F : '{ print $2 }'`" ]] + then + xsubfolders=`echo ${file_item} | sed -e 's/^[^:]*://'` + else + xsubfolders="*" + fi + + if [ "${do_quet}" == 0 ] + then + echo -n "Collecting folder ${xfolder}...." + fi + if [[ -f "${prefix}/incremental/${xfolder////_}.snar" ]] + then + cp ${prefix}/incremental/${xfolder////_}.snar ${prefix}/incremental/${xfolder////_}.snar.diff + xsuffix=".diff" + else + xsuffix="" + fi + + cd ${xfolder} && tar -c --listed-incremental=${prefix}/incremental/${xfolder////_}.snar${xsuffix} -f ${prefix}/${stamp}/${xfolder////_}${xsuffix}.tar ${xsubfolders//:/ } + rm -f ${prefix}/incremental/${xfolder////_}.snar.diff + if [ "${do_quet}" == 0 ] + then + echo "OK" + fi +done + +# Compressing +cd ${prefix}/${stamp} && tar -czf ${prefix}/compressed/${stamp}.tgz * +rm -f $prefix/$stamp/* +rmdir $prefix/$stamp + +# Sending data + +for xfile_item in `ls ${prefix}/compressed` +do + scp -B -i ${sshkey} ${prefix}/compressed/${xfile_item} ${sshuser}@${sshhost}:${sshpath} > /dev/null + if [ "$?" == 0 ] + then + rm -f ${prefix}/compressed/${xfile_item} + fi +done + diff --git a/config.sample b/config.sample new file mode 100644 index 0000000..76918f2 --- /dev/null +++ b/config.sample @@ -0,0 +1,9 @@ +sqldump_prefix="docker exec -t db mysqldump" +sql_list="dbname dbname2 dbname3:table:table" +files_list="/path/to/folder1 /path/to/folder/2" +prefix='/path_to_temp_folder'; +sshhost='host'; +sshport=22; +sshuser='login'; +sshkey='/path_to_ssh_private_key'; +sshpath='/media/hdd2/backup';