Skip to content

Commit cd5503c

Browse files
authored
main script
1 parent b86a734 commit cd5503c

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed

backup.sh

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#!/bin/bash
2+
#data
3+
SERVER_HOST="example.com"
4+
SERVER_PORT="22"
5+
USER="doomster"
6+
DB_USER="doomster"
7+
DB_PASSWORD="123456"
8+
REMOTE_FILES_PATH="/var/www/html"
9+
TEMP_FOLDER="~/backups_temp"
10+
LOCAL_BACKUP="/mnt/backups"
11+
12+
if [ $# -eq 0 ]
13+
then
14+
echo "Missing options!"
15+
echo "(run $0 -h for help)"
16+
echo ""
17+
exit 0
18+
fi
19+
20+
ECHO="false"
21+
22+
while getopts "dfch" OPTION; do
23+
case $OPTION in
24+
25+
d)
26+
TYPE="database"
27+
;;
28+
f)
29+
TYPE="files"
30+
;;
31+
c)
32+
TYPE="database"
33+
TYPE2="files"
34+
;;
35+
h)
36+
echo "Usage:"
37+
echo "backup.sh -h "
38+
echo "backup.sh -d "
39+
echo "backup.sh -f "
40+
echo ""
41+
echo " -d to perform database backup"
42+
echo " -f to perform files backup"
43+
echo " -c to perform full backup"
44+
echo " -h help (this output)"
45+
exit 0
46+
;;
47+
48+
esac
49+
done
50+
51+
if [ $TYPE = "database" ]
52+
then
53+
#Databases Backup...
54+
echo "Databases backup ..."
55+
#Create remote database backup configuration file, and check if there is a folder for temporary backups
56+
ssh -p $SERVER_PORT $USER@$SERVER_HOST "echo -e '[mysqldump]\nuser=$DB_USER\npassword=$DB_PASSWORD\n\n[mysql]\nuser=$DB_USER\npassword=$DB_PASSWORD' > .my.cnf | if [[ -d $TEMP_FOLDER ]]; then rm -R $TEMP_FOLDER ;fi | mkdir $TEMP_FOLDER"
57+
#create a mysqldump of every database in the server, put it in the temporary backup folder and gzip it
58+
ssh -p $SERVER_PORT $USER@$SERVER_HOST 'mysql -N -e "show databases" | while read dbname; do mysqldump --complete-insert --routines --triggers --single-transaction "$dbname" > '$TEMP_FOLDER'/$dbname$(date +"%d-%m-%Y").sql && gzip '$TEMP_FOLDER'/$dbname$(date +"%d-%m-%Y").sql; done'
59+
#Get database backup from server to local /backup folder
60+
mkdir -p $LOCAL_BACKUP/$SERVER_HOST/Databases
61+
scp -P $SERVER_PORT $USER@$SERVER_HOST:$TEMP_FOLDER/*.sql.gz $LOCAL_BACKUP/$SERVER_HOST/Databases/.
62+
#clearup server side files
63+
ssh -p $SERVER_PORT $USER@$SERVER_HOST "rm -r $TEMP_FOLDER | rm .my.cnf"
64+
echo "done!";
65+
if [ $TYPE2 = "files" ]
66+
then
67+
$TYPE = $TYPE2;
68+
fi
69+
fi
70+
71+
if [ $TYPE = "files" ]
72+
then
73+
echo "Files backup..."
74+
#create file backup to pull
75+
ssh -p $SERVER_PORT $USER@$SERVER_HOST "mkdir -p $TEMP_FOLDER | tar -zcf $TEMP_FOLDER/directory_backup_$(date +"%d-%m-%Y").tar.gz $REMOTE_FILES_PATH"
76+
#pull the file to local backup folder
77+
mkdir -p $LOCAL_BACKUP/$SERVER_HOST/Files
78+
scp -P $SERVER_PORT $USER@$SERVER_HOST:$TEMP_FOLDER/*.tar.gz $LOCAL_BACKUP/$SERVER_HOST/Files/.
79+
#clear remote server side temp files
80+
ssh -p $SERVER_PORT $USER@$SERVER_HOST "rm -R $TEMP_FOLDER";
81+
fi
82+

0 commit comments

Comments
 (0)