我写了一个bash脚本来备份我的LAMP服务器。老实说,这是我第一次接触bash,因此我想请教您一些改进方面的帮助。
#!/bin/bash
date=`(date +%Y%m%d%H%M)`
declare -A users=(
["user1"]="password1"
["user2"]="password2"
)
for user in ${!users[@]}
do
directory=$user
directories=`find /var/www/$directory -maxdepth 1 -mindepth 1 -type d`
for directory in $directories
do
cd $directory
if [ ! -d backups ]
then
mkdir backups
fi
tar --exclude ./backups -czf backups/$date.tar.gz .
password=${users[$user]}
databases=`mysql -u $user -p$password -Bse "SHOW DATABASES;"`
for database in $databases
do
if [ $database = ${directory##*/} ]
then
mysqldump -u $user -p$password $database | gzip > backups/$date.sql.gz
fi
done
chmod 770 backups -R
chown $user:$user backups -R
done
done
谢谢!