Last active
November 4, 2023 08:47
-
-
Save mdutt247/314bacf3e4bfca9ee9917bf3bc505287 to your computer and use it in GitHub Desktop.
Shell script to backup all specified directories using cron job
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# Author: M. Dutt ([email protected]) | |
# Date: 28/10/2023 | |
# Purpose: Backup all specified direcories. | |
# License: This shell script is released under GPL v2. | |
# | |
######################## | |
# REQUIREMENTS | |
######################## | |
# | |
# * Required commands: | |
# + du | |
# + tar | |
# | |
######################## | |
# USAGE | |
######################## | |
# | |
# * It stores all backup copies in directory '/var/backup' by default, | |
# You can change it in variable $BACKUP_DIR below. | |
# | |
# * Add crontab job for root user: | |
# | |
# # crontab -e -u root | |
# 10 0 * * * /bin/bash /var/daily_backup.sh | |
# | |
KEEP_DAYS='7' | |
export BACKUP_DIR="/var/backup" | |
export DIRECTORIES='directory1 directory2 directory3 directory4' | |
export CMD_DATE='/bin/date' | |
export CMD_DU='du -sh' | |
export YEAR="$(${CMD_DATE} +%Y)" | |
export MONTH="$(${CMD_DATE} +%m)" | |
export DAY="$(${CMD_DATE} +%d)" | |
export TIME="$(${CMD_DATE} +%H-%M-%S)" | |
export TIMESTAMP="${YEAR}-${MONTH}-${DAY}-${TIME}" | |
export BACKUP_SUCCESS='YES' | |
export PATH='/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/sbin' | |
export LOGFILE="${BACKUP_DIR}/${TIMESTAMP}.log" | |
[ ! -d ${BACKUP_DIR} ] && mkdir -p ${BACKUP_DIR} 2>/dev/null | |
chown root ${BACKUP_DIR} | |
chmod 0400 ${BACKUP_DIR} | |
echo "* Starting backup: ${TIMESTAMP}." >${LOGFILE} | |
echo "* Backup directory: ${BACKUP_DIR}." >>${LOGFILE} | |
chmod 0400 ${LOGFILE} | |
echo "* Backing up directories: ${DIRECTORIES}." >> ${LOGFILE} | |
for d in ${DIRECTORIES}; do | |
tar --exclude='vendor' --exclude='.git' --exclude='node_modules' --exclude='wp-admin' --exclude='wp-includes' -cjf ${BACKUP_DIR}/${d}-${TIMESTAMP}.tar.bz2 -C /var/www ${d} >>${LOGFILE} | |
compressed_size="$(${CMD_DU} ${BACKUP_DIR}/${d}-${TIMESTAMP}.tar.bz2 | awk '{print $1}')" | |
if [ X"$?" == X'0' ]; then | |
sleep 10 | |
else | |
export BACKUP_SUCCESS='NO' | |
fi | |
done | |
echo -e "* File size:\n----">>${LOGFILE} | |
cd ${BACKUP_DIR} && \ | |
${CMD_DU} *${TIMESTAMP}* >>${LOGFILE} | |
echo "----" >>${LOGFILE} | |
echo "* Backup completed (Success? ${BACKUP_SUCCESS})." >>${LOGFILE} | |
if [ X"${BACKUP_SUCCESS}" == X'YES' ]; then | |
echo "==> Backup completed successfully." | |
else | |
echo -e "==> Backup completed with !!!ERRORS!!!.\n" 1>&2 | |
fi | |
find ${BACKUP_DIR}/* -type f -mtime +${KEEP_DAYS} -print | while read -r file; do | |
echo "* Old backup found. Deleting: ${file}." >>${LOGFILE} | |
rm "$file" | |
done | |
echo "==> Detailed log (${LOGFILE}):" | |
echo "==============================" | |
cat ${LOGFILE} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment