Backup websites and MySQL database on a linux webserver
One of the servers my company has, runs CentOS with plesk 8 on it. Unfortunately there isn’t any easy way to backup all the sites and databases on the server, so I created a nice simple script to do just that.
#!/bin/sh
activeFolder='/var/www/vhosts/'
backupFolder='/path/to/backup/'
dbUser="root"
dbPassword="yourpassword"
currentDate=`date '+%Y%m%d'`
mkdir $backupFolder$currentDate
cd $backupFolder$currentDate
for i in `ls "$activeFolder"`; do
if [ -d $activeFolder$i ]
then
echo "zipping: $i"
tar czf $i.tar.gz $activeFolder$i
fi
done
# Create mysql backup folder
mkdir MySQL
# Go to backup folder
cd MySQL
# get a list of all databases
databases=`mysql --user=$dbUser --password=$dbPassword -e "SHOW DATABASES;" | tr -d "| " | grep -v Database`
# dump each database in turn
for db in $databases; do
echo "Dumping database: $db"
mysqldump --force --opt --user=$dbUser --password=$dbPassword --database $db > "$db.sql"
done
# Go to backup folder
cd $backupFolder$currentDate
# zip sql files
tar czf MySQL.tar.gz MySQL
# remove mysql backup folder
rm MySQL -rf
# Send e-mail notification
mail -s "Backup Is ready $currentDate" your@email.com < /dev/null
If you use this script, I suggest you run it once a week in off peak time (4-5am), and keep backups of your MySQL database daily.



