【问题标题】:Backup MySQLdatabase to remote server备份 MySQL 数据库到远程服务器
【发布时间】:2014-01-01 08:45:10
【问题描述】:

我需要使用 Cron 自动将我的 MySQL 数据库备份到我的网站根目录下的文件夹中。

我搜索了一下,终于找到了下面的cron作业。

0 1 * * * /usr/bin/mysqldump --opt --all-databases -u USERNAME -pPASSWORD | gzip > /backup-folder/db_bckp`date +\%Y-\%m-\%d`.sql.gz

但是,什么都没有发生。

可能是什么原因?

【问题讨论】:

    标签: mysql database cron backup


    【解决方案1】:

    我会让 cron 执行这个 .sh 文件。确保在外部服务器上验证您的公共 ssh 密钥...

    #!/bin/bash
    ## List of databases to backup
     declare -a arr=(database1 database2)
     ##use something like this to get full list of databases to loop through
     #databases=`mysql -B -r -u ${user} --skip-column-names -p${pass} --execute='show databases'`
    ## mysql user should have SELECT and LOCK privileges on tables to backup
     user='you'
     pass='pass'
    
    ## scp remote variables
     url_ext='external url' 
     folder_ext='/'
     use_ext='external user'
     pass_ext='external password'
    
    ## base dir where backups are saved
     shdir='/home/to/folder/'
    
    ## date and hostname
     day=$(date | awk '{ print $2"-"$3"-"$6}')
     hostname=$(hostname)  
    
    ## now loop through the above array
     for i in ${arr[@]}
     do
     echo $i # current database
     mysqldump -u $user --password=$pass $i > "$i.sql" #dump db to file
     wait
    
    #if you want you can zip the files
     zip backup_db_$i_$day.zip "$i.sql" 
    #or tar them
    #tar -zcvf backup_db_$i_$day.tar.gz /route/to/file
     wait
    
     ##2 using scp you can send the backups to external server
     ##read about private and public ssh keys for automation without password
     scp -v backup_db_$i_$day.zip  user@ip:/home/to/backup/folder/
    
     wait
     done
    
     #Errase old backups
     rm -rf $shdir*.sql 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-07
      • 1970-01-01
      • 2018-08-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-17
      • 2012-01-16
      相关资源
      最近更新 更多