Want more timely Centmin Mod News Updates?
Become a Member

Sysadmin Simple Backup Script

Discussion in 'System Administration' started by Jimmy, May 10, 2017.

  1. Jimmy

    Jimmy Premium Member Premium Member

    1,114
    247
    63
    Oct 24, 2015
    East Coast USA
    Ratings:
    +596
    Local Time:
    4:56 AM
    1.13.x
    MariaDB 10.1.x
    I created a simple backup script. With this script you can download the daily files from the sites and databases folder. If you want to run the cron multiple times per day for hourly backups you can do that as the TIME also is by the hour: TIME=`date +%F-%H`

    1. Create a folder in root and two sub-folders databases and sites
    2. Create a .sh file: <domain>_backup.sh and put in /root/tools/ folder.
    3. Populate the file with the backup script:
    Code:
    #!/bin/bash
    #SUPER SIMPLE BACKUP SCRIPT
    
    # Time format - Year/Month/Day/Hour(UDT)
    TIME=`date +%F-%H`
    
    ### MYSQL BACKUP
    
    # MYSQL User (root)
    MYSQLUSER=root
    MYSQLPASS='<mysql_root_password>'
    
    # MYSQL Database Name
    MYSQLDBASE=<database_name>
    
    # MYSQL Database Filenames
    FILENAME=<domain>_DBBackup-$TIME-backup.sql
    
    # Directories
    DBDESDIR=/root/backups/databases
    
    # Database Save Directory Name
    DBASESAVEDIR=DBBackup-$TIME
    
    # Make Save Directory
    mkdir $DBDESDIR/$DBASESAVEDIR
    DBBKUPFOLDER=$DBDESDIR/$DBASESAVEDIR
    
    # Dump Databases to /root/backups/databases/databasesBackup-$TIME folder
    mysqldump --opt --single-transaction -u $MYSQLUSER -p$MYSQLPASS $MYSQLDBASE > $DBBKUPFOLDER/$FILENAME
    sleep 5
    
    # Tar directory with database backups
    tar -cazf $DBDESDIR/$DBASESAVEDIR.tar.gz $DBBKUPFOLDER
    
    # Remove Folder with uncompressed database
    rm -rf $DBBKUPFOLDER
    sleep 5
    
    ### FILES / DIRECTORIES BACKUP
    
    # File Name
    FILESSAVENAME=<domain>_Backup-$TIME.tar.gz
    
    # Source Directory
    # This is going to backup the entire directory.  Modify for just public if you desire.
    SRCDIR=/home/nginx/domains/<domain>/
    
    # Save Directory
    DIRDESDIR=/root/backups/sites
    
    # Backup Directory (archive w/ permissions)
    tar -czpf $DIRDESDIR/$FILESSAVENAME -C $SRCDIR .
    sleep 5
    
    # Send Email Out
    mail -s "<domain> Backup $TIME" email@domain.com <<< "<domain> backup has been completed."
    4. Modify the Permissions on the File
    Code:
    chmod +x /root/tools/<domain>_backup.sh
    5. Access the Cron Interface
    Code:
    crontab -e
    6. Cron Job (will run everyday at 3AM) Crontab Generator - Generate crontab syntax
    Code:
    01 3 * * * /root/tools/<sitename>_backup.sh 2>/dev/null
    7. You can also manually run the script
    Code:
    # cd /root/tools/
    # ./<sitename>_backup.sh
    Have fun!
     
    • Like Like x 5
  2. eva2000

    eva2000 Administrator Staff Member

    30,160
    6,784
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +10,135
    Local Time:
    6:56 PM
    Nginx 1.13.x
    MariaDB 5.5
    nice, be careful using --single-transaction in mysqldump as it only applies to innodb tables and not myisam so if you have myisam tables, you can end up with not in sync /consistent data in myisam tables. Hint, for my dbbackup.sh script i only use --single-transaction in mysqldump if I detect 100% innodb table based mysql databases ;)
     
    • Informative Informative x 3
    • Useful Useful x 1
  3. Jimmy

    Jimmy Premium Member Premium Member

    1,114
    247
    63
    Oct 24, 2015
    East Coast USA
    Ratings:
    +596
    Local Time:
    4:56 AM
    1.13.x
    MariaDB 10.1.x
    All my tables are innodb, but glad you made that note for people who have myisam tables. :)
     
    • Friendly Friendly x 1
  4. ljseals

    ljseals Member

    88
    22
    8
    Dec 20, 2016
    Ratings:
    +43
    Local Time:
    3:56 AM
    Thanks for the script @Jimmy God bless you!
     
    • Like Like x 1
  5. Jimmy

    Jimmy Premium Member Premium Member

    1,114
    247
    63
    Oct 24, 2015
    East Coast USA
    Ratings:
    +596
    Local Time:
    4:56 AM
    1.13.x
    MariaDB 10.1.x
    @eva2000 do you have any idea why the script would send out 37 emails every minute for 37 minutes when it does a backup via the cron job? I don't get XX number of email when I run the script manually. Right now I have it setup to run every 6 hours.

    Code:
    * */6 * * * /root/tools/cct_backup.sh 2>/dev/null
    
    Looking at the emails I received, the time stamp on the last backup matches the most recent backup email I got (out of 37). Is the script running some kind of loop? Running the script manually it runs once and sends out an email.

    Do you have any idea why this might be happening?
     
  6. eva2000

    eva2000 Administrator Staff Member

    30,160
    6,784
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +10,135
    Local Time:
    6:56 PM
    Nginx 1.13.x
    MariaDB 5.5
  7. Jimmy

    Jimmy Premium Member Premium Member

    1,114
    247
    63
    Oct 24, 2015
    East Coast USA
    Ratings:
    +596
    Local Time:
    4:56 AM
    1.13.x
    MariaDB 10.1.x
    Opps looks like I needed a # in front of that and not a *.

    Code:
    0 */6 * * * /root/tools/<sitename>_backup.sh >/dev/null 2>&1