Backup Database and Other Attachments in ROR
- “Linux Crontab 定时任务 命令详解”:http://blog.csdn.net/tianlesoftware/article/details/5315039
Automatically backup on the remote server:
Let’s firstly assumpt:
- You have a site named “example.com”;
- You can login to it through a username “deploy”, and its password is “password”;
- You located the contents of your site in
- Your database server is Mysql, and the database for your site is example_production.
1. SSH login:
$ ssh email@example.com
2. Install the backup:
$ gem install backup
3. Prepare directories for backup files:
$ mkdir /var/www/example/backup $ mkdir /var/www/example/backup/db
4. Generate and edit backup script:
$ cd ~/ $ mkdir Backup/ $ cd Backup $ backup generate:model -t example_db --databases=mysql --compressors=gzip --storages=local $ cd models $ vim example_db.rb
Input the below content:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
Run backup command to confirm that your config works:
backup perform -t example_db
Attention: Remember to update the config file if you have changed the username and/or password of the database.
5. Install whenever:
$ gem install whenever
6. Generate a schedule.rb file with whenever:
$ cd /var/www/example/ $ mkdir config $ wheneverize .
The last command will create a new file named “schedule.rb” under
7. Add a daily task:
Open the file to edit:
1 2 3 4
8. Update whenever task into crontab:
$ whenever --update-crontab
9. Check whether the task is updated successfully or not:
$ crontab -e
If successed, you should see your new task shown at the bottom of the file.
Synchronize remote backups every day automatically by rsync and crontab:
Attention: Below operations are played on a different server, which is used to sync files. We assumpt its domain is
another.com and the username is
1. SSH login:
$ ssh firstname.lastname@example.org
2. Write a synchronize shell script:
$ mkdir ~/backup/example.com $ cd ~/backup/example.com $ vim sync.sh
Input the below contents into the new file named ‘sync.sh’:
#! /bin/bash cd /home/deploy/backup/example.com/ rsync -av -e "ssh" email@example.com:/var/www/example/backup/db/example_db/ db/ rsync -av -e "ssh" firstname.lastname@example.org:/var/www/example/shared/public/uploads . rsync -av -e "ssh" email@example.com:/var/www/example/shared/log .
and then, assign the scripe execution authority:
$ chmod 775 sync.sh
Run the script to ensure everything works well:
Attention: Because it is required to be authorized to access the targer server, you should remember to copy your ssh public key to the target server(such as, example.com) before you can sync files.
3. Open user’s crontab config file:
$ crontab -e
Append the below task at the end:
10 0 * * * /home/deploy/backup/example.com/sync.sh
Ok, that’s all. The rest work is that remember to check if there is any backups generated.