Skip to content

Instantly share code, notes, and snippets.

@mdutt247
Last active November 4, 2023 08:47
Show Gist options
  • Select an option

  • Save mdutt247/314bacf3e4bfca9ee9917bf3bc505287 to your computer and use it in GitHub Desktop.

Select an option

Save mdutt247/314bacf3e4bfca9ee9917bf3bc505287 to your computer and use it in GitHub Desktop.

Revisions

  1. mdutt247 revised this gist Nov 4, 2023. 1 changed file with 5 additions and 5 deletions.
    10 changes: 5 additions & 5 deletions daily_backup.sh
    Original file line number Diff line number Diff line change
    @@ -55,11 +55,11 @@ 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
    if [ X"$?" == X'0' ]; then
    sleep 10
    else
    export BACKUP_SUCCESS='NO'
    fi
    done

    echo -e "* File size:\n----">>${LOGFILE}
  2. mdutt247 revised this gist Nov 4, 2023. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion daily_backup.sh
    Original file line number Diff line number Diff line change
    @@ -53,7 +53,7 @@ chmod 0400 ${LOGFILE}

    echo "* Backing up directories: ${DIRECTORIES}." >> ${LOGFILE}
    for d in ${DIRECTORIES}; do
    tar -cjf ${BACKUP_DIR}/${d}-${TIMESTAMP}.tar.bz2 -C /var/www ${d} >>${LOGFILE}
    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
  3. mdutt247 revised this gist Oct 29, 2023. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions daily_backup.sh
    Original file line number Diff line number Diff line change
    @@ -29,6 +29,7 @@

    KEEP_DAYS='7'
    export BACKUP_DIR="/var/backup"
    export DIRECTORIES='directory1 directory2 directory3 directory4'

    export CMD_DATE='/bin/date'
    export CMD_DU='du -sh'
    @@ -42,8 +43,6 @@ export PATH='/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/sbin'

    export LOGFILE="${BACKUP_DIR}/${TIMESTAMP}.log"

    export DIRECTORIES='directory1 directory2 directory3 directory4'

    [ ! -d ${BACKUP_DIR} ] && mkdir -p ${BACKUP_DIR} 2>/dev/null
    chown root ${BACKUP_DIR}
    chmod 0400 ${BACKUP_DIR}
    @@ -52,6 +51,7 @@ 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 -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}')"
  4. mdutt247 revised this gist Oct 29, 2023. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion daily_backup.sh
    Original file line number Diff line number Diff line change
    @@ -76,7 +76,7 @@ else
    fi

    find ${BACKUP_DIR}/* -type f -mtime +${KEEP_DAYS} -print | while read -r file; do
    echo "* Old backup found. Deleting: " ${file} >>${LOGFILE}.
    echo "* Old backup found. Deleting: ${file}." >>${LOGFILE}
    rm "$file"
    done

  5. mdutt247 renamed this gist Oct 28, 2023. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  6. mdutt247 renamed this gist Oct 28, 2023. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  7. mdutt247 created this gist Oct 28, 2023.
    85 changes: 85 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,85 @@
    #!/usr/bin/env bash
    # Author: M. Dutt (hello@mditech.net)
    # 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 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"

    export DIRECTORIES='directory1 directory2 directory3 directory4'

    [ ! -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}

    for d in ${DIRECTORIES}; do
    tar -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}