Code Monkey home page Code Monkey logo

bash-backup-rotation-script's Introduction

Bash Backup Rotation Script

Simple script which can be easily modified if needed for backup rotation. This script use ssh and rsync for syncing This script is completely rewriten , old script can be found here : https://github.com/todiadiyatmo/bash-backup-rotation-script/tree/legacy-1.0.0 .

Feature

  • Daily, Weekly, Monthly backup script with number of retention (backup to keep) option
  • backup to local only, local + remote and remote only mode
  • 4 different backup script :
    • file backup with zip compression script
    • rsync script
    • mysql script
    • mysql with extrabackup script
  • Secure backup with SSH connection

Todo

This release is still missing this feature from the old relesae

  • email notification

Usage

MySQL / MySQL Extrabackup / Zip File Backup

  • Copy script to desired location
  • Edit the parameter of the script, configure the BACKUP_RETENTION_ to set the rotation / number of backup needed
### User Pass Mysql ###
USER=backup
PASS=backup
DBNAME=project_sql
BACKUP_DIR="/root/backup"
DST_HOST="user@host"
REMOTE_DST_DIR="/root/backup"
BACKUP_DAILY=true # if set to false backup will not work
BACKUP_WEEKLY=true # if set to false backup will not work
BACKUP_MONTHLY=true # if set to false backup will not work
BACKUP_RETENTION_DAILY=3
BACKUP_RETENTION_WEEKLY=3
BACKUP_RETENTION_MONTHLY=3
  • test the script to make sure everything correct , ex : mysql-backup-script.sh
  • put script on cron to make sure it is running everyday at your desired time : 00 03 * * * backup.sh
  • check your backup result
  • profit :)

Pull request and issue

feel free to open pull request and submit bug ticket

bash-backup-rotation-script's People

Contributors

atryfox avatar clickwir avatar kujiy avatar mehmandarov avatar miketweaver avatar todiadiyatmo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

bash-backup-rotation-script's Issues

Better backup.conf suggestion : separate DB and FTP

Hi,

In fact, in your server, you can have multiple sites (or directories) without sql database, a web site with 2 or 3 databases.
So, in this case, a better backup.conf can be with separate DB and FTP.

/Nico

Finished, but failed.

Keep getting mails like:

/backupdav/18-08-2016-daily.data.tar.xz does not seem to exist. Something failed.
When it run as cron, works if i run it manual.

Multiple problem with DEST_DIR="$DEST_BASE_DIR/$HOST/$DATABASE"

Hi,
Good work this script !
But little bug with DEST_DIR="$DEST_BASE_DIR/$HOST/$DATABASE".

  1. backup.sh:33
    mkdir: cannot create directory '/thebasedir/localhost/mydatabase': No such file or directory.

  2. backup_rotation.sh, line 397
    echo "mkdir $FTP_TARGET_DIR" >> $TMP_DIR/backup.incoming/ftp_command.tmp
    The exection of this ftp script say me that it cant create the dir, because there is 3 (or 2) directory to create. FTP seems only works with 1 directory to create.

Bisous.

Incorrect harmless error when SQL_BACKUP_OPTION=0

Using this option set:

LOCAL_BACKUP_OPTION=1
FTP_BACKUP_OPTION=0
SQL_BACKUP_OPTION=0
FILES_BACKUP_OPTION=7

Script will give incorrect error due to this code (it's not really optional, it's happening always):

if [ ! -f $TMP_DIR/backup.incoming/$backup_filename ]; then
echo "Daily backup failed! Please check for missing files." | mail -s "$EMAIL_SUBJECT_TAG Backup Failed" $MAIL
fi

One possible fix is to only check for back-up file name when SQL backup option is set to non-zero? We are using script for CouchDB backup so we used this for temporary fix:

if [ ! $PERFORM_SQL_BACKUP -eq 0 ]; then
if [ ! -f $TMP_DIR/backup.incoming/$backup_filename ]; then
echo "Daily backup failed! Please check for missing files." | mail -s "$EMAIL_SUBJECT_TAG Backup Failed" $MAIL
fi
fi

FTP upload failed

Using Ubuntu 14.04 and vsftpd on both systems.
$ vsftpd -v
$ vsftpd: version 3.0.2

I keep getting the following error:

local: /tmp/backup.incoming/27-01-2016-daily.sql.tar.xz remote: dcim_backup_opendcim/27-01-2016-daily.sql.tar.xz
200 PORT command successful. Consider using PASV.
553 Could not create file.

So I found a simple change that fixes it, YMMV of course. This works for me.

-echo "put $TMP_DIR/backup.incoming/$backup_filename $FTP_TARGET_DIR/$backup_filename" >> $TMP_DIR/backup.incoming/ftp_command.tmp
+echo "put $TMP_DIR/backup.incoming/$backup_filename $backup_filename" >> $TMP_DIR/backup.incoming/ftp_command.tmp

-echo "put $TMP_DIR/backup.incoming/$backup_filename_sql $FTP_TARGET_DIR/$backup_filename_sql" >> $TMP_DIR/backup.incoming/ftp_command.tmp
+echo "put $TMP_DIR/backup.incoming/$backup_filename_sql $backup_filename_sql" >> $TMP_DIR/backup.incoming/ftp_command.tmp

backup error

Hi thanks for your script but I get this error:

# sh backup_rotation.sh
backup_rotation.sh: 124: backup_rotation.sh: [[: not found
backup_rotation.sh: 128: backup_rotation.sh: [[: not found
mkdir: cannot create directory `/home/backup': File exists

Regards

Script Failure

After setting up the configuration file and running the script, I get the following error:

root@kraken:~/backups# bash backup_rotation.sh
Removing expired backups...
Creating backup dir..
Copy backup to local dir..
mv: cannot stat `/tmp/backup.incoming/*': No such file or directory
Finish..

However the directory does exist:

root@kraken:~/backups# ls -ahl /tmp/backup.incoming/
total 8.0K
drwxr-xr-x 2 root root 4.0K Dec  1 16:30 .
drwxrwxrwt 9 root root 4.0K Dec  1 16:30 ..

Any idea what could be happening

Doesn't delete old files

This section of code is incorrect

delete old files

find $TMP_DIR/ -maxdepth 1 -mtime +$RETENTION_DAY_LOOKUP -name "$BACKUP_TYPE" -exec rm -rv {} ;
find $TMP_DIR/.ftp_cache/ -maxdepth 1 -mtime +$RETENTION_DAY_LOOKUP -name "$BACKUP_TYPE" -exec rm -rv {} ;

Issue uploading to FTP

Hi, firstly thanks for writing this script!

I have the following issue while trying to get the file to send via FTP:

'Using binary mode to transfer files.
550 Can't create directory: File exists
250 OK. Current directory is /backups
200 TYPE is now 8-bit binary
local: /home/backup/backup.incoming/04-05-2015-daily.tgz remote: .
200 PORT command successful
553 Can't open that file: Is a directory
221-Goodbye. You uploaded 0 and downloaded 0 kbytes.
221 Logout.'

Thanks

DBHOST

Thank you so much for the code, it's perfect for what I needed.

Just a small improvement, I added a $DBHOST and $OPTIONS to the mysqldump ($OPTIONS in my case --column-statistics=0)

In case you want to keep it in mind

Thank you very much.

Greetings

Target_dir vs Backup_dir

# The directory to be backup , DO NOT END THE DIRECTORY WITH BACKSLASH !
TARGET_DIR=/target_directory
# The backup directory , DO NOT END THE DIRECTORY WITH BACKSLASH !
BACKUP_DIR=/copy_target_to_backup_directory

I think these are hard to understand. I would normally think the target directory should be where you want the files to go, and the backup directory to be the files that are being backed up.

Switching them and adding some extra words would make more sense:

# The directory where the files to be backed up are located DO NOT END THE DIRECTORY WITH BACKSLASH !
BACKUP_DIR=/backup_directory

# The directory where the backups are sent , DO NOT END THE DIRECTORY WITH BACKSLASH !
TARGET_DIR=/copy_backup_directory_to_here

Note: you will have to go through the script and switch them as well. It's not that many though.

Check LOCAL_BACKUP_OPTION value

The script should check the value of LOCAL_BACKUP_OPTION before executing "Cleanup expired backups", "Perform Files Backup" and "Optional check if source files exist. Email if failed." I have disabled local backup and use only FTP backup, so I'm getting error messages about the missing local target directory and email about "Finished, but failed".

Correct grep for deleting sql files

The local_remote function names the output file as mysql but the other functions name the output file as just "sql". This means that the "grep mysql" in the other functions won't find anything to delete. In my version I have normalised this so that all files are names "sql" and all searches also use "sql". I don't know if this repo is still being maintained but in case this helps anyone.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.