├── LICENSE ├── README.md ├── helper.md └── vpsbackup.sh /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Shubhamoy Chakrabarty 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # VPS Backup Script 2 | 3 | A script which automates the backup process for a Linux based Web Server. 4 | 5 | ## Steps Involved 6 | * Dumps the MySQL/MongoDB Database 7 | * Archives the Document Root Directory 8 | * Appends the MySQL Dump to the Archive 9 | * Moves it to another server(optional) 10 | * Removes the Intermediary/Old Files 11 | 12 | ## Configuration 13 | Read the ![guide](helper.md) for setting up the script correctly. 14 | 15 | Now set a cronjob so that the backup process is automated. Here's a crontab entry which would execute the backup script every Sunday. 16 | `$> crontab -e` 17 | `0 0 * * 0 bash /home/backup/vpsback.sh >/dev/null 2>&1` 18 | 19 | If everything goes well then this script shall execute all the above mentioned steps and automatically transfer the tarball to your backup server. 20 | 21 | ## Queries 22 | Drop a mail at me@shubhamoy.com 23 | 24 | ## Request 25 | Feel free to add your suggestions to the helper.md and send me a PR. 26 | -------------------------------------------------------------------------------- /helper.md: -------------------------------------------------------------------------------- 1 | # Guide for Obtaining Parameters Manually 2 | 3 | ## Find Apache's Document Root 4 | Normally the configuration file is stored in /etc/apache2/httpd.conf or /etc/httpd/conf/httpd.conf 5 | Execute the following command(s) when you are inside Apache configuration directory. This would return the DocumentRoot directory. You can cross check to be doubly sure. 6 | 7 | ```grep -i 'DocumentRoot' httpd.conf``` 8 | OR 9 | ```grep -R "DocumentRoot" sites-enabled``` 10 | 11 | ## Find nGINX's Document Root 12 | Execute the following command when you are inside the nginx directory(/etc/nginx/) 13 | ```grep -R "DocumentRoot sites-enabled``` 14 | 15 | ## Find MySQL's mysqldump Path 16 | ```which mysqldump``` 17 | 18 | ## Find MongoDB's mongodump Path 19 | ```which mongodump``` 20 | 21 | ## Using mongodump 22 | ```sudo mongodump --db newdb --out /var/backups/mongobackups/`date +"%m-%d-%y"```` 23 | 24 | ## Setup Auto Transfer of Backup File 25 | You need to setup a SSH based Passwordless Login from your machine to your backup machine. Read this tutorial for creating a passwordless login [link1](https://www.digitalocean.com/community/tutorials/how-to-set-up-ssh-keys--2) and [link2](http://www.thegeekstuff.com/2008/11/3-steps-to-perform-ssh-login-without-password-using-ssh-keygen-ssh-copy-id/). 26 | 27 | -------------------------------------------------------------------------------- /vpsbackup.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # VPS Backup Script by Shubhamoy 3 | # https://github.com/shubhamoy/vpsbackup 4 | 5 | # BEGIN configuration 6 | MYSQL_PASS="Enter your MySQL Password" 7 | BACKUP_DIRS="/var/www /home/me/stuff" 8 | SCP_TARGET="ubuntu@54.xxx.xxx.xxx:/home/backup/" 9 | # END configuration 10 | 11 | DATE=$(date +"%Y%m%d") 12 | MYSQLDUMP="$(which mysqldump)" 13 | TAR="$(which tar)" 14 | 15 | # Similarly other db dumps can be generated like mongodump 16 | $MYSQLDUMP -u root -h localhost -p$MYSQL_PASS dbname > sqldump$DATE.sql 17 | 18 | # Configure the path for your web directory 19 | # Apache Default Web Root: /var/www or /home/www 20 | # nGinx Default Web Root: /usr/share/nginx/html or /home/www 21 | 22 | # Creating a tarball from the web directory 23 | $TAR -cvf $DATE.tar $BACKUP_DIRS 24 | 25 | # Appending the db dump to the tarball 26 | $TAR -rvf $DATE.tar sqldump$DATE.sql 27 | 28 | # Move the backup to a different server 29 | # scp 20170317.tar ubuntu@54.xxx.xxx.xxx:/home/backup/ 30 | scp $DATE.tar $SCP_TARGET 31 | 32 | # Delete the tarball and the db dump 33 | rm -rf !"($DATE.tar | sqldump$DATE.sql)" 34 | --------------------------------------------------------------------------------