Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # checkt environment en bereid de backup lokaal voor, zodat alles naar de backup server gersynct kan worden
- # flip hess augustus 2012 [email protected]
- # Global variables:
- PATH='/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin'
- SCRIPT_PATH="${0}"
- SQLAUTH="/root/.my.cnf"
- BASEDIR="/data/backup"
- LOG="/var/log/backup.log"
- # files backup aan/uit te zetten
- FILES="yes"
- # sql backup aan/uit te zetten
- SQL="yes"
- ## DEZE 4 REGELS VERWIJDEREN NA EDIT ################################## # 1
- echo "Edit this script and change the BASEDIR variable!" # 2
- exit 1 # 3
- ###################################################################### # 4
- # The Functions:
- # function die
- function die() { echo -e "There was an error in ${SCRIPT_PATH}:\n \"${1}\" Script ended unsuccessfully" ; exit 1; }
- # function fCheck
- function fCheck()
- {
- # First things first, dit script mag alleen als root draaien
- [ "$(whoami)" = root ] || die "User must be root"
- # check for basedir empty or create
- if [ ! -d "${BASEDIR}" ]; then
- echo "Basedir ${BASEDIR} not found! Creating..."
- mkdir -p "${BASEDIR}" || die "Failed to create Basedir ${BASEDIR}"
- else
- find "${BASEDIR}" -type f -delete || die "Failed to empty ${BASEDIR}"
- fi
- return 0
- }
- # functie backup sql
- function fSqlBackup()
- {
- # Sql lokatie
- SQLDIR="${BASEDIR}/sql"
- # checken binaries
- { test -x /usr/bin/mysqldump && test -x /usr/bin/mysql && test -x /bin/gzip ; } || die "Sql-backup depends on mysql-common!"
- # check for basedir or create
- [ -d "${SQLDIR}" ] || { mkdir -p "${SQLDIR}" || die "Failed to create Basedir ${SQLDIR}"; }
- # check op .my.cnf
- [ -f "${SQLAUTH}" ] || die "${SQLAUTH} Not found!"
- # checken op connectiviteit
- { mysql --defaults-file="${SQLAUTH}" -Be 'show databases' |grep -Eq '(information_schema|performance_schema|mysql)'; } || die "Failed to connect to database!"
- # backuppen ( en Exclude van DB's )
- for DB in $( mysql --defaults-file="${SQLAUTH}" -Be "show databases" | sed '1d' |grep -Ev '(information_schema|performance_schema)')
- do
- # backup mysql database:
- mysqldump --defaults-file="${SQLAUTH}" ${DB} | gzip \
- > ${SQLDIR}/${DB}.sql.gz || { echo "${SCRIPT_PATH}: Failed to dump database ${DB} to ${SQLDIR}/${DB}.sql.gz" |tee -a ${LOG} ; continue; }
- done
- return 0
- }
- # function backup files
- function fFilesBackup()
- {
- # lokatie
- FILESDIR="${BASEDIR}/files"
- # check for basedir or create
- [ -d "${FILESDIR}" ] || { mkdir -p "${FILESDIR}" || die "Failed to create Basedir ${FILESDIR}"; }
- # accounts
- cat /etc/passwd > ${FILESDIR}/passwd
- cat /etc/group > ${FILESDIR}/group
- cat /etc/shadow > ${FILESDIR}/shadow
- cat /etc/sudoers > ${FILESDIR}/sudoers
- # packages
- dpkg -l > "${FILESDIR}/pkgs-list"
- # cron
- for CRON in $( find /var/spool/cron/crontabs/ -type f )
- do cat "${CRON}" > "${FILESDIR}/$( basename ${CRON} )-crontab"
- done
- # netstat
- netstat -ntulp | sort > "${FILESDIR}/netstat"
- netstat -ntulp | sed -r 's#^([a-z]{3}[[:blank:]]{2}|Proto)([[:blank:]]+[0-9]+[[:blank:]]+[0-9]+| Recv-Q Send-Q)#\1#;
- s#([[:blank:]]+)[0-9]+/([^/]+)$#\1\2#' | sort > "${FILESDIR}/netstat-clean"
- # check ifconfig
- ifconfig -a > "${FILESDIR}/ifconfig"
- # check route table
- route -n > "${FILESDIR}/route"
- # uname
- uname -a > "${FILESDIR}/uname"
- # firewall rules
- if [ -x /sbin/iptables ]; then
- iptables -L -n > "${FILESDIR}/iptables"
- iptables -t nat -L -n > "${FILESDIR}/iptables-nat"
- fi
- # check interface settings
- if [ -x /sbin/ip ]; then
- ip addr show > "${FILESDIR}/ip"
- fi
- # check interface settings
- if [ -x /sbin/ethtool ]; then
- for INTERFACE in $(ifconfig | awk '/^eth[0-9+] / {print $1}')
- do
- ethtool "${INTERFACE}" >> "${FILESDIR}/ethtool"
- done
- fi
- # list filesystems
- cat /proc/mounts | sort > "${FILESDIR}/mounts"
- # file listing
- find / > "${FILESDIR}/file-listing"
- # permissions
- find "${BASEDIR}" -type f -exec chmod 600 {} \; || die "Failed to set file permissions"
- # klaar
- return 0
- }
- # Start the program:
- if [ "${FILES}" = "yes" ] ; then
- fFilesBackup
- fi
- if [ "${SQL}" = "yes" ]; then
- fSqlBackup
- fi
- # Exit with previous return code:
- exit "${?}"
Advertisement
Add Comment
Please, Sign In to add comment