Skip to content

Instantly share code, notes, and snippets.

@i-sync
Last active October 8, 2023 06:42
Show Gist options
  • Save i-sync/7b2e3bb59573bb0d77750c4aba22666b to your computer and use it in GitHub Desktop.
Save i-sync/7b2e3bb59573bb0d77750c4aba22666b to your computer and use it in GitHub Desktop.
Mysql auto backup script
#!/bin/bash
# Database credentials
user="root"
password="xxx"
host=""
db_name="database_name"
# Other options
backup_path="/root/backup/mysql"
d=$(date +"%F-%T")
# Set default file permissions
umask 177
# Dump database into SQL file
mysqldump --user=$user --password=$password $db_name > $backup_path/$db_name-$d.sql
# Delete files older than 30 days
find $backup_path/* -mtime +30 -exec rm {} \;
#spc to remote server
sshpass -p "password" scp $backup_path/$db_name-$d.sql [email protected]:/remote/path/
#!/bin/bash
# Database credentials
user="root"
password="xxx"
host=""
#dbs
#dbs=("mysql" "test")
declare -A dict
dict=( ["test"]="test --ignore-table=test.my_cache_table" )
# Other options
backup_path="/root/mysql/backup"
d=$(date +"%F-%T")
# Set default file permissions
umask 177
# Dump database into SQL file
#for i in "${dbs[@]}"
#do
# mysqldump --user=$user --password=$password $i > $backup_path/$i-$d.sql
#done
for key in ${!dict[@]};
do
echo ${key} ${dict[${key}]}
mysqldump --user=$user --password=$password ${dict[${key}]} > $backup_path/${key}-$d.sql
done
# Delete files older than 30 days
find $backup_path/* -mtime +7 -exec rm {} \;
#spc to remote server
#sshpass -p "password" scp $backup_path/$db_name-$d.sql [email protected]:/remote/path/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment