(09-17-2014, 05:19 AM)Me.B Wrote: For me shell is more efficient. No time out risk, less overhead under php.
Hi!
I have modified your script to obtain:
-backup for only one domain
-backup for only one database
-no junk path in compressed files
-.htaccess included in compressed files
I did this because i am hosting more than one domain in my account, but i am working only in one domain.
So i want the backup to be made for that domain only.
It would be nice to have in the Sentora backup module these options...
ALL CREDITS FOR THE SCRIPT GOES TO "Me.B" !!!
Also, THANK YOU SENTORA DEVS for a great hosting software!
Obs:
-the code is now messed up,and some variables may be redundant, but it's working
Setup:
-put the code where you want
-fill with your datas:
fldname="folder_to_be_backed_up"
dbname="database_to_be_backed_up"
clntname="your_hosting_user"
-chmod to script so you can run it:
chmod u+x /path/to/file/script.sh
-remove the break lines if there are any:
sed -i 's/\r//' /path/to/file/script.sh
-run the script
./script.sh
Code:
#!/bin/bash
fldname="folder_to_be_backed_up"
dbname="database_to_be_backed_up"
clntname="your_hosting_user"
panelroot="/var/sentora/hostdata"
date=$(date +"%d-%b-%Y_%H_%M")
clearday=45
backups older than XX days apply to all zip files
echo "Backup starting"
mysqlpassword=$(cat /etc/sentora/panel/cnf/db.php | grep "pass =" | sed -s "s|.*pass \= '\(.*\)';.*|\1|")
logfile="backup_last.log"
oldlogfile="backup_old.log"
errorlogfile="backup_error.log"
touch $errorlogfile
exec > >(tee $logfile)
exec 2>&1
rm -rf old_$logfile
mv $logfile $oldlogfile
echo "Starting backup $(date +"%d-%b-%Y %H:%M")">> $logfile
function_backup_db_execute() {
backupfolder="$5/backups"
mkdir -p $backupfolder
mysqldump --user=$1 --password=$2 --host=$3 $4 > $backupfolder/$4_db_$date.sql
cd $backupfolder
zip -r $backupfolder/$4_db_$date.zip $4_db_$date.sql
rm -f $backupfolder/$4_db_$date.sql
}
function_backup_html() {
cd /root/
echo "html path backup now: $1/public_html/$2"
cd $panelroot/$1/public_html/$2
zip -r $panelroot/$1/backups/$1_$2_files_$3.zip ./
}
function_backup_clear() {
# Delete files older than 45 days
find $1/$2_*.zip -mtime +$clearday -exec rm -f {} \;
echo "Removing old backups in $1 user $2"
}
function_backup_db() {
echo "user ID to backup : $1"
mysql sentora_core -u root -p$mysqlpassword -e "SELECT my_name_vc FROM x_mysql_databases WHERE my_acc_fk='$1' AND my_deleted_ts IS NULL;;"| while read my_name_vc; do
if [ "$my_name_vc" == $dbname ]; then
echo "database to backup $my_name_vc"
function_backup_db_execute root $mysqlpassword localhost $my_name_vc $2
fi
done
}
mysql sentora_core -u root -p$mysqlpassword -e "SELECT ac_id_pk, ac_user_vc, CONCAT('/var/sentora/hostdata/' , ac_user_vc) as ac_user_path, CONCAT( CONCAT('/var/sentora/hostdata/' , ac_user_vc),'/public_html') as ac_user_path_tobackup FROM x_accounts WHERE ac_deleted_ts IS NULL and ac_enabled_in = 1 ;" | while read ac_id_pk ac_user_vc ac_user_path guid ac_user_path_tobackup; do
if [ "$ac_user_vc" == $clntname ]; then
echo "Backup: User: $clntname Folder: $fldname Database: $dbname"
function_backup_html $ac_user_vc $fldname $date >> $logfile
function_backup_db $ac_id_pk $ac_user_path >> $logfile
function_backup_clear "$ac_user_path/backups" $ac_user_vc >> $logfile
fi
done
echo "Finished backup $(date +"%d-%b-%Y %H:%M")">> $logfile
echo "All backup actions logged in $logfile and errors in $errorlogfile"