There are a number of backup methods that you can use in Linux to make sure your data is safe and available. The simplest is probably to backup everything. This method, although viable under certain circumstances, is not always the smartest way to go if you have space constraints. So you can use a more selective backup method. Let’s look at one, wherein you choose the files you want backed up, and run a single line command to backup only these files.
Launch your favorite text editor and make a list of all the files you want to back up. Put the full path of each file, and have one file per line. So your final list will look something like this:
Save the file with a name like backup_list.txt, or whatever you find suitable. Now we’ll use the tar command with an extra input parameter, -T. The upper case T is a parameter that takes a list of files as input.
The command to archive a directory without the -T parameter is:
# tar -zcf archive.tar.gz directory-to-backup
We will now modify this command so that it takes the list we prepared for it as an input. We will do this using the -T option in tar:
# tar -zcf backup.tar.gz -T /etc/backup_list.txt
Check out the contents of the newly created archive, backup.tar.gz. It should contain the files you listed in the file backup_list.txt. You can make this command a bit smarter using the date command in Linux. Use this command to have Linux automatically insert the date of the backup into the filename of the archive you create. Modify the above command to look something like this:
# tar -zcf backup-`date +%Y-%m-%d`.tar.gz -T /etc/backup_list.txt
Now the archive file containing your backup will have the date, month and year of the backup. This makes it easier to store and retrieve data from your backups.