Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@kosztik
Created August 3, 2019 06:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kosztik/282757d4210d71b8f192c3f269e256df to your computer and use it in GitHub Desktop.
Save kosztik/282757d4210d71b8f192c3f269e256df to your computer and use it in GitHub Desktop.
#!/bin/bash
# Copyright (c) 2016, Florian Schaal, info@schaal-24.de
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification,
# are permitted
DOW1=`date +%j`
# no of backups
DOW=`expr $DOW1 % 3`
# Path
BPATH="/var/backup" # like /var/backups no slash at the end!
WWW="/var/www"
MAIL="/var/vmail"
# Databases
DBISPconfig="dbispconfig"
# Binaries
SQLDUMP="/usr/bin/mysqldump"
SQLBIN="/usr/bin/mysql"
GZIP="/bin/gzip"
LEVEL=`date +%u` # day of week (1..7); 1 is Monday
(( LEVEL-- )) # Mon full-Backup, Sun level 6
BACKUP_NO=$LEVEL
# tar-Options
BACKUP_TAR_OPT_NOZIP="-ch"
# sonstige
SYSLOG_TAG=simple-backup.sh
TMP_LOG=/tmp/backuplog.tmp
# Funktionen ------------------------------------------------------------------------------------------------
calculate_time () {
runtime=`expr $2 - $1`
HOUR=`date -d @${runtime} "+%H"`
HOUR=`expr $HOUR - 1`
MINUTES=`date -d @${runtime} "+%M"`
SECOND=`date -d @${runtime} "+%S"`
RUNTIME="0"$HOUR":"$MINUTES":"$SECOND
SIZE=`ls -lh --si $BPATH/$4|cut -d" " -f5`
if ([ $4 != NIL ] && [ $SIZE > 0 ])
then logger -d -t $SYSLOG_TAG $3"-Backup: "$RUNTIME "(size: "$SIZE")"
else logger -d -t $SYSLOG_TAG $3"-Backup: "$RUNTIME
fi
}
# -----------------------------------------------------------------------------------------------------------
logger -d -t $SYSLOG_TAG "Backup day $DOW1 - start"
STARTTIME=`date +%s`
# save old UMASK
UMASK=`umask`
umask 0077
# /usr-Backup -----------------------------------------------------------------------------------------------
START=`date +%s`
logger -d -t $SYSLOG_TAG "/usr backup"
tar cfz - /usr --exclude=src --exclude=share --exclude=X11R6 --exclude=lost+found --exclude=src --exclude=tmp --exclude=x86_64-suse-linux > $BPATH/$DOW-usr.tar.gz
STOP=`date +%s`
calculate_time $START $STOP usr-backup $DOW-usr.tar.gz
# /var-Backup -----------------------------------------------------------------------------------------------
START=`date +%s`
logger -d -t $SYSLOG_TAG "/var backup"
tar cfz - /var --exclude=adm/autoinstall/cache --exclude=adm/backup --exclude=adm/mount --exclude=adm/YaST/InstSrcManager --exclude=cache --exclude=games --exclude=X11R6 --exclude=lost+found --exclude=lib/clamav --exclude=lib/named/proc --exclude=lib/ntp/drift --exclude=lib/ntp/proc --exclude=lib/ntp/var --exclude=lib/zypp --exclude=lock --exclude=log --exclude=backup --exclude=log --exclude=run --exclude=spool/amavis/tmp --exclude=tmp > $BPATH/$DOW-var.tar.gz
STOP=`date +%s`
calculate_time $START $STOP var-backup $DOW-var.tar.gz
# Complete MySQL-DB-Dump ------------------------------------------------------------------------------------
START=`date +%s`
`$SQLDUMP --all-databases --add-drop-table | $GZIP > $BPATH/$DOW-complete_MySQL-DB.sql.gz`
STOP=`date +%s`
calculate_time $START $STOP MySQL-DB-Dump $DOW-complete_MySQL-DB.sql.gz
# ISPConfig-DB ----------------------------------------------------------------------------------------------
START=`date +%s`
`$SQLDUMP $DBISPconfig | $GZIP > $BPATH/$DOW-$DBISPconfig.sql.gz`
STOP=`date +%s`
calculate_time $START $STOP ISPconfig-DB $DOW-$DBISPconfig.sql.gz
# mysql-DB --------------------------------------------------------------------------------------------------
START=`date +%s`
`$SQLDUMP mysql | $GZIP > $BPATH/$DOW-mysql.sql.gz`
STOP=`date +%s`
calculate_time $START $STOP MySQL-DB $DOW-mysql.sql.gz
# System Backup ---------------------------------------------------------------------------------------------
START=`date +%s`
logger -d -t $SYSLOG_TAG "System backup"
tar cfz - / --exclude=/vz --exclude=/backups --exclude=/dev --exclude=/media --exclude=/data --exclude=/mnt --exclude=/proc --exclude=/sys --exclude=/srv --exclude=/tmp --exclude=/usr --exclude=/var --exclude=/root/.cpan --exclude=/root/backup --exclude=/home > $BPATH/$DOW-system.tar.gz
STOP=`date +%s`
calculate_time $START $STOP System $DOW-system.tar.gz
logger -d -t $SYSLOG_TAG "Backup day $DOW1 - stop"
exit 0
: '
SAVE AND EXIT THIS FILE (by pushing control plus X, then it asks if you want to save, click yes) you need root access to save this file.
next type in ssh:
chmod 700 backup.sh
chown root.root backup.sh
Now we need to edit the my.cnf file, type in ssh:
nano /root/.my.cnf
paste the below (CHANGE THE PASSWORD TO BE THE REAL ROOT MYSQL PASSWORD FOR YOUR SERVER)
[mysql]
user=root
password=pass
[mysqladmin]
user = root
password = pass
[mysqldump]
user=root
password=pass
SAVE AND EXIT THIS FILE (by pushing control plus X, then it asks if you want to save, click yes) you need root access to save this file.
now type in ssh:
chown root.root /root/.my.cnf
add to crontab to automatically run this backup script daily at 4 am (or change the 4 below to any hour you want).
in ssh type:
crontab -e
type i to insert, then paste
00 4 * * * /root/backup.sh 2>&1 > /dev/null
Hit esc button to stop insert, then type :x to close and exit
All the backup files will go into /var/backup folder.
'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment