mysql – 如何创建capistrano任务以下载最新的database_backup.tgz并在本地导入?

我正在尝试使用类似于heroku db的Capistrano任务:如果您熟悉它,请执行pull功能.

我有一台远程服务器.在该服务器上,我在/ path / db_backups /文件夹中有一堆备份.在该文件夹中,每天都有数据库的备份.

我想做的就是

>在客户端计算机上下载最新备份.
>解开它.
>将其导入本地mysql数据库.

有人知道处理这个问题的好方法吗?有没有我不知道的宝石?你有一个方便的脚本吗?

解决方法:

以下脚本应该实现:

# Find out which file to copy and save its name in a local text file:
# ssh allows you to specify a command that should be executed on the remote
# machine instead of opening a terminal session on it. I use this to get
# a sorted (ls -t sorts by modification date) list of all backups. I then
# truncate this list to one entry using head -1 and save the file name in a
# local file (filename.txt).
# (12.34.56.78 is a placeholder for the ip/hostname of your server)
ssh 12.34.56.78 ls -t /path/to/backups/ | head -1 > filename.txt

# Copy the backup specified in filename.txt to the tmp dir on your local machine.
scp 12.34.56.78:/path/to/backups/`cat filename.txt` /tmp/db_backup.sql.tar

# Untar the backup archive.
cd /tmp && tar -xf db_backup.sql.tar

# Import into database of choice.
mysql -u your_username -p database_to_import_to < /tmp/db_backup.sql

(这假设您在UNIX系统上并安装了scp和tar …)

上一篇:如何处理要在cron作业中处理的新文件


下一篇:3306端口没有收听?无法远程连接MySQL