如何用一个命令备份多个数据库以分离文件

发布于 2024-10-19 16:57:37 字数 256 浏览 2 评论 0原文

大家好,我们有 2 个网络服务器,周五可能会离线。我们在这些服务器上托管了大约 90 个网站,我已经找到了一种将每个网站文件夹备份到其自己的 .tar.gz 文件的方法 - 我现在需要的是找到一种方法将每个数据库从我们的数据库服务器导出为自己的数据库通过一条命令进行 SQL 备份。

目前我知道的唯一方法是使用 PHP myAdmin,但对于 100 多个数据库来说,这有点乏味。

有没有一种简单的方法可以通过 SSH 将每个数据库导出为其自己的 SQL 备份文件?

Hey all, we have 2 web servers which may go offline Friday. we have ~90 websites hosted on these servers and I already have found a way to backup each website folder to its own .tar.gz file - what I need now is to find a way to export each database from our database server as its own SQL backup with one command.

Currently the only way I know how is to use PHP myAdmin, but for 100+ databases that gets a little tedious.

Is there a simple way to export each database as its own SQL backup file over SSH?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

情绪 2024-10-26 16:57:37

您可以使用mysqldump --all-databases,但您只会得到一个大的 SQL 转储。

找到这个 bash 脚本:

for T in `mysql -N -B -e 'show databases' -pYOUR_ROOT_PASSWORD`; 
   do echo $T; 
   mysqldump -pYOUR_ROOT_PASSWORD $T | gzip -c > $T.sql.gz;
done

您只需测试它并根据您的需要调整路径/名称。

You could mysqldump --all-databases but you'll get only one big SQL dump.

Found this bash script:

for T in `mysql -N -B -e 'show databases' -pYOUR_ROOT_PASSWORD`; 
   do echo $T; 
   mysqldump -pYOUR_ROOT_PASSWORD $T | gzip -c > $T.sql.gz;
done

You just have to test it and adapt the path/names to your needs.

过气美图社 2024-10-26 16:57:37
if [ -z "$1" ]
  then
    echo "Provide MySQL root password as argument.";
    exit;
fi
cnt=0
for T in `mysql -N -B -e 'show databases' -u root -p$1`; 
   do
   D=`date "+%Y_%m_%d_%H_%M_%S"`;
   cnt=$((cnt + 1))
   File=$T"_"$D;
   echo "Backing up $T to $File.sql.gz"; 
   mysqldump -u root -p$1 $T | gzip -c > $File.sql.gz;
   echo "Done.";
done

echo "$cnt databases backed up.";
if [ -z "$1" ]
  then
    echo "Provide MySQL root password as argument.";
    exit;
fi
cnt=0
for T in `mysql -N -B -e 'show databases' -u root -p$1`; 
   do
   D=`date "+%Y_%m_%d_%H_%M_%S"`;
   cnt=$((cnt + 1))
   File=$T"_"$D;
   echo "Backing up $T to $File.sql.gz"; 
   mysqldump -u root -p$1 $T | gzip -c > $File.sql.gz;
   echo "Done.";
done

echo "$cnt databases backed up.";
谁把谁当真 2024-10-26 16:57:37

这个适用于共享托管服务器,它将每个备份存储在各自用户的主文件夹中,以便他们可以相应地恢复/使用/删除它们。

backup_folder="automated_backup/";


if [ "$(id -u)" != "0" ]; then
   echo "This script must be run as root" 1>&2
   exit 1
fi

if [ -z "$1" ]
  then
    echo "Provide MySQL root password as argument.";
    exit;
fi
cnt=0
for T in `mysql -N -B -e 'show databases' -u root -p$1`; 
   do
   D=`date "+%Y_%m_%d_%H_%M_%S"`;
   cnt=$((cnt + 1))
   User=`echo $T | cut -d'_' -f 1`
   Folder="/home/$User/";
   Folder2=$Folder$backup_folder;
   if [ -d "$Folder" ]; then
      if [ ! -d "$Folder2" ]; then

        echo "$Folder2 not exists, creating it …";
        mkdir -p $Folder2

      fi
      File=$Folder2$T"_"$D;
      echo "Backing up $T to $File.sql.gz"; 
      mysqldump -u root -p$1 $T | gzip -c > $File.sql.gz;
      chown $User.$User -R $Folder2;
      echo "Done.";
   fi   
done

echo "$cnt databases backed up.";

This one is suitable for shared hosting servers, it stores every backup in respective users home folder, so that they can restore/use/remove them accordingly.

backup_folder="automated_backup/";


if [ "$(id -u)" != "0" ]; then
   echo "This script must be run as root" 1>&2
   exit 1
fi

if [ -z "$1" ]
  then
    echo "Provide MySQL root password as argument.";
    exit;
fi
cnt=0
for T in `mysql -N -B -e 'show databases' -u root -p$1`; 
   do
   D=`date "+%Y_%m_%d_%H_%M_%S"`;
   cnt=$((cnt + 1))
   User=`echo $T | cut -d'_' -f 1`
   Folder="/home/$User/";
   Folder2=$Folder$backup_folder;
   if [ -d "$Folder" ]; then
      if [ ! -d "$Folder2" ]; then

        echo "$Folder2 not exists, creating it …";
        mkdir -p $Folder2

      fi
      File=$Folder2$T"_"$D;
      echo "Backing up $T to $File.sql.gz"; 
      mysqldump -u root -p$1 $T | gzip -c > $File.sql.gz;
      chown $User.$User -R $Folder2;
      echo "Done.";
   fi   
done

echo "$cnt databases backed up.";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文