Script to backup home directory and mysql
Hi,
i search in this forum for a backup for home directory and mysql database.
i wrote this script, if you is correct?
i tried it and it seems to work...
my goal is replicate each home dicretory (exclude tmp folder) and in some user backup each mysql database. the script create an incremental backup in 6 folder. then i want to configure crontab every 2 or 4 hours... thank you in advance
#!/bin/sh
destinazione=user@server.backup.tld
cartella=/home/user/backup
key=/root/.ssh/key
mkdir -p /home/some_user/mysql_backup/
rm -rf /home/some_user/mysql_backup/*
# Add your backup dir location, password, mysql location and mysqldump location
DATE=$(date +"%Y-%m-%d-%H-%M")
BACKUP_DIR="/home/some_user/mysql_backup"
MYSQL_USER="root"
MYSQL_PASSWORD="password"
MYSQL=/usr/bin/mysql
MYSQLDUMP=/usr/bin/mysqldump
# To create a new directory into backup directory location
mkdir -p $BACKUP_DIR/$DATE
# get a list of databases
databases=`$MYSQL -u$MYSQL_USER -p$MYSQL_PASSWORD -e "SHOW DATABASES;" | grep -Ev "(Database|information_schema)"`
# dump each database in separate name
for db in $databases; do
echo $db
$MYSQLDUMP --force --opt --user=$MYSQL_USER -p$MYSQL_PASSWORD --databases $db | gzip > "$BACKUP_DIR/$DATE/$db.sql.gz"
done
ssh -i $key $destinazione rm -rf $cartella/backup.6
ssh -i $key $destinazione mv $cartella/backup.5 $cartella/backup.6
ssh -i $key $destinazione mv $cartella/backup.4 $cartella/backup.5
ssh -i $key $destinazione mv $cartella/backup.3 $cartella/backup.4
ssh -i $key $destinazione mv $cartella/backup.2 $cartella/backup.3
ssh -i $key $destinazione mv $cartella/backup.1 $cartella/backup.2
ssh -i $key $destinazione cp -al $cartella/backup.0 $cartella/backup.1
cd /var/cpanel/users
for i in *; do
ssh -i $key $destinazione mkdir -p $cartella/backup.0/$i/ ;
rsync --delete --recursive "-exclude 'tmp' -avz -e "ssh -i $key" /home/$i/* $destinazione:$cartella/backup.0/$i/ ;
done
exit
my goal is replicate each home dicretory (exclude tmp folder) and in some user backup each mysql database. the script create an incremental backup in 6 folder. then i want to configure crontab every 2 or 4 hours... thank you in advance
-
the script sometimes crashes on mysql dump... but rsync works correctly... thank you 0 -
Hello :) I've moved this thread to our "Workarounds" forum. Note that while we are happy to answer questions about cPanel/WHM development and our API, code review for these types of scripts generally fall outside our scope of support. Thank you. 0
Please sign in to leave a comment.
Comments
2 comments