Skip to content

Instantly share code, notes, and snippets.

@Glennmen
Created August 27, 2018 12:00
Show Gist options
  • Save Glennmen/02fb9c5a84c94af1d6c736d97c215cfc to your computer and use it in GitHub Desktop.
Save Glennmen/02fb9c5a84c94af1d6c736d97c215cfc to your computer and use it in GitHub Desktop.
System + MySQL backup script
#!/bin/sh
# System + MySQL backup script
# Full backup day - Sun (rest of the day do incremental backup)
# Copyright (c) 2005-2006 nixCraft <http://www.cyberciti.biz/fb/>
# This script is licensed under GNU GPL version 2.0 or above
# Automatically generated by http://bash.cyberciti.biz/backup/wizard-ftp-script.php
# ---------------------------------------------------------------------
### System Setup ###
DIRS="/home /etc /var/www"
BACKUP=/tmp/backup.$$
NOW=$(date +"%d-%m-%Y")
REMOVAL_DATE=$(date -d "2 week ago" +"%d-%m-%Y")
INCFILE="/root/tar-inc-backup.dat"
DAY=$(date +"%a")
FULLBACKUP="Sun"
### MySQL Setup ###
MUSER="xxxxx" # Replace with MySQL username
MPASS="xxxxx" # Replace with MySQL user password
MHOST="localhost"
MYSQL="$(which mysql)"
MYSQLDUMP="$(which mysqldump)"
GZIP="$(which gzip)"
### FTP server Setup ###
FTPD="/backup/incremental"
FTPU="xxxxx" # Replace with FTP backup server username
FTPP="xxxxx" # Replace with FTP backup server user password
FTPS="xxxxx" # Replace with FTP back up server IP
NCFTP="$(which ncftpput)"
### Start Backup for file system ###
[ ! -d $BACKUP ] && mkdir -p $BACKUP || :
### See if we want to make a full backup ###
if [ "$DAY" = "$FULLBACKUP" ]; then
FTPD="/backup/full"
FILE="fs-full-$NOW.tar.gz"
tar -zcf $BACKUP/$FILE $DIRS
else
i=$(date +"%Hh%Mm%Ss")
FILE="fs-i-$NOW-$i.tar.gz"
tar -g $INCFILE -zcf $BACKUP/$FILE $DIRS
fi
### Start MySQL Backup ###
# Get all databases name
DBS="$($MYSQL -u $MUSER -h $MHOST -p$MPASS -Bse 'show databases')"
for db in $DBS
do
FILE=$BACKUP/mysql-$db.$NOW-$(date +"%T").gz
$MYSQLDUMP -u $MUSER -h $MHOST -p$MPASS --skip-lock-tables $db | $GZIP -9 > $FILE
done
### Dump backup using FTP ###
#Start FTP backup using ncftp
ncftp -u"$FTPU" -p"$FTPP" $FTPS<<EOF
rmdir -r $FTPD/$REMOVAL_DATE
mkdir $FTPD
mkdir $FTPD/$NOW
cd $FTPD/$NOW
lcd $BACKUP
mput *
quit
EOF
### Remove local backup files
rm -r /tmp/*
@Glennmen
Copy link
Author

Glennmen commented Aug 27, 2018

Used nixCradft auto script generator as a base, customised it with my own preferences.

I run this script with a cron job as root.

Does a backup of /home, /etc and /var/www and a MYSQLDUMP of all available MySQL databases.
A full backup every sunday and an incremental backup all the other days.
Most the of the things are easily changeable by the setup params at the top of the file.

Of course I am open for any improvements or suggestions.

@verus001
Copy link

Dag Glenn,
Ik krijg een fout namelijk:
ERROR 1045 (28000): Access denied for user 'backupuser'@'localhost' (using password: YES)

Niettemin kan ik met die gebruiker op localhost wel een backup nemen als ik manueel de opdracht geef.
Ligt dit aan het script?
Alvast bedankt, Philippe.

@Glennmen
Copy link
Author

Glennmen commented May 8, 2019

@verus001 Sorry voor de late reactie, ik krijg jammer genoeg geen meldingen van Gist updates (vanaf vandaag heeft Github dit pas toegevoegd).

Dus dat is MySQL dat die error geeft geloof ik. Je kan dus met die exact credentials inloggen in je database? Op dezelfde server als waar dit script draait uiteraard. mysql -u backupuser -h localhost -p

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment