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.
🥰
These scripts do three things:
- Make daily backups of Folders/Files(auto_backup_files.sh) and MySql databases(auto_backup_mysql.sh).
- Remove backups after the retention period that you have set for them passes.
- Compress Folders/Files and MySql database dump files.
- Clone this repo on your web server making sure you are root.
- 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
- Edit the scripts to reflect your web server environment. See configs you must change below. 🔽
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.
- 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
- 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/
Make sure you can run the script manually, example:
./auto_backup_files.sh
This should shout out any error.
- on Debian-based systems, like Ubuntu:
grep auto_backup /var/log/syslog
- on RHEL-based systems, like CentOS:
grep auto_backup /var/log/cron
- on Debian-based systems, like Ubuntu:
systemctl status cron
- on RHEL-based systems, like CentOS:
systemctl status crond
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
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