Backups Files¶
#!/usr/bin/env bash
# gunzip -c file.sql.gzip > dump.sql
# gpg --decrypt-files /tmp/file.gpg
dateTime=$(date +%Y-%m-%d-%H-%M-%S)
backupDir="/backups/files"
ownerUser="root"
ownerGroup="wheel"
oldDays=30
if [ -z $(getent group | cut -d ":" -f 1 | grep wheel) ]; then
ownerGroup="root"
fi
dirs=(
/etc
/usr/local/etc
)
if [ ! -d "${backupDir}" ]; then
mkdir "${backupDir}"
chown "${ownerUser}:${ownerGroup}" "${backupDir}"
chmod ug+rw "${backupDir}"
chmod o-rwx "${backupDir}"
fi
cd "${backupDir}"
for dir in "${dirs[@]}"
do
name=$(echo "${dir}" | sed 's#^/##g' | sed 's#/#_#g' | sed 's# #_#g')
cd "${backupDir}"
if [ ! -d "${name}" ]; then
mkdir "${name}"
chown "${ownerUser}:${ownerGroup}" "${name}"
chmod ug+rw "${name}"
chmod o-rwx "${name}"
fi
cd "${name}"
echo "Backup ${dir} to ${currentBackupDir}"
tar -cjf "${dateTime}_$(hostname)_${name}.tgz" "${dir}"
countFiles=$(ls "./" | wc -l)
# если количество файлов больше или равно 30
if [ ${countFiles} -ge ${oldDays} ]; then
echo "Delete old backups: count (${countFiles})"
find ./ -name "*.tgz" -mdateTime +${oldDays} -type f -delete > /dev/null 2>&1
else
echo "No old dumps files"
fi
done