File tree Expand file tree Collapse file tree 2 files changed +15
-6
lines changed
Expand file tree Collapse file tree 2 files changed +15
-6
lines changed Original file line number Diff line number Diff line change 1+ # DB connection
12db_host = " "
23db_user = " "
34db_pass = " "
5+
6+ # Maxiumum number of parallel jobs
47max_parallel = 10
5- max_backups = 5
8+
9+ # Keeping the last 5 backups
10+ max_backups = 5
11+
12+ # Backup directory
13+ backup_dir = " backup/"
Original file line number Diff line number Diff line change @@ -29,11 +29,11 @@ export $(cat .env | xargs)
2929timestamp=$( date +" %Y%m%d-%H%M%S" )
3030
3131# Make sure backup directory exists
32- mkdir -p " backup /$timestamp "
32+ mkdir -p " $backup_dir /$timestamp "
3333
3434# Function to cleanup old backups
3535cleanup_old_backups () {
36- local backup_dir=" backup/ "
36+ local backup_dir=" $1 "
3737 local backups=($( ls -t " $backup_dir " | grep " ^[0-9]*-[0-9]*$" ) )
3838 local num_backups=${# backups[@]}
3939
@@ -53,14 +53,15 @@ backup_db() {
5353 local db_pass=" $3 "
5454 local db_host=" $4 "
5555 local timestamp=" $5 "
56+ local backup_dir=" $6 "
5657
5758 echo " Backing up $db "
5859 mysqldump \
5960 --user=" $db_user " --password=" $db_pass " \
6061 --host=" $db_host " \
6162 --single-transaction \
6263 --skip-lock-tables \
63- $db | gzip > " backup /$timestamp /$db .sql.gz"
64+ $db | gzip > " $backup_dir /$timestamp /$db .sql.gz"
6465
6566 echo " Backup of $db completed"
6667}
@@ -70,11 +71,11 @@ export -f backup_db
7071databases=$( mysql -h $db_host -u $db_user --password=" $db_pass " -e " SHOW DATABASES;" | grep -Ev " (Database|information_schema|performance_schema|mysql|sys|vapor)" )
7172
7273# Run export job
73- echo " $databases " | parallel -j $max_parallel backup_db {} $db_user $db_pass $db_host $timestamp
74+ echo " $databases " | parallel -j $max_parallel backup_db {} $db_user $db_pass $db_host $timestamp $backup_dir
7475
7576# Clean up old backups
7677echo " Cleaning up old backups"
77- cleanup_old_backups
78+ cleanup_old_backups $backup_dir
7879
7980# Done
8081echo -e " \n\nBackup completed\n"
You can’t perform that action at this time.
0 commit comments