Introduction
While there are numerous backup solutions available for Linux, many require extensive configuration and maintenance, and restoring from the backup is not always simple. Incremental backups are ideal because they maintain snapshots of the files and allow for access to previous versions of files.
Linux Journal recently published an article on various backup solutions, and I thought I’d provide my incremental backup script that uses rsync
and cp
.
Incremental backup script
This script provides an incremental backup solution and only requires rsync
and cp
to be installed on the system.
|
|
Let’s break this down line by line.
Source and backup directories
First, we need to define the source and backup directories, and any directories from the source that are to be excluded from the backup:
|
|
You can add as many directories as you want here.
Backup function
Then we have the backup function. This performs the following:
- Takes an input of the source and backup directories (defined above)
- Checks to see if the source directory exists
- Prompts for a year
- Prompts for a date
- Checks to make sure the backup destination directory exists
- Executes the backup
|
|
rsync
is used to compare the files in the source to the Monthly
backup directory and then update or delete files accordingly.
Once the files are copied over via rsync
, then the cp
command is used to link the files in the Monthly
directory to the year/date/
diorectory. As the files change in the Monthly
directory, then the link also changes. This method saves disk space because files are not copied over and over again. Any files that do not change are simply linked within the filesystem. The links take up a trivial amount of disk space, and the filesystem handles all of the heavy lifting associated with tracking which files are linked and where on the filesystem. There is no database, log, etc. required to track the individual files and/or their versions.
Running backups
Finally, run the backups and confirm complete:
|
|
Results
This script provides an incremental backup record organized by year and date:
Accessing older backups is straightforward - simply navigate to the desired directory within the filesystem.
Deleting old backups
Deleting or removing old and out-of-date backups is as simple as deleting the directories. The filesystem links and files that are not linked elsewhere are removed from the filesystem, freeing up the disk space.
References
https://rsync.samba.org/ https://github.com/WayneD/rsync https://www.gnu.org/software/coreutils/ https://www.man7.org/linux/man-pages/man1/cp.1.html