Skip to content

Instantly share code, notes, and snippets.

@teawithfruit
Last active November 16, 2015 12:36
Show Gist options
  • Save teawithfruit/2d6e266500697fb498da to your computer and use it in GitHub Desktop.
Save teawithfruit/2d6e266500697fb498da to your computer and use it in GitHub Desktop.
Create a backup from your MySQL database and a given directory.
#!/bin/bash
### This directory will be zipped and pushed to the ftp server.
LOCAL_BACKUP="/srv/www"
### MySQL credentials
MYSQL_USER="root"
MYSQL_PASS="password"
### FTP credentials
FTP_DIR="/backup"
FTP_USER="user"
FTP_PASS="password"
FTP_SERVER="sub.domain.de"
### Temporary local work directory
TEMP_DIR="/srv/backup"
############################################################
NOW=$(date +"%d-%m-%Y")
mkdir $TEMP_DIR
mkdir $TEMP_DIR/$NOW/
mysqldump -u $MYSQL_USER -p$MYSQL_PASS --all-databases > $TEMP_DIR/$NOW/db_$NOW-$(date +"%T").sql
zip -r $TEMP_DIR/$NOW/www_$NOW-$(date +"%T").zip $LOCAL_BACKUP
ncftp -u"$FTP_USER" -p"$FTP_PASS" $FTP_SERVER<<EOF
mkdir $FTP_DIR
mkdir $FTP_DIR/$NOW
cd $FTP_DIR/$NOW
lcd $TEMP_DIR/$NOW
mput *
quit
EOF
rm -r $TEMP_DIR/$NOW
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment