1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
|
#!/bin/bash
# A Simple Shell Script to Backup Red Hat / CentOS / Fedora / Debian / Ubuntu Apache Webserver and SQL Database
# Path to backup directories
DIRS="/home/ubuntu/backup/bin"
# Store todays date
NOW=$(date +"%F")
# Store backup path
BACKUP="/home/ubuntu/backup/var/$NOW"
#BACKUP="/mnt/cald-backup/database/$NOW"
# Backup file name hostname.time.sql.gz
MFILE="$(hostname).$(date +'%T').sql.gz"
# Set MySQL username and password
MYSQLUSER="userdb"
MYSQLPASSWORD="password"
# Remote SSH server cald-backup setup
SSHSERVER="ipserver" # your remote ssh server
SSHUSER="ubuntu" # username
SSHDUMPDIR="/home/ubuntu/backup/var" # remote ssh server directory to store dumps
# Paths for binary files
TAR="/bin/tar"
MYSQLDUMP="/usr/bin/mysqldump"
GZIP="/bin/gzip"
SCP="/usr/bin/scp"
SSH="/usr/bin/ssh"
LOGGER="/usr/bin/logger"
# make sure backup directory exists
[ ! -d $BACKUP ] && mkdir -p ${BACKUP}
# Log backup start time in /var/log/messages
$LOGGER "$0: *** Backup started @ $(date) ***"
# Backup MySQL
$MYSQLDUMP -u ${MYSQLUSER} --single-transaction -h localhost -p${MYSQLPASSWORD} sooner | $GZIP -9 > ${BACKUP}/${MFILE}
# Dump all local files to failsafe remote UNIX ssh server / home server
$SSH ${SSHUSER}@${SSHSERVER} mkdir -p ${SSHDUMPDIR}/${NOW}
$SCP -r ${BACKUP}/* ${SSHUSER}@${SSHSERVER}:${SSHDUMPDIR}/${NOW}
# Log backup end time in /var/log/messages
$LOGGER "$0: *** Backup Ended @ $(date) ***"
# Check for backup file and log messages
if [ -f $BACKUP/$MFILE ]
then
# /usr/bin/mail -s "DB ethan7888 backup completed successfully." useremail
else
#echo toto
/usr/bin/mail -s "DB ethan7888 backup failed." useremail
fi
exit |
Partager