cron job for backup the database in linux/php

后端 未结 5 1676
独厮守ぢ
独厮守ぢ 2021-02-10 22:02

Am new to linux cron job, i am using mysql DB, my database name finaldb, i want to take this database every one hour,

I have folder called dailbackup, in this i have fol

相关标签:
5条回答
  • 2021-02-10 22:30

    Create somewhere a script to make your rolling backups, like this (untested, but should work):

    #!/bin/bash
    
    BKPDIR=dailbackup  # You must use absolute path here
    DB=finaldb
    USERNAME=myusername
    PASSWORD=mypassword
    
    BKPFILE=${BKPDIR}/`date +%Y-%m-%d`/final_db_`date +%H`.sql
    
    # Create backup
    mysqldump --user=${USERNAME} --password=${PASSWORD} ${DB} | gzip -c > ${BKPFILE}
    
    # Remove older backups (> 7 days),
    # unless you want to run out of drive space
    find ${BKPDIR} -mtime +7 -print0 | xargs -0 rm -rf
    

    Then setup this script to run as an hourly cronjob:

    crontab -e
    
    0 * * * * /absolute-path-to-where-you-saved-the-script
    
    0 讨论(0)
  • 2021-02-10 22:36
    crontab -e
    

    putting this:

    the_date='date +%Y%m%d'
    the_hour='date +%H'
    0 * * * * mysqldump OPTIONS > /dailbackup/`$the_date`/final_db_`$the_hour`.sql
    

    the above cron is allow you to backup database every hour and using the %H as sql file name

    0 讨论(0)
  • 2021-02-10 22:45

    Yes ofcourse, you can do it as long as your mysql server is up and listening :). You will need to make a shell or perl script and use edit the crond using the below command (in Fedora):

    crontab -e
    

    Components of your cron job is ::

    1) Path to your script(executable)

    2) Minutes(00-59)

    3) Hours (00 - 23)

    4) Month (01-12)

    5) Day (01-31)

    6) Day of the week (00 -06 with 00 as Sunday)

    Example :: You wat to run test_pl script every day at 1200

    entry in crontab will be ::

    00 12 * * * /home/jok/test_pl

    0 讨论(0)
  • 2021-02-10 22:49

    Untested one liner:

    mysqldump -u*user* -p*password* -P*dbport* -h localhost finaldb > /.../dailbackup/final_db_$(date +%Y-%m-%d_%H_%M).sql
    

    just add it to your cron job or wrap it in a script and you are done

    0 讨论(0)
  • 2021-02-10 22:54

    Create a PHP Script containing the following:

    $dbFile = 'final_db'.date('H').'.sql.gz';
    $dbHost = 'localhost'; // Database Host
    $dbUser = 'username'; // Database Username
    $dbPass = 'password'; // Database Password
    exec( 'mysqldump --host="'.$dbHost.'" --user="'.$dbUser.'" --password="'.$dbPass.'" --add-drop-table "finaldb" | gzip > "'.$dbFile.'"' );
    
    0 讨论(0)
提交回复
热议问题