Linux shell script for database backup

后端 未结 10 1720
忘了有多久
忘了有多久 2020-12-02 04:59

I tried many scripts for database backup but I couldn\'t make it. I want to backup my database every hour.
I added files to \"/etc/cron.hourly/\" folder, changed its chm

10条回答
  •  醉梦人生
    2020-12-02 05:33

    I got the same issue. But I manage to write a script. Hope this would help.

    #!/bin/bash
    # Database credentials
    user="username"
    password="password"
    host="localhost"
    db_name="dbname"
    # Other options
    backup_path="/DB/DB_Backup"
    date=$(date +"%d-%b-%Y")
    # Set default file permissions
    umask 177
    # Dump database into SQL file
    mysqldump --user=$user --password=$password --host=$host $db_name >$backup_path/$db_name-$date.sql
    
    # Delete files older than 30 days
    find $backup_path/* -mtime +30 -exec rm {} \;
    
    
    #DB backup log
    echo -e "$(date +'%d-%b-%y  %r '):ALERT:Database has been Backuped"    >>/var/log/DB_Backup.log
    

提交回复
热议问题