rsnapshot is a backup software based on rsync. It can make an incremental backup. rsnapshot makes an automatic incremental backup using rsync and cronjob. It use linux hardlinks to save disk space. When you make the first backup, all files are copied to the backup location, every subsequent backup makes a copy of the previous backup using Linux hard links, then copies over new and changed files, deletes files that are removed from the source folder. This way, even if you have multiple copies of the files, only one file is stored in your disk. rsnapshot is available for installation from apt repositories in Ubuntu and Debian Linux.
https://rsnapshot.org/ (GitHub)
To install rsnapshot on Ubuntu/Debian, run
apt install rsnapshot -y
Configure rsnapshot
rsnapshot configuration file available in /etc/rsnapshot.conf, you need to edit this file to configure rsnapshot.
vi /etc/rsnapshot.confTo change the location of backup folder, change
snapshot_root   /var/cache/rsnapshot/I change this to /backup/
snapshot_root   /backup/Below you will find
retain  alpha   6
retain  beta    7
retain  gamma   4To easily understand the backup schedule, I renamed it as daily/weekly/monthly
retain  daily   6
retain  weekly    3
retain  monthly   3This will store 6 daily backups, 3 weekly backups, and 3 monthly backups.
Uncomment following lines
cmd_ssh        /usr/bin/ssh
cmd_du         /usr/bin/duThe lines starting with “backup” define the backup. In these lines each parameter must be separated with TAB (space won’t work).
You can verify the configuration with the command
rsnapshot configtestTest Backup
To see the commands used by rsnapshot to take backup, run
rsnapshot -t dailyHere is rsnapshot -t daily result for the backup command
backup	/root/		localhost/
To exclude a folder from the backup, you can use +rsync_long_args=–exclude=/full/path/to/folder
Example
backup  root@31.42.184.232:/home/serverok.in/  serverok.in/  +rsync_long_args=--exclude=/home/serverok.in/html/wp-content/cacheIf you need to exclude multiple files, you can use exclude-from to specify a file with all paths to exclude.
backup	root@137.184.80.235:/home/	137.184.80.235/		+rsync_long_args=--exclude-from="/usr/serverok/exclude-server5"NOTE: use TAB to separate each item in the backup line. For arguments, use space to separate.
Bash script to schedule Backups
Here is a backup script, that checks the day of the month and the day of the week to decide what type of backup needs to run today, this avoids running multiple backups same day.
creare file
mkdir /usr/serverok/
vi /usr/serverok/backupadd following content
#!/bin/bash
# Author: Yujin Boby
# Web: https://serverok.in
DAY_OF_WEEK=$(date +%w)
DAY_OF_MONTH=$(date +%d)
if [[ "$DAY_OF_MONTH" -eq 1 ]]; then
    echo "Running monthly backup..."
    /usr/bin/rsnapshot monthly
elif [[ "$DAY_OF_WEEK" -eq 0 ]]; then
    echo "Running weekly backup..."
    /usr/bin/rsnapshot weekly
else
    echo "Running daily backup..."
    /usr/bin/rsnapshot daily
fiset a cronjob to execute the file every day.
@daily /usr/serverok/backup > /dev/null 2>&1Running Daily Backup
To run the daily automated backup, you need to set a cronjob
# Daily backup at midnight (00:00)
0 0 * * *  /usr/bin/rsnapshot daily
# Weekly backup on Sunday at 01:00
0 1 * * 0  /usr/bin/rsnapshot weekly
# Monthly backup on 1st of each month at 02:00
0 2 1 * *  /usr/bin/rsnapshot monthlyThis will run backup every day at 00:00 hours server time.
MySQL Backup
Create file
vi /usr/serverok/backup-mysql.shAdd following content
#!/bin/bash
# MySQL Backup Scipt
# Author: admin@ServerOk.in
# @daily /usr/serverok/backup-mysql.sh > /var/log/backup-mysql.log 2>&1
BACKUP_DIR="/backup/tmp/"
if [ ! -d $BACKUP_DIR ]; then
    mkdir -p $BACKUP_DIR
fi
find "${BACKUP_DIR}" -maxdepth 1 -type f -mtime +30 -name *.sql -delete
# Backup MySQL Database
mysql -e "show databases" | egrep -v "(-|Database|mysql|information_schema|performance_schema|phpmyadmin)"  > /tmp/sok-dbs.txt
for db in `cat /tmp/sok-dbs.txt`
do
    /usr/bin/mysqldump --opt --triggers --routines --events $db > "${BACKUP_DIR}/${db}.sql";
done
Make it executable
chmod 755 /usr/serverok/backup-mysql.shEdit file
vi /etc/rsnapshot.confAdd
backup_script	/usr/serverok/backup-mysql.sh	localhost/mysql/In the above line, white spaces are tabs, not spaces.
See Backup

Leave a Reply