No announcement yet.

Shell Script To Take Backup of the Database

  • Filter
  • Time
  • Show
Clear All
new posts

  • Shell Script To Take Backup of the Database

    Shell Script To Take Backup of the Database

    First create a script under the home directory of the account lets say


    In this case I am taking the example of domain hosted on the Linux Plesk Server, please change the paths accordingly for cPanel servers

    Then chmod 755 /var/www/vhosts/

    Now configure a cron

    vi /var/spool/cron/username

    Add the following command
    0 22 * * * sh /var/www/vhosts/

    Now add the following script at /var/www/vhosts/

    now=$(date +%d_%m_%Y_%H_%M_%S)
    logfile=$backupfolder/巴ackup_log_$(date +%Y_%m).txt
    echo 杜ysqldump started at $(date +%d-%m-%Y %H:%M:%S) >> $logfile
    mysqldump 勃ser=mysqlusername 朴assword=userpassword 撲pt dbname | gzip > $fullpathbackupfile
    echo 杜ysqldump finished at $(date +%d-%m-%Y %H:%M:%S) >> $logfile
    chown username.psacln $fullpathbackupfile
    chown username.psacln $logfile
    echo 吐ile permission changed >> $logfile
    find $backupfolder -name db_backup_* -mtime +8 -exec rm {} \;
    echo 登ld files deleted >> $logfile
    echo 登peration finished at $(date +%d-%m-%Y %H:%M:%S) >> $logfile
    echo ***************** >> $logfile
    exit 0

    Note:- backupfolder path in in the root of the domain please set it according to the actual root of the domain

    As the script was desined for plesk server I致e set the ownership to username.psacln, please change it to username.username for cPanel server

    In the above command now is reading the current time

    backupfolder is reading the path of the folder where you wish the backup files to get stored

    Define the database username like I did 勃ser=mysqlusername 朴assword=userpassword and name of the database to backed up in this case it is dbname .