October 12

extremely stupid backup script (files+dbs)

#!/bin/bash

if [[ "$1" != "db_backup" && "$1" != "files_backup" && "$1" != "all" ]]; then
echo -e "Please run the script with following keys:\ndb_backup -> to do backup of all DBs\nfiles_backup -> to do backup of all users without DBs\nall -> to do users and their DBs backups at once"
exit 1
fi
datetime=$(date +%d-%m-%Y_%H-%M-%S)
db_backup_function(){
mkdir -pv /backup/dbs/"$datetime"
for i in `mysql -e "show databases;"|grep -wv "Database\|performance_schema\|information_schema\|sys\|mysql"`;do
mysqldump "$i" >> /backup/dbs/"$datetime"/"$i".sql
if [[ $? -ne 0 ]];then
echo "$datetime DB $i FAILED" >> /backup/failed_DBs.log
fi

zip --quiet /backup/dbs/"$datetime"/"$i".sql.zip /backup/dbs/"$datetime"/"$i".sql
if [[ $? -eq 0 ]];then
rm -f /backup/dbs/"$datetime"/"$i".sql
fi
done
}

files_backup_function(){
mkdir -pv /backup/files/"$datetime"
cd /home/
for i in `ls -1 /home/`;do
zip -r --quiet --symlinks /backup/files/"$datetime"/"$i".zip "$i"
if [[ $? -ne 0 ]];then
echo "$datetime files for $i account FAILED" >> /backup/failed_users.log
fi
done
}

if [[ "$1" = "db_backup" ]];then
db_backup_function
fi

if [[ "$1" = "files_backup" ]];then
files_backup_function
fi

if [[ "$1" = "all" ]];then
db_backup_function
files_backup_function
fi


Copyright 2021. All rights reserved.

Posted 12 October 2025 by admin in category "simple memo

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.