This is a simple script take to daily backup server files and MySQL databases to FTP server. Make sure you have lftp client installed. Type the following command if you are using Centos/Fedora/RedHat
# yum install lftp
If you are using Ubuntu/Debian
# sudo apt-get install install lftpCreate the folder /backup, enter
# mkdir /backup # chmod 0600 /backupCreate backup.sh file, enter
# vi /backup/backup.shAdd the following content to backup.sh
#!/bin/bash # Daily backup script # Backup Server Files & MySQL # Support backup multiple directories and multiple databases # Copyright (c) 2010-2011 lifeLinux # This script is licensed under GNU GPL version 2.0 or above ### Webserver directory WD="/var/www/domain" ### MySQL Server Login Info ### MUSER="root" MPASS="MYSQL_ROOT_PASSWORD" MHOST="localhost" DBS="DATABASE_NAME_1 DATABASE_NAME_2" MYSQL="$(which mysql)" MYSQLDUMP="$(which mysqldump)" GZIP="$(which gzip)" TAR="$(which tar)" ### Backup directory BAK="/backup" ### FTP SERVER Login info ### FTPU="FTP USER" FTPP="FTP PASSWORD" FTPS="FTP SERVER IP" DAILY=$(date +"%u") [ ! -d $BAK/$DAILY ] && mkdir -p $BAK/$DAILY || /bin/rm -f $BAK/$DAILY/* for db in $DBS do FILE=$BAK/$DAILY/mysql-$db.gz $MYSQLDUMP -u $MUSER -h $MHOST -p$MPASS $db | $GZIP -9 > $FILE done WDS=`dir $WD` for w in $WDS do FILE=$BAK/$DAILY/source-$w.gz $TAR -zcvf $FILE $WD/$w/* done lftp -u $FTPU,$FTPP -e "mkdir $DAILY;cd $DAILY; mput $BAK/$DAILY/*; quit" $FTPSSet cron job
# crontab -eIf you want to auto backup at 3:00AM, add the following line to crontab
00 03 * * * /backup/backup.sh > /dev/null 2>&1
No comments:
Post a Comment