DEV Community

Cover image for Easy Way To Backup Your Data In GNU/Linux
Aris Ripandi
Aris Ripandi

Posted on • Updated on

Easy Way To Backup Your Data In GNU/Linux

Data is one thing that is very important for us. Data loss is a disaster for everyone, including a System Administrator. Therefore, it is necessary to backup your data regularly. In Linux we can easily to backup data automatically and scheduled. The following will explain how.

Step 01 – setup ssh

This step should be done because we will upload the backup file to a remote server. At this stage we will arrange to login SSH without password.

Create RSA key:

mkdir -p ~/.ssh  
touch ~/.ssh/authorized_keys  
ssh-keygen -t rsa -b 2048  
chmod go-w ~/  
chmod 700 ~/.ssh  
chmod 600 ~/.ssh/*
Enter fullscreen mode Exit fullscreen mode

Upload public key to backup server:

ssh-copy-id -i ~/.ssh/id_rsa.pub '-o StrictHostKeyChecking=no REMOTEUSER@REMOETHOST -pREMOTEPORT'  
Enter fullscreen mode Exit fullscreen mode

Create backup directory in local and remote server:

mkdir -p /var/backup; chown -R REMOTEUSER /var/backup  
ssh REMOTEUSER@REMOETHOST -pREMOTEPORT 'sudo mkdir -p /var/backup; sudo chown -R REMOTEUSER /var/backup'
Enter fullscreen mode Exit fullscreen mode

Step 02 – install backup2l

# In RedHat based distros:
yum update  
yum install backup2l

# In Debian based distros:
apt-get update  
apt-get install backup2l 

Enter fullscreen mode Exit fullscreen mode

Or you can build it from source. Please read the official documentation for more information.

Step 03 – configure backup2l

Edit /etc/backup2l.conf and create configuration like this:

FOR_VERSION=1.5  
VOLNAME="all"  
SRCLIST=(/etc/apache2/sites-available /home)  
SKIPCOND=(-path "/home/REMOTEUSER" "/etc/apache2/sites-available/default*" "*.nobackup*" -o -name "*.o")  
BACKUP_DIR="/var/backup"  
MAX_LEVEL=3  
MAX_PER_LEVEL=9  
MAX_FULL=2  
GENERATIONS=1  
CREATE_CHECK_FILE=1  
AUTORUN=0  
SIZE_UNITS="M"  
UNCONFIGURED=0  
CREATE_DRIVER="DRIVER_TAR_GZ"  
PRE_BACKUP () {  
  echo -e "  Backup MySQL Databases:"
  echo -e "  -----------------------"
  /usr/bin/mysqldump -u root -p --all-databases --skip-lock-tables | bzip2 > /home/all_db.sql.bz2
}
POST_BACKUP () {  
  echo -e "  Uploading Backup File to Backup Server:"
  echo -e "  ---------------------------------------"
  /usr/bin/scp -r /var/backup/all.*.tar.gz REMOTEUSER@REMOTEHOST:/var/backup/.
}
Enter fullscreen mode Exit fullscreen mode

Create Cron for auto backup, in this example I just create automatic backup every week. You can edit as you want.

cat > /etc/cron.d/autobackup <<EOF  
@weekly /usr/sbin/backup2l > /dev/null || nice -n 19 ionice -c 3 backup2l -b
EOF
Enter fullscreen mode Exit fullscreen mode

Important: You can modify the configuration as you want. Remember to change REMOTEUSER, REMOTEHOST, and REMOTEPORT variables.

Top comments (5)

Collapse
 
ferricoxide profile image
Thomas H Jones II

Worth noting that most ssh client installations, if ${HOME}/.ssh doesn't exist, it will be created when you run ssh-keygen.

Collapse
 
aris profile image
Aris Ripandi

Yes you right, by default .ssh is doesn't exist.

Collapse
 
ondrejs profile image
Ondrej • Edited

You should use ssh-keygen -t rsa -b 4096, as 2048 bits for keys are considered as weak.

Collapse
 
aris profile image
Aris Ripandi

Thank you.

Collapse
 
ondrejs profile image
Ondrej

You're welcome :)