Initial commit

This commit is contained in:
Pavel@Vostro5468 2021-11-02 11:05:23 +03:00
parent b190f36eb7
commit 067e2ad3d2
3 changed files with 130 additions and 0 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
compressed
incremental
config

118
backup.sh Executable file
View File

@ -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

9
config.sample Normal file
View File

@ -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';