cron job for backup the database in linux/php

后端 未结 5 1795
不知归路
不知归路 2021-02-10 21:58

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:42

    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
    

提交回复
热议问题