Skip to content

Instantly share code, notes, and snippets.

@tbvinh
Last active December 22, 2022 15:37
Show Gist options
  • Save tbvinh/51bc2b297b1b864546ba42e4b2a92a04 to your computer and use it in GitHub Desktop.
Save tbvinh/51bc2b297b1b864546ba42e4b2a92a04 to your computer and use it in GitHub Desktop.
#!/bin/sh
# wget -O backup-apache-mysql-wwwfolder.sh https://gist.githubusercontent.com/tbvinh/51bc2b297b1b864546ba42e4b2a92a04/raw/backup-apache-mysql-wwwfolder.sh?rnd=`date +%s`
## 0 ======================
echo 1. Backup apache .conf files
echo 2. Backup www/ folder
echo 3. Backup mysql
## 1 ======================
BK_FOLDER="bk_`date +%Y-%m-%d-%m`"
mkdir -p $BK_FOLDER
wget -O $BK_FOLDER/restore-www-apache-domains.sh https://gist.githubusercontent.com/tbvinh/37062f28b60768e413af567b527be9f2/raw/restore-www-apache-domains.sh?rnd=`date +%s`
## 2 ======================
echo Backup apache .conf files
APACHE=$BK_FOLDER/apache
mkdir -p $APACHE
cp -Lr /etc/apache2/sites-available/*.conf $APACHE
LETSENCRYPT=$BK_FOLDER/letsencrypt
mkdir -p $LETSENCRYPT
cp -Lr /etc/letsencrypt/live/* $LETSENCRYPT
## 3 ======================
echo Backup www/ folder
WWW=$BK_FOLDER/www
mkdir -p $WWW
cp -r /var/www/* $WWW
## 4 ======================
#Backup mysql
echo Backup MYSQL
SQL=$BK_FOLDER/mysql
mkdir -p $SQL
echo BACKUP mysql db is at $SQL
mysql -uroot -e "show databases" \
| grep -Ev 'mysql|performance_schema|Database|sys|information_schema' \
| while read dbname; \
do \
echo "Dumping $dbname";\
mysqldump -uroot --routines $dbname > $SQL/$dbname.sql;\
done
echo ==========CREATE USER==========
mysql -uroot -sNe " \
SELECT \
CONCAT( 'CREATE USER \'', User, '\'@\'localhost\' IDENTIFIED BY \'cha@NG1t@#&\'\;' ) AS User \
FROM mysql.user \
WHERE \
User NOT LIKE 'mysql.%' AND CONCAT( User, Host ) <> 'rootlocalhost' AND User <> 'debian-sys-maint' \
" > $SQL/create-mysql-users.sql
echo ==========GRANT USER==========
mysql -uroot -sNe " \
SELECT \
CONCAT( '\'', User, '\'@\'', Host, '\'' ) as User FROM mysql.user \
WHERE \
User NOT LIKE 'mysql.%' \
AND CONCAT( User, Host ) <> 'rootlocalhost' \
AND User <> 'debian-sys-maint' \
" | sort | while read u ;
do echo "-- $u" >> $SQL/create-mysql-users.sql; mysql -uroot -sNe "show grants for $u" | sed 's/$/;/' >> $SQL/create-mysql-users.sql
done
echo ==========DONE==========
echo Compressing...
tar cvfz $BK_FOLDER.tar.gz $BK_FOLDER/
echo DONE, $BK_FOLDER
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment