Skip to content

These scripts are meant for Fullstacks, Webdevs or Sysadmins who use linux based webservers; who need daily backups of MySql databases, Folders and/or Files.

License

Notifications You must be signed in to change notification settings

TylerByte666/auto-backup-scripts

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

53 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🚀 Scripts for automatically backing up MySql Databases and Folders/Files daily.

These scripts are meant for Fullstacks, Webdevs or Sysadmins who use linux based webservers; who need daily backups of MySql databases, Folders and/or Files.

These scripts have been divided in two, one for MySql databases and the other for Files and Folders so you can choose which one you need.

Please note You will need root access to use these scripts.

🥰

Scope and Purpose:

These scripts do three things:

  1. Make daily backups of Folders/Files(auto_backup_files.sh) and MySql databases(auto_backup_mysql.sh).
  2. Remove backups after the retention period that you have set for them passes.
  3. Compress Folders/Files and MySql database dump files.

Basic Instructions:

  1. Clone this repo on your web server making sure you are root.
  2. Make each script you want to use executable, by running these commands in the directory where you cloned the code:
chmod +x auto_backup_files.sh
chmod +x auto_backup_mysql.sh
  1. Edit the scripts to reflect your web server environment. See configs you must change below. 🔽

Configs you must change:

auto_backup_files.sh

FILE_BACKUP_PATH='/folder/to/backup/to'
FILE_BACKUP_SRC='/folder/to/backup'
APP_NAME='The specific application you are backing up'
BACKUP_RETAIN_DAYS=Days you want to keep backups, default 60 days.
VERBOSE= By default verbose archiving is true, to limit the size of your logs, turn this to false.

auto_backup_mysql.sh

DB_BACKUP_PATH='/folder/to/backup/to'
MYSQL_USER='Your MySql user'
DATABASE_NAME='MySql db you want to backup'
BACKUP_RETAIN_DAYS=Days you want to keep backups, default 60 days.
  1. Add the mysqldump password paramater to your .my.cnf. The my.cnf file is hidden in your home directory, usually /home/username/.my.cnf. 🔽

Please note: This will allow passwordless mysqldump commands.

nano ~/.my.cnf

Edit and Enter in the following two lines replacing YOUR_PASSWORD_HERE with your own and save the file.

[mysqldump]
password=YOUR_PASSWORD_HERE

Next, change the file permissions.

chmod 600 ~/.my.cnf
  1. Add the desired script to your /etc/cronjob.daily. 🔽
cp auto_backup_files.sh /etc/cronjob.daily/
cp auto_backup_mysql.sh /etc/cronjob.daily/

🚀 You're done and ready for disaster! 🚀



🐛 Toubleshooting

The scripts do not run.

Make sure you can run the script manually, example:

./auto_backup_files.sh

This should shout out any error.

Where can I find logs to see if they ran or not.

  • on Debian-based systems, like Ubuntu:
grep auto_backup /var/log/syslog
  • on RHEL-based systems, like CentOS:
grep auto_backup /var/log/cron

Make sure your cron service is running.

  • on Debian-based systems, like Ubuntu:
systemctl status cron
  • on RHEL-based systems, like CentOS:
systemctl status crond

🥳 DR Cheat Sheet

Files/Folders restore

For unzipping files, first clear the target directory:

rm -rf /directory/to/restore/to

Then extract and output the backup

tar -zxf backup.tar.gz -C /directory/to/restore/to

MySql database restore

First, drop the db you are restoring:

Log into mysql

mysql -u root -p

Now make sure you choose the right database:

SHOW DATABASES;

WARNING Make sure you drop the right one!

DROP DATABASE db-to-restore;

Now create it again:

CREATE DATABASE db-to-restore;

After that is successful, exit mysql and import the file:

exit;

Then, extract the backup:

gzip -d < backup-dbname.sql.gz

Last import backed up db.

mysql -u root -p db-to-restore < backup-dbname.sql

About

These scripts are meant for Fullstacks, Webdevs or Sysadmins who use linux based webservers; who need daily backups of MySql databases, Folders and/or Files.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages