更改 aws 中的路径

更改 aws 中的路径

我有将 mysql 数据库备份到 aws 的脚本。我需要在脚本中添加代码,在备份成功到 AWS 后删除数据库有人能指导我代码吗?这是脚本

#!/bin/bash

# Basic variables
mysqlpass="mysql"
bucket="s3://akshaysolutionbackup/orbit/"

# Timestamp (sortable AND readable)
stamp=`date +"%d-%m-%Y"`

# List all the databases
databases=`mysql -u root -pmysql -e "SHOW DATABASES;" | tr -d "| " | grep -v "\(Database\|information_schema\|performance_schema\)"`

# Feedback
echo -e "Dumping to \e[1;32m$bucket/$stamp/\e[00m"

# Loop the databases
for db in $databases; do

# Define our filenames
filename="$stamp - $db.sql.gz"
tmpfile="/tmp/$filename"
object="$bucket/$stamp/$filename"

# Feedback
echo -e "\e[1;34m$db\e[00m"

# Dump and zip
echo -e "  creating \e[0;35m$tmpfile\e[00m"
mysqldump -u root -pmysql --force --opt --databases "$db" | gzip -c > $tmpfile"

# Upload
echo -e "  uploading..."
s3cmd put "$tmpfile" "$object"

# Delete
rm -f "$tmpfile"
done;

# Jobs a goodun
echo -e "\e[1;32mJobs a goodun\e[00m"

答案1

从脚本中删除数据库的最简单方法是:

echo "drop database dbname" | mysql -u username -ppassword

这将启动mysql命令行客户端并执行命令传入的 SQL 语句echo。在脚本中,您可以在备份数据库后立即删除它:

echo "drop database $db" | mysql -u username -ppassword

(在do/done循环内),或者等到脚本结束,当整个备份完成时,再对数据库进行一次循环并删除所有数据库:

for db in databases; do
  echo "drop database $db" | mysql -u username -ppassword
done;

相关内容