Skip to content

Instantly share code, notes, and snippets.

@samirreza
Created September 2, 2018 05:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samirreza/aebfc194836e4edbadad7fb66acb1bfb to your computer and use it in GitHub Desktop.
Save samirreza/aebfc194836e4edbadad7fb66acb1bfb to your computer and use it in GitHub Desktop.
shell script for backup mysql database
backup_parent_dir="/Applications/MAMP/htdocs/test"
mysql_user="root"
mysql_password="root"
db_name="cms"
db_port="8889"
days=3
excluded_tables=(
log
)
mysql_executable_folder="/Applications/MAMP/Library/bin"
echo exit | ${mysql_executable_folder}/mysql -P $db_port --user=$mysql_user --password=$mysql_password -B 2>/dev/null
if [ "$?" -gt 0 ]; then
exit 1
fi
ignored_tables_as_string=''
for table in "${excluded_tables[@]}"
do :
ignored_tables_as_string+=" --ignore-table=${db_name}.${table}"
done
backup_date=`date +%Y_%m_%d_%H_%M`
backup_dir="${backup_parent_dir}/${backup_date}"
mkdir -p "${backup_dir}"
chmod 700 "${backup_dir}"
${mysql_executable_folder}/mysqldump -P $db_port --user=$mysql_user --password=$mysql_password ${ignored_tables_as_string} $db_name | gzip -9 > "${backup_dir}/$db_name.sql.gz"
chmod 600 "${backup_dir}/$db_name.sql.gz"
find $backup_parent_dir/* -type d -mtime +$days -exec rm -rf {} \;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment