Skip to content

Commit 8ac1ec9

Browse files
committed
new version
1 parent 6f3a42d commit 8ac1ec9

File tree

8 files changed

+259
-724
lines changed

8 files changed

+259
-724
lines changed

README.md

Lines changed: 52 additions & 141 deletions
Original file line numberDiff line numberDiff line change
@@ -1,141 +1,52 @@
1-
### Bash Backup Rotation Script
2-
3-
### Description
4-
This script allows you to create a backup rotation for your files and mysql database. The backup target can be local or remote (FTP).
5-
6-
The script has 3 backup types
7-
8-
- Daily backup
9-
- Weekly backup (default : perform every sixth day of the week)
10-
- Monthly backup (default : perform every first day of the month)
11-
12-
This script allows you to customize where to backup each `backup type`. For example you can choose to do daily and weekly backup on local machine and monthly backup on a FTP server.
13-
14-
Each type of backup will have a backup retention(copy). The default retention for every type of backup can be configured.
15-
16-
The backup retention default day is :
17-
18-
* Daily backup : 5 Day ( 5 daily backups )
19-
* Weekly backup : 14 Day ( ~2 weekly backups )
20-
* Monthly backup : 30 Day ( ~1 Monthly backups )
21-
22-
Please consider your storage size when chosing retention settings.
23-
24-
### Table of Contents
25-
1. [Usage Instruction](#usage_instruction)
26-
2. [Backup Type Configuration](#backup_type_configuration)
27-
3. [FTP Backup Configuration](#ftp_backup_configuration)
28-
4. [Contribute and Contact](#contribute_and_contact)
29-
30-
<div id='usage_instruction'>
31-
32-
## 1. Usage Instruction
33-
34-
1. Download the `backup_rotation.sh` script into your NIX server.
35-
36-
wget https://raw.githubusercontent.com/todiadiyatmo/bash-backup-rotation-script/master/backup_rotation.sh
37-
38-
2. Open the file using your favourite editor (ex :`nano` or `vim`)
39-
40-
nano backup_rotation.sh
41-
42-
or
43-
44-
vim backup_rotation.sh
45-
46-
3. Edit the configuration file. MYSQL configuration, for minimal `BACKUP_DIR`, `TARGET_DIR` must be filled.
47-
48-
# The directory to be backup , DO NOT END THE DIRECTORY WITH BACKSLASH !
49-
TARGET_DIR=/target_directory
50-
51-
# The backup directory , DO NOT END THE DIRECTORY WITH BACKSLASH !
52-
SOURCE_DIR=/your_backup_direcory
53-
54-
# Admin email
55-
56-
57-
# Number of day the daily backup keep
58-
RETENTION_DAY=5
59-
60-
# Number of day the weekly backup keep
61-
RETENTION_WEEK=14
62-
63-
# Number of day the monthly backup keep
64-
RETENTION_MONTH=30
65-
66-
#Monthly date backup option (day of month)
67-
MONTHLY_BACKUP_DATE=1
68-
69-
#Weekly day to backup option (day of week - 1 is monday )
70-
WEEKLY_BACKUP_DAY=6
71-
72-
4. Make the script executable
73-
74-
chmod +x backup_rotation.sh
75-
76-
5. Test the script (sudo may be required)
77-
78-
bash backup_rotation.sh
79-
80-
6. If the script runs correctly the file will be available in `TARGET_DIR`. Try to extract the file and check the contents
81-
82-
tar -xJfv [filename].tar.xz
83-
84-
7. To make the backup run daily, put it in a crontab
85-
86-
crontab -e
87-
88-
#put this line in crontab
89-
#This will run it 10 minutes after midnight.
90-
10 0 * * * [path to your backup_rotation.sh]
91-
92-
8. If crontab is not available, consult your distributions packages. Ubuntu Server includes it by default.
93-
94-
<div id='backup_type_configuration'>
95-
96-
## 2. Backup Type Configuration
97-
98-
To customize the `backup type` you must alter the `LOCAL_BACKUP_OPTION` and `FTP_BACKUP_OPTION` in the configuration script.
99-
100-
The configuration value of `LOCAL_BACKUP_OPTION` and `FTP_BACKUP_OPTION` can be view on the table below
101-
102-
![Backup type configuration](http://todiadiyatmo.com/wp-content/uploads/2014/11/Selection_105.png)
103-
104-
For example, if you want to do a daily backup on local and weekly on FTP you must fill both options like this:
105-
106-
LOCAL_BACKUP_OPTION=1
107-
108-
FTP_BACKUP_OPTION=2
109-
110-
<div id='ftp_backup_configuration'>
111-
112-
## 3. FTP Backup Configuration
113-
114-
1. Make sure that you fill the `FTP_BACKUP_OPTION` with a correct value.
115-
2. Fill out FTP configuration in the script.
116-
117-
# -----------------
118-
# FTP Configuration
119-
# Enter all data within the ' ' single quote !
120-
# -----------------
121-
122-
#This is the FTP servers host or IP address.
123-
FTP_HOST='FTP HOST'
124-
125-
#FTP PORT
126-
FTP_PORT=21
127-
128-
#This is the FTP user that has access to the server.
129-
FTP_USER='FTP USER'
130-
131-
#This is the password for the FTP user.
132-
FTP_PASSWORD='FTP_PASSWORD'
133-
134-
#The backup directory on remote machine, DO NOT END THE DIRECTORY WITH BACKSLASH !
135-
FTP_TARGET_DIR='/remote_path'
136-
137-
<div id='contribute_and_contact'>
138-
139-
## 4. Contribute and Contact
140-
141-
Everybody is welcome to fork and contribute. If you need to pull something just post it on the message board.
1+
# Bash Backup Rotation Script
2+
3+
Simple script which can be easily modified if needed for backup rotation. This script use ssh and rsync for syncing
4+
This script is completely rewriten , old script can be found here : https://github.com/todiadiyatmo/bash-backup-rotation-script/tree/legacy-1.0.0 .
5+
6+
## Feature
7+
8+
- Daily, Weekly, Monthly backup script with number of retention (backup to keep) option
9+
- backup to local only, local + remote and remote only mode
10+
- 4 different backup script :
11+
- file backup with zip compression script
12+
- rsync script
13+
- mysql script
14+
- mysql with extrabackup script
15+
- Secure backup with SSH connection
16+
17+
18+
## Todo
19+
20+
This release is still missing this feature from the old relesae
21+
22+
- [ ] email notification
23+
24+
# Usage
25+
26+
## MySQL / MySQL Extrabackup / Zip File Backup
27+
28+
- Copy script to desired location
29+
- Edit the parameter of the script, configure the `BACKUP_RETENTION_` to set the rotation / number of backup needed
30+
31+
```
32+
### User Pass Mysql ###
33+
USER=backup
34+
PASS=backup
35+
DBNAME=project_sql
36+
BACKUP_DIR="/root/backup"
37+
DST_HOST="user@host"
38+
REMOTE_DST_DIR="/root/backup"
39+
BACKUP_DAILY=true # kalau false ga dibackup
40+
BACKUP_WEEKLY=true # kalau false ga dibackup
41+
BACKUP_MONTHLY=true # kalau false ga dibackup
42+
BACKUP_RETENTION_DAILY=3
43+
BACKUP_RETENTION_WEEKLY=3
44+
BACKUP_RETENTION_MONTHLY=3
45+
```
46+
- test the script to make sure everything correct , ex : `mysql-backup-script.sh`
47+
- put script on cron to make sure it is running everyday at your desired time : `00 03 * * * backup.sh`
48+
- check your backup result
49+
- profit :)
50+
51+
## Pull request and issue
52+
feel free to open pull request and submit bug ticket

backup-extrabackup.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
bac

backup-file-zip.sh

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#!/bin/bash
2+
3+
## Note
4+
# Remote delete use SSH Command
5+
6+
## Configuration
7+
SRC_CODE="/var/www/html"
8+
BACKUP_DIR="/home/user/backup"
9+
PROJECT_NAME="project"
10+
DST_HOST="user@host"
11+
REMOTE_DST_DIR="/root/backup"
12+
BACKUP_DAILY=true # if set to false backup will not work
13+
BACKUP_WEEKLY=true # if set to false backup will not work
14+
BACKUP_MONTHLY=true # if set to false backup will not work
15+
BACKUP_RETENTION_DAILY=3
16+
BACKUP_RETENTION_WEEKLY=3
17+
BACKUP_RETENTION_MONTHLY=3
18+
BACKUP_MODE='local-only' ## Available option ( 'local-only' | 'remote-only' | 'local-remote' )
19+
20+
MONTH=`date +%d`
21+
DAYWEEK=`date +%u`
22+
23+
if [[ ( $MONTH -eq 1 ) && ( $BACKUP_MONTHLY == true ) ]];
24+
then
25+
FN='monthly'
26+
elif [[ ( $DAYWEEK -eq 7 ) && ( $BACKUP_WEEKLY == true ) ]];
27+
then
28+
FN='weekly'
29+
elif [[ ( $DAYWEEK -lt 7 ) && ( $BACKUP_DAILY == true ) ]];
30+
then
31+
FN='daily'
32+
fi
33+
34+
DATE=$FN-`date +"%Y%m%d"`
35+
36+
function local_remote
37+
{
38+
zip -r $BACKUP_DIR/$PROJECT_NAME-htdocs-$DATE.zip $SRC_CODE -x *wp-content/uploads*
39+
cd $BACKUP_DIR/
40+
ls -t | grep $PROJECT_NAME | grep htdocs | grep daily | sed -e 1,"$BACKUP_RETENTION_DAILY"d | xargs -d '\n' rm -R > /dev/null 2>&1
41+
ls -t | grep $PROJECT_NAME | grep htdocs | grep weekly | sed -e 1,"$BACKUP_RETENTION_WEEKLY"d | xargs -d '\n' rm -R > /dev/null 2>&1
42+
ls -t | grep $PROJECT_NAME | grep htdocs | grep monthly | sed -e 1,"$BACKUP_RETENTION_MONTHLY"d | xargs -d '\n' rm -R > /dev/null 2>&1
43+
rsync -avh --delete $BACKUP_DIR/ $DST_HOST:$REMOTE_DST_DIR
44+
}
45+
46+
function local_only
47+
{
48+
zip -r $BACKUP_DIR/$PROJECT_NAME-htdocs-$DATE.zip $SRC_CODE -x *wp-content/uploads*
49+
cd $BACKUP_DIR/
50+
ls -t | grep $PROJECT_NAME | grep htdocs | grep daily | sed -e 1,"$BACKUP_RETENTION_DAILY"d | xargs -d '\n' rm -R > /dev/null 2>&1
51+
ls -t | grep $PROJECT_NAME | grep htdocs | grep weekly | sed -e 1,"$BACKUP_RETENTION_WEEKLY"d | xargs -d '\n' rm -R > /dev/null 2>&1
52+
ls -t | grep $PROJECT_NAME | grep htdocs | grep monthly | sed -e 1,"$BACKUP_RETENTION_MONTHLY"d | xargs -d '\n' rm -R > /dev/null 2>&1
53+
}
54+
55+
function remote_only
56+
{
57+
zip -r $BACKUP_DIR/$PROJECT_NAME-htdocs-$DATE.zip $SRC_CODE -x *wp-content/uploads*
58+
rsync -avh --remove-source-files $BACKUP_DIR/ $DST_HOST:$REMOTE_DST_DIR
59+
ssh -t -t $DST_HOST "cd $REMOTE_DST_DIR ; ls -t | grep $PROJECT_NAME | grep htdocs | grep daily | sed -e 1,"$BACKUP_RETENTION_DAILY"d | xargs -d '\n' rm -R > /dev/null 2>&1"
60+
ssh -t -t $DST_HOST "cd $REMOTE_DST_DIR ; ls -t | grep $PROJECT_NAME | grep htdocs | grep weekly | sed -e 1,"$BACKUP_RETENTION_WEEKLY"d | xargs -d '\n' rm -R > /dev/null 2>&1"
61+
ssh -t -t $DST_HOST "cd $REMOTE_DST_DIR ; ls -t | grep $PROJECT_NAME | grep htdocs | grep monthly | sed -e 1,"$BACKUP_RETENTION_MONTHLY"d | xargs -d '\n' rm -R > /dev/null 2>&1"
62+
}
63+
64+
if [ $BACKUP_MODE == local-remote ]; then
65+
if [[ ( $BACKUP_DAILY == true ) && ( ! -z "$BACKUP_RETENTION_DAILY" ) && ( $BACKUP_RETENTION_DAILY -ne 0 ) && ( $FN == daily ) ]]; then
66+
local_remote
67+
fi
68+
if [[ ( $BACKUP_WEEKLY == true ) && ( ! -z "$BACKUP_RETENTION_WEEKLY" ) && ( $BACKUP_RETENTION_WEEKLY -ne 0 ) && ( $FN == weekly ) ]]; then
69+
local_remote
70+
fi
71+
if [[ ( $BACKUP_MONTHLY == true ) && ( ! -z "$BACKUP_RETENTION_MONTHLY" ) && ( $BACKUP_RETENTION_MONTHLY -ne 0 ) && ( $FN == monthly ) ]]; then
72+
local_remote
73+
fi
74+
elif [ $BACKUP_MODE == local-only ]; then
75+
if [[ ( $BACKUP_DAILY == true ) && ( ! -z "$BACKUP_RETENTION_DAILY" ) && ( $BACKUP_RETENTION_DAILY -ne 0 ) && ( $FN == daily ) ]]; then
76+
local_only
77+
fi
78+
if [[ ( $BACKUP_WEEKLY == true ) && ( ! -z "$BACKUP_RETENTION_WEEKLY" ) && ( $BACKUP_RETENTION_WEEKLY -ne 0 ) && ( $FN == weekly ) ]]; then
79+
local_only
80+
fi
81+
if [[ ( $BACKUP_MONTHLY == true ) && ( ! -z "$BACKUP_RETENTION_MONTHLY" ) && ( $BACKUP_RETENTION_MONTHLY -ne 0 ) && ( $FN == monthly ) ]]; then
82+
local_only
83+
fi
84+
elif [ $BACKUP_MODE == remote-only ]; then
85+
if [[ ( $BACKUP_DAILY == true ) && ( ! -z "$BACKUP_RETENTION_DAILY" ) && ( $BACKUP_RETENTION_DAILY -ne 0 ) && ( $FN == daily ) ]]; then
86+
remote_only
87+
fi
88+
if [[ ( $BACKUP_WEEKLY == true ) && ( ! -z "$BACKUP_RETENTION_WEEKLY" ) && ( $BACKUP_RETENTION_WEEKLY -ne 0 ) && ( $FN == weekly ) ]]; then
89+
remote_only
90+
fi
91+
if [[ ( $BACKUP_MONTHLY == true ) && ( ! -z "$BACKUP_RETENTION_MONTHLY" ) && ( $BACKUP_RETENTION_MONTHLY -ne 0 ) && ( $FN == monthly ) ]]; then
92+
remote_only
93+
fi
94+
fi

backup-mysql.sh

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#!/bin/bash
2+
3+
### User Pass Mysql ###
4+
USER=backup
5+
PASS=backup
6+
DBNAME=project_sql
7+
BACKUP_DIR="/root/backup"
8+
DST_HOST="user@host"
9+
REMOTE_DST_DIR="/root/backup"
10+
BACKUP_DAILY=true # if set to false backup will not work
11+
BACKUP_WEEKLY=true # if set to false backup will not work
12+
BACKUP_MONTHLY=true # if set to false backup will not work
13+
BACKUP_RETENTION_DAILY=3
14+
BACKUP_RETENTION_WEEKLY=3
15+
BACKUP_RETENTION_MONTHLY=3
16+
BACKUP_MODE='local-only' ## Available option ( 'local-only' | 'remote-only' | 'local-remote' )
17+
18+
###Test daily weekly or monthly###
19+
MONTH=`date +%d`
20+
DAYWEEK=`date +%u`
21+
22+
if [[ ( $MONTH -eq 1 ) && ( $BACKUP_MONTHLY == true ) ]];
23+
then
24+
FN='monthly'
25+
elif [[ ( $DAYWEEK -eq 7 ) && ( $BACKUP_WEEKLY == true ) ]];
26+
then
27+
FN='weekly'
28+
elif [[ ( $DAYWEEK -lt 7 ) && ( $BACKUP_DAILY == true ) ]];
29+
then
30+
FN='daily'
31+
fi
32+
33+
34+
DATE=$FN-`date +"%Y%m%d"`
35+
36+
function local_remote
37+
{
38+
mysqldump -u$USER -p$PASS $DBNAME | gzip > $BACKUP_DIR/$DBNAME-mysql-$DATE.sql.gz
39+
cd $BACKUP_DIR/
40+
ls -t | grep $DBNAME | grep mysql | grep daily | sed -e 1,"$BACKUP_RETENTION_DAILY"d | xargs -d '\n' rm -R > /dev/null 2>&1
41+
ls -t | grep $DBNAME | grep mysql | grep weekly | sed -e 1,"$BACKUP_RETENTION_WEEKLY"d | xargs -d '\n' rm -R > /dev/null 2>&1
42+
ls -t | grep $DBNAME | grep mysql | grep monthly | sed -e 1,"$BACKUP_RETENTION_MONTHLY"d | xargs -d '\n' rm -R > /dev/null 2>&1
43+
rsync -avh --delete $BACKUP_DIR/ $DST_HOST:$REMOTE_DST_DIR
44+
}
45+
46+
function local_only
47+
{
48+
mysqldump -u$USER -p$PASS $DBNAME | gzip > $BACKUP_DIR/$DBNAME-sql-$DATE.sql.gz
49+
cd $BACKUP_DIR/
50+
ls -t | grep $DBNAME | grep mysql | grep daily | sed -e 1,"$BACKUP_RETENTION_DAILY"d | xargs -d '\n' rm -R > /dev/null 2>&1
51+
ls -t | grep $DBNAME | grep mysql | grep weekly | sed -e 1,"$BACKUP_RETENTION_WEEKLY"d | xargs -d '\n' rm -R > /dev/null 2>&1
52+
ls -t | grep $DBNAME | grep mysql | grep monthly | sed -e 1,"$BACKUP_RETENTION_MONTHLY"d | xargs -d '\n' rm -R > /dev/null 2>&1
53+
}
54+
55+
function remote_only
56+
{
57+
mysqldump -u$USER -p$PASS $DBNAME | gzip > $BACKUP_DIR/$DBNAME-sql-$DATE.sql.gz
58+
rsync -avh --remove-source-files $BACKUP_DIR/ $DST_HOST:$REMOTE_DST_DIR
59+
ssh -t -t $DST_HOST "cd $REMOTE_DST_DIR ; ls -t | grep $DBNAME | grep mysql | grep daily | sed -e 1,"$BACKUP_RETENTION_DAILY"d | xargs -d '\n' rm -R > /dev/null 2>&1"
60+
ssh -t -t $DST_HOST "cd $REMOTE_DST_DIR ; ls -t | grep $DBNAME | grep mysql | grep weekly | sed -e 1,"$BACKUP_RETENTION_WEEKLY"d | xargs -d '\n' rm -R > /dev/null 2>&1"
61+
ssh -t -t $DST_HOST "cd $REMOTE_DST_DIR ; ls -t | grep $DBNAME | grep mysql | grep monthly | sed -e 1,"$BACKUP_RETENTION_MONTHLY"d | xargs -d '\n' rm -R > /dev/null 2>&1"
62+
}
63+
64+
if [ $BACKUP_MODE == local-remote ]; then
65+
if [[ ( $BACKUP_DAILY == true ) && ( ! -z "$BACKUP_RETENTION_DAILY" ) && ( $BACKUP_RETENTION_DAILY -ne 0 ) && ( $FN == daily ) ]]; then
66+
local_remote
67+
fi
68+
if [[ ( $BACKUP_WEEKLY == true ) && ( ! -z "$BACKUP_RETENTION_WEEKLY" ) && ( $BACKUP_RETENTION_WEEKLY -ne 0 ) && ( $FN == weekly ) ]]; then
69+
local_remote
70+
fi
71+
if [[ ( $BACKUP_MONTHLY == true ) && ( ! -z "$BACKUP_RETENTION_MONTHLY" ) && ( $BACKUP_RETENTION_MONTHLY -ne 0 ) && ( $FN == monthly ) ]]; then
72+
local_remote
73+
fi
74+
elif [ $BACKUP_MODE == local-only ]; then
75+
if [[ ( $BACKUP_DAILY == true ) && ( ! -z "$BACKUP_RETENTION_DAILY" ) && ( $BACKUP_RETENTION_DAILY -ne 0 ) && ( $FN == daily ) ]]; then
76+
local_only
77+
fi
78+
if [[ ( $BACKUP_WEEKLY == true ) && ( ! -z "$BACKUP_RETENTION_WEEKLY" ) && ( $BACKUP_RETENTION_WEEKLY -ne 0 ) && ( $FN == weekly ) ]]; then
79+
local_only
80+
fi
81+
if [[ ( $BACKUP_MONTHLY == true ) && ( ! -z "$BACKUP_RETENTION_MONTHLY" ) && ( $BACKUP_RETENTION_MONTHLY -ne 0 ) && ( $FN == monthly ) ]]; then
82+
local_only
83+
fi
84+
elif [ $BACKUP_MODE == remote-only ]; then
85+
if [[ ( $BACKUP_DAILY == true ) && ( ! -z "$BACKUP_RETENTION_DAILY" ) && ( $BACKUP_RETENTION_DAILY -ne 0 ) && ( $FN == daily ) ]]; then
86+
remote_only
87+
fi
88+
if [[ ( $BACKUP_WEEKLY == true ) && ( ! -z "$BACKUP_RETENTION_WEEKLY" ) && ( $BACKUP_RETENTION_WEEKLY -ne 0 ) && ( $FN == weekly ) ]]; then
89+
remote_only
90+
fi
91+
if [[ ( $BACKUP_MONTHLY == true ) && ( ! -z "$BACKUP_RETENTION_MONTHLY" ) && ( $BACKUP_RETENTION_MONTHLY -ne 0 ) && ( $FN == monthly ) ]]; then
92+
remote_only
93+
fi
94+
fi

0 commit comments

Comments
 (0)