This backup method will back up your local VPS databases, files, and configuration settings to another (remote) server of yours. It will also back up your remote server’s files to your local VPS.
Step 1: Install Zip
We must first make sure the zip package is installed. To do so, type in the following via SSH:
dpkg -l zip
If you get something like dpkg-query: no packages found matching zip, then you do not have the zip package installed. However, if you get something that lists the name and version number of the zip package, this means you have the zip package installed.
If you do not have the zip package installed, type the following to install it:
apt-get install zip
When prompted whether or not you want to continue, type Y and then hit Enter.
Step 2: Generate SSH Keys
On your local server, via SSH, type in the following to generate SSH Keys:
ssh-keygen -t rsa -b 4096
When asked to “Enter file in which to save the key”, hit Enter.
When asked to “Enter passphrase (empty for no passphrase)”, hit Enter.
When asked to “Enter same passphrase again”, hit Enter.
Now, type in the following to copy the public key to the remote server using ssh-copy-id:
ssh-copy-id -i ~/.ssh/id_rsa.pub root@123.123.123.123
When asked “Are you sure you want to continue connecting”, type yes and hit Enter.
When prompted for the password of your remote server, type it in and hit Enter.
Now, test to make sure the SSH keys were properly added by trying to log in to the remote server by typing in the following:
ssh root@123.123.123.123
You should be granted access to your remote server without being prompted for a password.
Repeat these same steps on your remote server if you also want to back up your remote server to your local server.
Log on to your other server. Via SSH, type in the following to generate SSH Keys:
ssh-keygen -t rsa -b 4096
When asked to “Enter file in which to save the key”, hit Enter.
When asked to “Enter passphrase (empty for no passphrase)”, hit Enter.
When asked to “Enter same passphrase again”, hit Enter.
Now, type in the following to copy the public key to your other server using ssh-copy-id:
ssh-copy-id -i ~/.ssh/id_rsa.pub root@321.321.321.321
When asked “Are you sure you want to continue connecting”, type yes and hit Enter.
When prompted for the password of your remote server, type it in and hit Enter.
Now, test to make sure the SSH keys were properly added by trying to log in to the remote server by typing in the following:
ssh root@321.321.321.321
You should be granted access to your remote server without being prompted for a password.
Step 3: Create Backup Directory
We must first create a directory which will store the backups. On each of your servers, type in the following to create the directory which will store the backups:
mkdir /var/backups
Step 4: Create Configuration File for MySQL Database Dumps without using Password
On each of your servers, type in the following to create a new file (with restricted permissions) which will allow us to back up all databases without having to enter in a password each time. Substitute yourpassword with the actual password for your root MySQL user.
echo -e '[mysqldump]\npassword=yourpassword' > /etc/custom/.my.cnf && chmod 0440 /etc/custom/.my.cnf
Step 5: Back Up
On each of your servers you’d like to back up, type in the following to create a backup script:
rm -f /var/backups/firstserver.zip && echo "mysqldump --defaults-file=/etc/custom/.my.cnf --user='root' --all-databases --events --ignore-table=mysql.event > /var/backups/databases.sql && crontab -l > /var/backups/crontab && zip -r /var/backups/firstserver.zip /etc/hosts /etc/hostname /etc/apt/sources.list /etc/ajenti /etc/custom /etc/nginx /etc/nginx.custom.d /etc/ssl/private/pure-ftpd.pem /etc/pure-ftpd/conf/TLS /etc/fail2ban/jail.local /etc/network/if-pre-up.d/firewall /etc/sysctl.conf /etc/sudoers.d/my_sudoers /etc/rc.local /etc/mysql/my.cnf /etc/init/mysql.conf /etc/php5/fpm/php.ini /var/www /var/log /var/backups/databases.sql /var/backups/crontab && rm /var/backups/databases.sql && rm /var/backups/crontab && chmod 0440 /var/backups/firstserver.zip && rsync -avz --partial --delete /var/backups/firstserver.zip root@123.123.123.123:/var/backups/firstserver.zip" > /etc/custom/backup.sh
Type the following to apply proper permissions to the backup script file we just created:
chmod 0750 /etc/custom/backup.sh
Now, type in the following to run the backup script:
/etc/custom/backup.sh
Step 6: Enable Automatic Backups
To enable automatic backups every day, we must add our newly created backup script to the crontab. To do so, type in the following:
cat <(crontab -l) <(echo "@daily /etc/custom/backup.sh") | crontab -
Further Reading: