├── README.md ├── authenticator.sh ├── how-to-create-ssh-key.md ├── profile-compressed-backup.sh └── profile.sh /README.md: -------------------------------------------------------------------------------- 1 | # backuplift 2 | 3 | ![](https://pbcdn.cloud/backuplift.png) 4 | 5 | Backups are the most important part of securing your website. But many people are having a hard time finding cost - effective ways to back up the website. With the backuplift script, you can set up a backup to be stored on a Hetzner storage box. 6 | 7 | - Run backup with cron, 8 | - Takes 5-minutes to setup everything. 9 | 10 | Manage multiple backup profiles: 11 | Create multiple profile.sh files to backup multipe wordpress sites. 12 | - profile-one.sh 13 | - profile-two.sh 14 | 15 | Important notes: 16 | - By default, profile.sh will send site files without compressing them. 17 | - If you like to send files compressing them, use `profile-compressed-backup.sh` instead. 18 | - Make sure to test run once the setup is done. 19 | - You must have rsync installed in your linux server. Test it by typing `rsync` in your terminal. If you see manual, it would mean rsync is installed already. If not, you'll have to install it. 20 | 21 | ------------ 22 | 23 | # How to use this? 24 | 25 | ### **STEP 1. Create SSH key for secure comminication.** 26 | 27 | - I have written down step by step way to create & authenticate your storagebox to your server, [click here to read md file](https://github.com/manoj-wpzonify/backuplift/blob/main/how-to-create-ssh-key.md "click here to read md file"). 28 | - *Only prorceed to next step if you were able to authenticate properly 29 | 30 | ### **STEP 2. download profile.sh file & upload to your server** 31 | 32 | - Create folder, name it backuplift. 33 | - Then download the profile.sh & upload to backuplift folder. 34 | - make profile.sh executable file. 35 | 36 | ------------ 37 | 38 | 39 | - or you can type the following command to do it. [This will create folder, download the profile.sh file & make the file executable] 40 | 41 | ```html 42 | mkdir backuplift && wget https://raw.githubusercontent.com/manoj-wpzonify/backuplift/main/profile.sh -P backuplift && chmod +x backuplift/profile.sh 43 | ``` 44 | 45 | - *it would be good idea to do this in the `/home` directory. of course you can do it anywhere you want.* 46 | 47 | ### **STEP 3. Modify profile.sh file with your own storagebox details** 48 | 49 | - Open the profile.sh file 50 | 51 | ```html 52 | nano profile.sh 53 | ``` 54 | 55 | - And modify the following with your storagebox details 56 | 57 | ```html 58 | remote_dirs="" -------------> remote directory location. 59 | remote_user="" -------------> hetzner storagebox username. 60 | remote_server=""-------------> hetzner storagebox hostname. 61 | target_dir="" -------------> Your website directory path. 62 | ``` 63 | 64 | - Save the changes & close the editor. 65 | 66 | ### **STEP 4. Create cronjob that runs as per your preference** 67 | 68 | - You can find the example cronjob below. This runs midnight, you can customize the way you want. 69 | 70 | ```html 71 | 0 0 * * * bash /backuplift/profile.sh 72 | ``` 73 | 74 | ------------ 75 | 76 | 77 | 78 | # That's all, Your backups now will run every day & stored in your hetzner. 79 | -------------------------------------------------------------------------------- /authenticator.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | 4 | # Check if id_rsa_backuplift_hetzner_backups exists 5 | if [ -f .ssh/id_rsa_backuplift_hetzner_backups ]; then 6 | echo "id_rsa_backuplift_hetzner_backups already exists. Aborting..." 7 | exit 1 8 | fi 9 | 10 | # Generate ssh key without asking for passphrase and store it in .ssh/id_rsa_backuplift_hetzner_backups 11 | ssh-keygen -t rsa -b 4096 -N "" -f .ssh/id_rsa_backuplift_hetzner_backups 12 | 13 | # Convert key to RFC4716 14 | ssh-keygen -e -f .ssh/id_rsa_backuplift_hetzner_backups.pub | grep -v "Comment:" > .ssh/id_rsa_backuplift_hetzner_backups_rfc.pub 15 | 16 | # Append the public keys to storagebox_authorized_keys 17 | cat .ssh/id_rsa_backuplift_hetzner_backups.pub >> .ssh/storagebox_authorized_keys 18 | cat .ssh/id_rsa_backuplift_hetzner_backups_rfc.pub >> .ssh/storagebox_authorized_keys 19 | 20 | # Create .ssh directory in the remote server and upload authorized_keys 21 | echo "mkdir .ssh" | sftp username@username.your-storagebox.de 22 | scp .ssh/storagebox_authorized_keys username@username.your-storagebox.de:.ssh/authorized_keys 23 | -------------------------------------------------------------------------------- /how-to-create-ssh-key.md: -------------------------------------------------------------------------------- 1 | **[NEW] You can update hetzner storagebox username in authenticator.sh file and run it to do the following without doing it manually.** 2 | 3 | Update username and run this command: 4 | 5 | `chmod +x backuplift/profile.sh ; ./backuplift/authenticator.sh` 6 | 7 | 8 | -- 9 | 10 | **[OLD] You can also do this manually:** 11 | 12 | **To generate new ssh key, type the following in your server ssh terminal** 13 | 14 | `ssh-keygen` 15 | 16 | **The above command will prompt to ask for location. type the following including dot** 17 | 18 | `.ssh/id_rsa_backuplift_hetzner_backups` 19 | 20 | **Converting your key to RFC4716 format - must for sftp** 21 | 22 | `ssh-keygen -e -f .ssh/id_rsa_backuplift_hetzner_backups.pub | grep -v "Comment:" > .ssh/id_rsa_backuplift_hetzner_backups_rfc.pub` 23 | 24 | **Creating authorized_keys file** 25 | 26 | `cat .ssh/id_rsa_backuplift_hetzner_backups.pub >> .ssh/storagebox_authorized_keys` 27 | 28 | **For SFTP - must** 29 | 30 | `cat .ssh/id_rsa_backuplift_hetzner_backups_rfc.pub >> .ssh/storagebox_authorized_keys` 31 | 32 | **copy storagebox_authorized_keys content to storagebox .ssh/authorized_keys** 33 | 34 | `cat .ssh/storagebox_authorized_keys` 35 | 36 | Now copy the content displayed, note down somewhere else. 37 | 38 | **Create ssh folder in storagebox** 39 | 40 | - Create `.ssh` folder in the root directory and create file called `authorized_keys` inside .ssh folder 41 | 42 | - Paste the copied content inside authorized_keys with your favorite editor. 43 | 44 | ***Thats all, test connection using the following command in your server ssh terminal*** 45 | 46 | `sftp -i .ssh/id_rsa_backuplift_hetzner_backups username@username.your-storagebox.de` 47 | -------------------------------------------------------------------------------- /profile-compressed-backup.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | 4 | # START EDITING.. 5 | 6 | remote_dirs="" 7 | remote_user="" 8 | remote_server="" 9 | target_dir="" 10 | 11 | # STOP EDITING..! 12 | 13 | 14 | date=$(date +"%d-%b-%Y") 15 | ssh_key_loc="/root/.ssh/id_rsa_backuplift_hetzner_backups" 16 | random_file_name=$(echo $RANDOM | md5sum | head -c 20; echo;) 17 | wpmysqluser=$(grep -w "DB_USER" $target_dir/wp-config.php | cut --delimiter="'" --fields="4") 18 | wpmysqldb=$(grep -w "DB_NAME" $target_dir/wp-config.php | cut --delimiter="'" --fields="4") 19 | wpmysqluserpass=$(grep -w "DB_PASSWORD" $target_dir/wp-config.php | cut --delimiter="'" --fields="4") 20 | 21 | # create backup folder if not exist 22 | 23 | echo "Creating backup directory" 24 | mkdir -p $target_dir/backuplift/$date 25 | echo "Finished creating directory" 26 | 27 | #Create database dump file or backup file. 28 | 29 | mysqldump --no-tablespaces -u $wpmysqluser -p$wpmysqluserpass $wpmysqldb > $target_dir/backuplift/$date/database-$date-$random_file_name.sql 30 | 31 | #Creating directory zip file with date. 32 | 33 | echo "Compressing your directory" 34 | tar -zcvf $target_dir/backuplift/$date/files-$date-$random_file_name.tar.gz $target_dir 35 | echo "compressed the directory is finished" 36 | 37 | #transferring compressed date directory via rsync 38 | 39 | echo 40 | echo "starting rsync backup..." 41 | rsync --progress -auvz -e 'ssh -p23 $ssh_key_loc' --recursive $target_dir/backuplift/$date $remote_user@$remote_server:$remote_dirs 42 | echo "done. Finished sending backup date directory to remote location" 43 | 44 | #delete backup folder after transfering to remote location. 45 | 46 | echo "deleting your backup date folder & sql file" 47 | rm -rf $target_dir/backuplift 48 | echo "finished deleting the backup" 49 | echo "....................." 50 | echo "....................." 51 | echo "....................." 52 | echo "Backup job finished, isn't it awesome?" 53 | -------------------------------------------------------------------------------- /profile.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | 4 | # START EDITING.. 5 | 6 | remote_dirs="" 7 | remote_user="" 8 | remote_server="" 9 | target_dir="" 10 | 11 | # STOP EDITING..! 12 | 13 | 14 | date=$(date +"%d-%b-%Y") 15 | ssh_key_loc="/root/.ssh/id_rsa_backuplift_hetzner_backups" 16 | random_file_name=$(echo $RANDOM | md5sum | head -c 20; echo;) 17 | wpmysqluser=$(grep -w "DB_USER" $target_dir/wp-config.php | cut --delimiter="'" --fields="4") 18 | wpmysqldb=$(grep -w "DB_NAME" $target_dir/wp-config.php | cut --delimiter="'" --fields="4") 19 | wpmysqluserpass=$(grep -w "DB_PASSWORD" $target_dir/wp-config.php | cut --delimiter="'" --fields="4") 20 | 21 | 22 | # create backup folder if not exist 23 | 24 | echo "Creating backup directory" 25 | mkdir -p $target_dir/wp-database 26 | echo "Finished creating directory" 27 | 28 | 29 | #Create database dump file or backup file. 30 | echo "....................." 31 | echo "....................." 32 | echo "Exporting sql file..." 33 | echo "....................." 34 | echo "....................." 35 | mysqldump --no-tablespaces -u $wpmysqluser -p$wpmysqluserpass $wpmysqldb > $target_dir/wp-database/$date-$random_file_name.sql 36 | echo "done.!" 37 | echo "....................." 38 | echo "....................." 39 | echo "This SQL file will be sent to remote location via rsync" 40 | 41 | #transferring compressed date directory via rsync 42 | 43 | echo "....................." 44 | echo "....................." 45 | echo "starting rsync backup" 46 | echo "This process will take time depending on size of your website..." 47 | rsync --progress -auvz -e "ssh -p23 -i $ssh_key_loc" --recursive $target_dir $remote_user@$remote_server:$remote_dirs/$date 48 | echo "done.!" 49 | echo "....................." 50 | echo "....................." 51 | echo "Finished sending backup date directory to remote location" 52 | 53 | 54 | #delete backup folder after transfering to remote location. 55 | 56 | echo "deleting your SQL file to avoid impacting disk space usage" 57 | echo "....................." 58 | echo "....................." 59 | rm -rf $target_dir/wp-database 60 | echo "done.!" 61 | echo "....................." 62 | echo "....................." 63 | echo "finished backups. " 64 | 65 | --------------------------------------------------------------------------------